如何在python中获取打开文件的win32句柄? [英] How to get a win32 handle of an open file in python?

查看:64
本文介绍了如何在python中获取打开文件的win32句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这在某处有记录,但我找不到它...

我的代码正在从另一个库中获取一个 python 对象(我无法修改),我需要在它上面调用一些 win32 api 函数.

Python 从 file.fileno() 返回一些不是操作系统级句柄的东西,我猜它给出了 MSVCRT 的文件号.

<预><代码>>>>ctypes.windll.kernel32.CreateFileA('test',0x80000000L,1,None,3,0,0)1948 # <- 手柄>>>file('test','r').fileno()4 # <- 不是句柄

如何将其转换为真正的 win32 句柄?

解决方案

我找到了答案:

<预><代码>>>>msvcrt.get_osfhandle(a.fileno())1956 # 有效的句柄

这实际上记录在 http://docs.python.org/library/msvcrt.html ,不知道我是怎么错过的.

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 returns something that isn't the os-level handle from file.fileno(), my guess is that it gives MSVCRT's fileno.

>>> 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?

解决方案

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.

这篇关于如何在python中获取打开文件的win32句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆