在 Windows 上使用 Python 打开设备句柄
我正在尝试使用 Giveio.sys 驱动程序,该驱动程序需要先打开一个“文件”,然后才能访问受保护的内存。 我正在查看 WinAVR/AVRdude 中的一个 C 示例,它使用以下语法:
#define DRIVERNAME "\\\\.\\giveio"
HANDLE h = CreateFile(DRIVERNAME,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
但这似乎在 Python 中不起作用 - 我只是得到一个“指定的路径无效”错误,对于两者来说,
f = os.open("\\\\.\\giveio", os.O_RDONLY)
为什么
f = os.open("//./giveio", os.O_RDONLY)
这不执行一样?
编辑希望减少想法的混乱(感谢威尔)。 我确实通过 AVRdude 附带的批处理文件验证了设备驱动程序正在运行。
进一步编辑以澄清 SamB 的赏金。
I'm trying to use the giveio.sys driver which requires a "file" to be opened before you can access protected memory. I'm looking at a C example from WinAVR/AVRdude that uses the syntax:
#define DRIVERNAME "\\\\.\\giveio"
HANDLE h = CreateFile(DRIVERNAME,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
but this does not seem to work in Python - I just get a "The specified path is invalid" error, for both
f = os.open("\\\\.\\giveio", os.O_RDONLY)
and
f = os.open("//./giveio", os.O_RDONLY)
Why doesn't this do the same thing?
Edited to hopefully reduce confusion of ideas (thanks Will).
I did verify that the device driver is running via the batch files that come with AVRdude.
Further edited to clarify SamB's bounty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
解决方案:在Python中你必须使用win32file.CreateFile()而不是open()。 感谢大家告诉我我正在尝试做什么,它帮助我找到了答案!
Solution: in python you have to use win32file.CreateFile() instead of open(). Thanks everyone for telling me what I was trying to do, it helped me find the answer!
我对Python一无所知,但我对驱动程序有所了解。 您根本不是在尝试“在内核空间中打开文件” - 您只是在尝试打开设备的句柄,而该设备恰好看起来有点像打开文件。
CreateFile 是一个用户模式函数,您在这里所做的一切都是用户模式,而不是内核模式。
正如 xenon 所说,你的调用可能会失败,因为你还没有加载驱动程序,或者因为你用来执行 CreateFile 的任何 Python 调用都没有传递写入参数。
我自己从未使用过 Giveio.sys,但就我个人而言,在我尝试通过 Python 使其工作之前,我会确定它已通过使用“C”或 C++(或某些预先编写的应用程序)正确加载。
I don't know anything about Python, but I do know a bit about drivers. You're not trying to 'open a file in kernel space' at all - you're just trying to open a handle to a device which happens to be made to look a bit like opening a file.
CreateFile is a user-mode function, and everything you're doing here is user-mode, not kernel mode.
As xenon says, your call may be failing because you haven't loaded the driver yet, or because whatever Python call you're using to do the CreateFile is not passing the write parameters in.
I've never used giveio.sys myself, but personally I would establish that it was loaded correctly by using 'C' or C++ (or some pre-written app) before I tried to get it working via Python.
至少可以说你的问题非常令人困惑。
1> 您粘贴的代码使用了一种技巧,使用“DOSNAME”与驱动程序进行通信,即
2> 您是否创建了& 加载“giveio”驱动程序?
驱动程序处理此调用的原因是由于
http://msdn.microsoft .com/en-us/library/ms806162.aspx
You're question is very confusing to say the least.
1> The code you pasted is using a trick to communicate with the driver using its 'DOSNAME' i.e.
2> Have you created & loaded the 'giveio' driver ?
The reason the driver handles this calls is because of this
http://msdn.microsoft.com/en-us/library/ms806162.aspx
我不确定这是否可能。 作为替代方案,您可以编写一个 C/C++ 程序,为您完成所有内核空间工作,并通过 子进程模块 或 Python C/ C++ 绑定(以及另一个链接)。
I'm not sure if that's possible. As an alternative, you could write a C/C++ program that does all that kernel space work for you and interface with it in Python via the subprocess module or Python C/C++ bindings (and another link for that).
在我看来,您好像在问为什么 os.open 并不神奇地等于使用一组非常具体的参数调用 CreateFile。 Kostya 的答案很实用,因为它告诉您可以使用 Win32 python 绑定直接调用 CreateFile(这是一个 Win32 API)。
除了直接执行 CreateFile/readFile/writeFile IO 之外的任何操作都会在顶部引入另一层(python 文件对象及其行为),该层将您限制为 os.open 支持的参数。 os.open 创建一个 python 文件对象,这并不完全相同,并且并不打算提供 Win32 CreateFile 的所有选项。
这意味着,例如,不能保证 GENERIC_READ、OPEN_EXISTING 或 FILE_ATTRIBUTE_NORMAL 的精确模拟存在。
我最好的猜测是 os.open 并不打算取代对 CreateFile 的直接调用,因为您使用它的目的很奇怪。
如果你能读懂 C,为什么不打开 python 源代码并阅读 os.open 的实现。 如果您确实必须通过 os.open,您将找出要传递给它的参数,以便最终 os.open 的实现(用 C 语言)使用上面的正确参数调用 Win32 API 中的 CreateFile。 所有这些看起来更像是工作,而不仅仅是使用科斯特亚的建议。
It sounds to me like you're asking why os.open is not magically equal to calling CreateFile with a very specific set of parameters. Kostya's answer is practical in that it tells you that you can use the Win32 python bindings to call CreateFile which is a Win32 API, directly.
Anything other than doing direct CreateFile/readFile/writeFile IO is going to introduce another layer on top (the python file objects and their behaviours) that restricts you to the parameters that os.open supports. os.open creates a python file object, which is not exactly the same thing, and not intended to provide all of Win32 CreateFile's options.
That means, for example, that no exact analog of GENERIC_READ, or OPEN_EXISTING, or FILE_ATTRIBUTE_NORMAL are guaranteed to exist.
My best guess is that os.open is not intended to replace direct calls to CreateFile, for such odd purposes as the one you're using it for.
If you can read C, why not open up the sources for python and read the implementation of os.open. If you really must go through os.open, you're going to find out what parameters to pass to it, so that in the end, os.open's implementation (in C) calls CreateFile in Win32 API with the correct parameters above. All of that seems more like work, than just using Kostya's suggestion.
有两种方法可以做到这一点。
第一种方法是使用 win32 python 绑定
或使用 ctypes
There are 2 ways to do this.
The first way is using the win32 python bindings
Or using ctypes