我以前收集的资料,不知道对你是否有用。我还没有用过。你搞定后可以给我发一份就好。yanghai0437@21cn.com;<br><br>#include <windows.h><br>#include "serial.h"<br>#include "key.h"<br><br>#define BYTE_SIZE 8 // Use 8 bit bytes for serial communications<br><br>static HANDLE hCom; // File handle for opened COM port connected to ioGizmo<br><br>int serialOpen(const char * comPort, int baudRate) {<br> <br> DCB m_dcb; // structure stores COM Port Configuration<br> COMMTIMEOUTS timeouts; // structure stores timeout Configuration<br> int success = 0; // function return value indicates success <br> // of initializing port<br> <br> //create Win32 File Handle for COM Port<br> hCom = CreateFile(comPort, // Port name<br> GENERIC_READ | GENERIC_WRITE, // read-write mode<br> 0, // exclusive access<br> NULL, // no security<br> OPEN_EXISTING, // for serial port<br> 0, // no overlapped I/O<br> NULL); // null template <br> <br> // get current configuration and store in DCB structure<br> if(GetCommState(hCom, &m_dcb)) {<br> <br> // ensure that communications parameters are correctly set<br> m_dcb.BaudRate = baudRate;<br> m_dcb.ByteSize = BYTE_SIZE;<br> m_dcb.Parity = NOPARITY;<br> m_dcb.StopBits = ONESTOPBIT;<br> m_dcb.fAbortOnError = TRUE;<br> <br> // set updated configuration<br> if (SetCommState(hCom, &m_dcb)) {<br><br> // get current timeout configuration and store in COMMTIMEOUTS structure<br> if (GetCommTimeouts(hCom, &timeouts)) {<br><br> // set the read timeouts so that a read of the receive buffer will<br> // return immediately with the bytes that have already been read<br> timeouts.ReadIntervalTimeout = MAXDWORD; <br> timeouts.ReadTotalTimeoutMultiplier = 0;<br> timeouts.ReadTotalTimeoutConstant = 0;<br><br> // set updated timeout configuration<br> success = SetCommTimeouts(hCom, &timeouts);<br> }<br> }<br> }<br><br> return success;<br>}<br><br>char serialGetC() {<br><br> char readChar;<br> int actBytesRead;<br><br> ReadFile(hCom, &readChar, 1, &actBytesRead, NULL);<br><br> if(actBytesRead == 0) // no character read<br> readChar = NO_CHAR;<br><br> return readChar;<br>}<br><br><br>void serialPutC(char c) {<br><br> int actBytesWrite;<br> <br> WriteFile(hCom, &c, 1, &actBytesWrite, NULL);<br>}<br><br><br>void serialClose() {<br> CloseHandle(hCom); // release serial port so other programs can use it<br>}<br><br>如何用程序来控制停止USB设备<br>如此简单的程序你要等一年还没解决???晕~!如下是你要实现的Delphi程序<br>代码,不知你看得懂不~?晕~靠~~~~~<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, ExtCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br> uses registry;<br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> reg:Tregistry;<br> state:integer;<br>begin<br>reg:=tregistry.create;<br>reg.rootkey:=HKEY_LOCAL_MACHINE;<br>reg.openkey('/SYSTEM/CurrentControlSet/Services/USBSTOR',true);<br>reg.Writeinteger('start',4);//禁用停止USB设备//<br>reg.free;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br> reg:Tregistry;<br> state:integer;<br>begin<br>reg:=tregistry.create;<br>reg.rootkey:=HKEY_LOCAL_MACHINE;<br>reg.openkey('/SYSTEM/CurrentControlSet/Services/USBSTOR',true);<br>reg.Writeinteger('start',3);//开启USB设备//<br>reg.free;<br>end;<br><br>end.<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, ExtCtrls;<br><br>type<br> TForm1 = class(TForm)<br> ComboBox1: TComboBox;<br> Button1: TButton;<br> Label1: TLabel;<br> Bevel1: TBevel;<br> procedure Button1Click(Sender: TObject);<br><br> procedure FormShow(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br><br>implementation<br> uses registry;<br>{$R *.dfm}<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> reg:Tregistry;<br> state:integer;<br>begin<br>if combobox1.text='只读' then<br>begin<br>reg:=tregistry.create;<br>reg.rootkey:=HKEY_LOCAL_MACHINE;<br>reg.openkey('/SYSTEM/CurrentControlSet/Services/USBSTOR',true);<br>reg.Writeinteger('start',2);<br>application.MessageBox('成功更改为只读.请从新播入U盘','提示',64);<br><br>reg.free;<br>end<br> else<br> begin<br> if combobox1.text='读写' then<br> begin<br>reg:=tregistry.create;<br>reg.rootkey:=HKEY_LOCAL_MACHINE;<br>reg.openkey('/SYSTEM/CurrentControlSet/Services/USBSTOR',true);<br>reg.Writeinteger('start',3);<br><br>application.MessageBox('成功更改为读写.请从新播入U盘','提示',64);<br><br>reg.free;<br> end<br> else<br> begin<br><br>reg:=tregistry.create;<br>reg.rootkey:=HKEY_LOCAL_MACHINE;<br>reg.openkey('/SYSTEM/CurrentControlSet/Services/USBSTOR',true);<br>reg.Writeinteger('start',4);<br><br>application.MessageBox('成功更改为禁用.请从新播入U盘','提示',64);<br><br>reg.free;<br> end<br><br><br><br>end;<br>end;<br>procedure TForm1.FormShow(Sender: TObject);<br>var<br> regSYS:Tregistry;<br> read:integer;<br>begin<br>combobox1.Items.add('只读');<br>combobox1.Items.add('读写');<br>combobox1.Items.add('禁用');<br>begin<br>regSYS:=tregistry.create;<br>regSYS.rootkey:=HKEY_LOCAL_MACHINE;<br>regSYS.openkey('/SYSTEM/CurrentControlSet/Services/USBSTOR',true);<br>read:=(regSYS.ReadInteger('start'));<br>if read=2 then<br>begin<br> combobox1.ItemIndex:=0;<br> end<br> else<br> if read=3 then<br> begin<br> combobox1.ItemIndex:=1;<br> end<br> else<br> if read=4 then<br> begin<br> combobox1.ItemIndex:=2;<br> end<br><br><br>end;<br>end;<br><br>end.<br>如何状这个做成服务 <br><br><br><br>2005-6-17 11:56:57 <br> 发表评语&raquo;&raquo;&raquo; <br><br> 2005-6-18 8:29:36 USB安全补充如何禁用usb设备<br>function TForm1.GetDevInfo(var hDevInfo: hDevInfo): boolean;<br>begin<br> // Get a handle to all devices in all classes present on system<br> hDevInfo := SetupDiGetClassDevs(nil, nil, 0, DIGCF_PRESENT or DIGCF_ALLCLASSES);<br> Result := hDevInfo <> Pointer(INVALID_HANDLE_VALUE);<br>end;<br><br>调用为:<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> if (not LoadSetupAPI) then<br> begin<br> ShowMessage('Could not load SetupAPI.dll');<br> exit;<br> end;<br> DevInfo := nil;<br> ShowHidden := false;<br> // Get a handle to all devices in all classes present on system<br> if not GetDevInfo(DevInfo) then<br> begin<br> ShowMessage('GetClassDevs');<br> exit;<br> end;<br> // Get the Images for all classes, and bind to the TreeView<br> ClassImageListData.cbSize := SizeOf(TSPClassImageListData);<br> if (not SetupDiGetClassImageList(ClassImageListData)) then<br> begin<br> ShowMessage('GetClassImageList');<br> exit;<br> end;<br> ImageList.Handle := ClassImageListData.ImageList;<br> TreeView.Images := ImageList;<br> // Add the devices to the TreeView window.<br> EnumAddDevices(ShowHidden, TreeView, DevInfo);<br>end;<br><br>procedure TForm1.mRefreshDisplayClick(Sender: TObject);<br>begin<br> if not GetDevInfo(DevInfo) then<br> begin<br> ShowMessage('GetClassDevs');<br> exit;<br> end;<br> EnumAddDevices(ShowHidden, TreeView, DevInfo);<br>end;<br><br> case CM_Request_Device_Eject(DeviceInfoData.DevInst, VetoType, @VetoName, SizeOf(VetoName), 0) of<br> CR_SUCCESS:<br> begin<br> MessageBox(Handle, 'Successful to eject the Device', 'Done', MB_OK);<br> if not GetDevInfo(DevInfo) then<br> begin<br> ShowMessage('GetClassDevs');<br> exit;<br> end;<br> EnumAddDevices(ShowHidden, TreeView, DevInfo);<br> end;<br><br>哈哈,这回就OK了 <br>我想在程序中加一个监测USB接口的功能,也就是能在USB口中当有插入或拔出设备时,获得一个相应的通知,以便做某些相应的提示。<br>我知道有一个 WM_DEVICECHANGE = 537的消息,当设备的硬件配置改变时发送此消息给应用程序或设备驱动程序,<br>type <br> TForm1 = class(TForm) <br> Button1: TButton; <br> private <br> { Private declarations } <br> procedure WMDeviceChange(var Message: TMessage); <br> message WM_DEVICECHANGE; <br> public <br> { Public declarations } <br>end; <br>var <br> Form1: TForm1; <br>implementation <br>{$R *.DFM} <br>const DBT_DEVICEARRIVAL = $8000; <br>const DBT_DEVICEQUERYREMOVE = $8001; <br>const DBT_DEVICEQUERYREMOVEFAILED = $8002; <br>const DBT_DEVICEREMOVEPENDING = $8003; <br>const DBT_DEVICEREMOVECOMPLETE = $8004; <br>const DBT_DEVICETYPESPECIFIC = $8005; <br>const DBT_CONFIGCHANGED = $0018; <br><br>procedure TForm1.WMDeviceChange(var Message: TMessage); <br>var <br> s : string; <br>begin <br> {Do Something here} <br> case Message.wParam of <br> DBT_DEVICEARRIVAL : <br> s := 'A device has been inserted and is now available'; <br> DBT_DEVICEQUERYREMOVE: <br> begin <br> s := 'Permission to remove a device is requested'; <br> ShowMessage(s); {True grants premission} <br> Message.Result := integer(true); <br> exit; <br> end; <br> DBT_DEVICEQUERYREMOVEFAILED : <br> s := 'Request to remove a device has been canceled'; <br> DBT_DEVICEREMOVEPENDING : <br> s := 'Device is about to be removed'; <br> DBT_DEVICEREMOVECOMPLETE : <br> s := 'Device has been removed'; <br> DBT_DEVICETYPESPECIFIC : <br> s := 'Device-specific event'; <br> DBT_CONFIGCHANGED : <br> s:= 'Current configuration has changed' <br> else s := 'Unknown Device Message'; <br> end; <br> ShowMessage(s); <br> inherited; <br>end;<br>我找到如上代码,在运行过程中确实能在拔插的过程中获得了通知,但现在的问题是,拔插获得的Message.wParam的值都是一样的,都等于7,因而没有办法判断到底哪一次获得该消息时是拔掉,还是插入了。有什么办法能让我准确的区分这两个不同的操作吗???拖也好些时候了,希望各位老兄的帮忙,不胜感激!!!!