delphi 读写U盘(100分)

  • 主题发起人 主题发起人 changku
  • 开始时间 开始时间
C

changku

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