C# 调用Device驱动

来源:互联网 发布:九次方大数据怎么样 编辑:程序博客网 时间:2024/04/28 02:48
 using System; 
using
System.IO; 
using
System.ComponentModel; 
using
System.Runtime.InteropServices; 
 
class Program{ 
   
static voidMain(string[] args){ 
       
IntPtr hdl =CreateFile("\\\\.\\ADVSYS",FileAccess.ReadWrite,FileShare.None,IntPtr.Zero,FileMode.Open,FileOptions.None,IntPtr.Zero); 
       
if (hdl== (IntPtr)(-1))throw newWin32Exception(); 
       
try { 
           
byte drawer = 1; 
            bool ok
= DeviceIoControl(hdl, CTLCODE,ref drawer,1, IntPtr.Zero,0, IntPtr.Zero,IntPtr.Zero); 
           
if (!ok)throw newWin32Exception(); 
       
} 
       
finally { 
           
CloseHandle(hdl); 
       
} 
   
} 
   
// P/Invoke: 
   
private constuint CTLCODE =0xdaf52480; 
   
[DllImport("kernel32.dll",CharSet =CharSet.Auto,SetLastError =true)] 
   
private staticextern IntPtrCreateFile(string filename,FileAccess access,FileShare sharing,  
         
IntPtr SecurityAttributes,FileMode mode,FileOptions options,IntPtr template 
   
); 
   
[DllImport("kernel32.dll",SetLastError =true)] 
   
private staticextern bool DeviceIoControl(IntPtr device,uint ctlcode, 
       
ref byte inbuffer,int inbuffersize, 
       
IntPtr outbuffer,int outbufferSize, 
       
IntPtr bytesreturned,IntPtr overlapped 
   
); 
   
[DllImport("kerne32.dll")] 
   
private staticextern voidCloseHandle(IntPtr hdl); 
}