And*_*rew 2 python 64-bit ctypes msvcrt
我想使用ctypes包从64位python中调用msvcrt函数.我显然做错了.这是明智的正确方法吗?
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> libc = ctypes.cdll.msvcrt
>>> fp = libc.fopen('text.txt', 'wb') #Seems to work, creates a file
>>> libc.fclose(ctypes.c_void_p(fp))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
WindowsError: exception: access violation reading 0xFFFFFFFFFF082B28
>>>
Run Code Online (Sandbox Code Playgroud)
如果这个代码做了我想要的,它将打开并关闭一个文本文件而不会崩溃.
默认的ctypes结果类型是32位整数,但文件句柄是指针宽度,即64位.因此,您丢失了文件指针中的一半信息.
在调用fopen之前,必须声明结果类型是指针:
libc.fopen.restype = ctypes.c_void_p
fp = libc.fopen(...)
libc.fclose(fp)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1394 次 |
| 最近记录: |