简体   繁体   English

Windows 设备文件不使用 python win32API 返回有效句柄

[英]Windows device file does not return valid handle with python win32API

I am trying to open a device file in windows using python.我正在尝试使用 python 在 windows 中打开一个设备文件。 I heard I needed to use win32 API.听说需要用win32 API。 So I am using that, to open my file I need to perform the following this stackoverflow question: Opening a handle to a device in Python on Windows所以我使用它,要打开我的文件,我需要执行以下这个stackoverflow问题: Open a handle to a device in Python on Windows

import win32file as w32
self.receiver_handle = w32.CreateFile("\\\\.\\xillybus_read_32",  # file to open

w32.GENERIC_READ,  # desired access
w32.FILE_ATTRIBUTE_READONLY,  # shared mode
None,  # security attribute
w32.OPEN_EXISTING,  # creation distribution
w32.FILE_ATTRIBUTE_READONLY,  #flags and attributes
None)  # no template file

This results in the handle always returning 0. Here is the API reference: http://winapi.freetechsecrets.com/win32/WIN32CreateFile.htm这导致句柄始终返回 0。这是 API 参考: http://winapi.freetechsecrets.com/win32/WIN32CreateFile.htm

The driver came with a barebones C program to test it and it works flawlessly, so it can't be the driver itself that is not properly working.该驱动程序附带了一个准系统 C 程序来测试它,它可以完美地工作,所以它不能是驱动程序本身不能正常工作。

What am I doing wrong?我究竟做错了什么?

The API should not return zero. API 不应返回零。 It should return a PyHANDLE object.它应该返回一个PyHANDLE object。 I don't have your device, but opening an existing file works.我没有您的设备,但可以打开现有文件。 The 3rd parameter should be w32.FILE_SHARE_READ (or similar share mode value), however:第三个参数应该是w32.FILE_SHARE_READ (或类似的共享模式值),但是:

>>> import win32file as w32
>>> w32.CreateFile('blah.txt',w32.GENERIC_READ,w32.FILE_SHARE_READ,None,w32.OPEN_EXISTING,w32.FILE_ATTRIBUTE_READONLY,None)
<PyHANDLE:280>

If the file does not exist (or any other error), Python should raise an exception based on the GetLastError() code returned by the Win32 API called:如果文件不存在(或任何其他错误),Python 应根据 Win32 API 返回的GetLastError()代码引发异常,称为:

>>> w32.CreateFile('blah.txt',w32.GENERIC_READ,w32.FILE_SHARE_READ,None,w32.OPEN_EXISTING,w32.FILE_ATTRIBUTE_READONLY,None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pywintypes.error: (2, 'CreateFile', 'The system cannot find the file specified.')

If this doesn't help, edit your question to show the exact code you are running and the exact results of running the code.如果这没有帮助,请编辑您的问题以显示您正在运行的确切代码以及运行代码的确切结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM