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

解决方案 »

  1.   


    [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
    );
      

  2.   

    但是比如说dwDesiredAccess的值我在c++里可以用GENERIC_READ | GENERIC_WRITE,但是c#这么写,编译不过。不知道还要引用什么
      

  3.   

    public const int GENERIC_ALL = 0x10000000;
    public const int GENERIC_EXECUTE = 0x20000000;
    public const int GENERIC_READ = 0x80000000;
    public const int GENERIC_WRITE = 0x40000000;要声明出来。
      

  4.   

    去Pinvoke网站上查,需要的常量大多都用枚举体定义了, 枚举体相比常量定义的好处在于可以用智能提示
      

  5.   

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

  6.   


    瞎说,误导楼主。C#调用windows api,DllImport "kernel32.dll" 直接可以访问设备!