日期:2014-05-17  浏览次数:21633 次

c#如何使用CreateFile和DeviceIoControl
想先打开一个sys,然后与其通信。
用C++使用这2个函数。但是c#该如何使用呢?

------解决方案--------------------

[DllImport("kernel32.dll", EntryPoint="CreateFile")]
public static extern int CreateFile (
string lpFileName,
int dwDesiredAccess,
int dwShareMode,
ref SECURITY_ATTRIBUTES lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);
[DllImport("kernel32.dll", EntryPoint="DeviceIoControl")]
public static extern int DeviceIoControl (
int hDevice,
int dwIoControlCode,
ref int lpInBuffer,
int nInBufferSize,
ref int lpOutBuffer,
int nOutBufferSize,
ref int lpBytesReturned,
ref OVERLAPPED lpOverlapped
);

------解决方案--------------------
public const int GENERIC_ALL = 0x10000000;
public const int GENERIC_EXECUTE = 0x20000000;
public const int GENERIC_READ = 0x80000000;
public const int GENERIC_WRITE = 0x40000000;

要声明出来。
------解决方案--------------------
C#,可能还包括所有.net语言,都不能访问形如@"\\.\\"的设备文件,也就是说.net不能直接访问设备文件。我试过。你要想访问设备文件只能自己用C++之类的语言做个native DLL,然后.net通过这个DLL访问设备文件。