[英]How to get a win32 handle of an open file in python?
I'm sure this is documented somewhere but i can't find it...我确定这在某处有记录,但我找不到...
My code is getting a python object from another library (that i can't modify), and i need to call some win32 api functions on it.我的代码正在从另一个库(我无法修改)获取 python object,我需要在其上调用一些 win32 api 函数。
Python returns something that isn't the os-level handle from file.fileno(), my guess is that it gives MSVCRT's fileno. Python 从 file.fileno() 返回不是操作系统级句柄的东西,我猜它给出了 MSVCRT 的文件号。
>>> ctypes.windll.kernel32.CreateFileA('test',0x80000000L,1,None,3,0,0)
1948 # <- HANDLE
>>> file('test','r').fileno()
4 # <- not a HANDLE
How do i convert it into a real win32 handle?如何将其转换为真正的 win32 句柄?
I found the answer:我找到了答案:
>>> msvcrt.get_osfhandle(a.fileno())
1956 # valid HANDLE
This is actually documented on http://docs.python.org/library/msvcrt.html , no idea how i missed it.这实际上记录在http://docs.python.org/library/msvcrt.html上,不知道我是怎么错过的。
win32file._get_osfhandle
from the PyWin32
library will return what you want. PyWin32
库中的win32file._get_osfhandle
将返回您想要的内容。 win32file._get_osfhandle(a.fileno())
returns the same thing as msvcrt.get_osfhandle(a.fileno())
in my testing. win32file._get_osfhandle(a.fileno())
msvcrt.get_osfhandle(a.fileno())
在我的测试中返回与 msvcrt.get_osfhandle(a.fileno()) 相同的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.