線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:5971
推到 Plurk!
推到 Facebook!

如何偵測隨身碟對插拔動作,並取得隨身碟的磁碟代號?

答題得分者是:yckuo
haman
中階會員


發表:46
回覆:137
積分:56
註冊:2005-03-10

發送簡訊給我
#1 引用回覆 回覆 發表時間:2006-11-09 08:55:20 IP:210.85.xxx.xxx 訂閱
目前想作一個測試隨身碟的傳輸效率的程式,由於每次換一個隨身碟就要選一次目錄有點麻煩,
想請問一下,要如何偵測隨身碟已插入的這個動作呢?又若偵測到了,要怎麼取得磁碟機代號呢?
yckuo
高階會員


發表:55
回覆:389
積分:238
註冊:2003-03-07

發送簡訊給我
#2 引用回覆 回覆 發表時間:2006-11-09 12:17:33 IP:220.132.xxx.xxx 未訂閱
取得磁碟機代號
<textarea class="cpp" rows="10" cols="60" name="code"> void __fastcall TForm1::search_driver(void) { LPTSTR lpBuffer ; DWORD dwDeviceCnt; dwDeviceCnt = GetLogicalDriveStrings( 0, lpBuffer ); if(dwDeviceCnt) { char *buf = new char [dwDeviceCnt]; GetLogicalDriveStrings(dwDeviceCnt, buf ); std::vector vStrDevice; for(DWORD idx =0 ;idx < (dwDeviceCnt - 1) ; idx) if(buf[idx] ==0 ) buf[idx] =' '; AnsiString sDevice = buf; while(!sDevice.IsEmpty()) { vStrDevice.push_back(sDevice.SubString(1,sDevice.Pos(" ")-1)); sDevice.Delete(1,sDevice.Pos(" ")); } for(int idx=0; idx< (int)vStrDevice.size(); idx) { switch (GetDriveType( vStrDevice[idx].c_str() )) { case DRIVE_UNKNOWN : Memo1->Lines->Add(vStrDevice[idx] " DRIVE_UNKNOWN"); break; case DRIVE_NO_ROOT_DIR: Memo1->Lines->Add(vStrDevice[idx] " DRIVE_NO_ROOT_DIR"); break; case DRIVE_REMOVABLE : Memo1->Lines->Add(vStrDevice[idx] " DRIVE_REMOVABLE"); // 這個就是可移除的裝置 例如usb 的磁碟機 ,軟碟機等 break; case DRIVE_FIXED : Memo1->Lines->Add(vStrDevice[idx] " DRIVE_FIXED"); break; case DRIVE_REMOTE : Memo1->Lines->Add(vStrDevice[idx] " DRIVE_REMOTE"); break; case DRIVE_CDROM : Memo1->Lines->Add(vStrDevice[idx] " DRIVE_CDROM"); break; case DRIVE_RAMDISK : Memo1->Lines->Add(vStrDevice[idx] " DRIVE_RAMDISK"); break; } } delete buf; } } </textarea>
------
yckuo
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2006-11-09 14:20:26 IP:61.219.xxx.xxx 未訂閱
Win32 Application Support for Plug and Play
http://msdn2.microsoft.com/en-US/library/ms810038.aspx

關於USB裝置插入的問題
http://topic.csdn.net/t/20050305/21/3827513.html
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2006-11-10 13:28:52 IP:61.219.xxx.xxx 未訂閱
 

Detecting Media Insertion or Removal

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/detecting_media_insertion_or_removal.asp
Windows sends all top-level windows a set of default WM_DEVICECHANGE messages when new devices or media (such as a CD or DVD) are added and become available, and when existing devices or media are removed. You do not need to register to receive these default messages. See the Remarks section in RegisterDeviceNotification for details on which messages are sent by default. The messages in the code example below are among the default messages.
Each WM_DEVICECHANGE message has an associated event that describes the change, and a structure that provides detailed information about the change. The structure consists of an event-independent header, DEV_BROADCAST_HDR, followed by event-dependent members. The event-dependent members describe the device to which the event applies. To use this structure, applications must first determine the event type and the device type. Then, they can use the correct structure to take appropriate action.

When the user inserts a new CD or DVD into a drive, applications receive a WM_DEVICECHANGE message with a DBT_DEVICEARRIVAL event. The application must check the event to ensure that the type of device arriving is a volume (the dbch_devicetype member is DBT_DEVTYP_VOLUME) and that the change affects the media (the dbcv_flags member is DBTF_MEDIA).
When the user removes a CD or DVD from a drive, applications receive a WM_DEVICECHANGE message with a DBT_DEVICEREMOVECOMPLETE event. Again, the application must check the event to ensure that the device being removed is a volume and that the change affects the media.
The following code demonstrates how to check for insertion or removal of a CD or DVD.
<textarea class="cpp" rows="10" cols="60" name="code"> #include #include void Main_OnDeviceChange (HWND hwnd, WPARAM wParam, LPARAM lParam); char FirstDriveFromMask (ULONG unitmask); //prototype /*------------------------------------------------------------------ Main_OnDeviceChange (hwnd, wParam, lParam) Description Handles WM_DEVICECHANGE messages sent to the application's top-level window. --------------------------------------------------------------------*/ void Main_OnDeviceChange (HWND hwnd, WPARAM wParam, LPARAM lParam) { PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam; char szMsg[80]; switch(wParam) { case DBT_DEVICEARRIVAL: // Check whether a CD or DVD was inserted into a drive. if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME) { PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb; if (lpdbv -> dbcv_flags & DBTF_MEDIA) { wsprintf (szMsg, "Drive %c: Media has arrived.\n", FirstDriveFromMask(lpdbv ->dbcv_unitmask)); MessageBox (hwnd, szMsg, "WM_DEVICECHANGE", MB_OK); } } break; case DBT_DEVICEREMOVECOMPLETE: // Check whether a CD or DVD was removed from a drive. if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME) { PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb; if (lpdbv -> dbcv_flags & DBTF_MEDIA) { wsprintf (szMsg, "Drive %c: Media was removed.\n", FirstDriveFromMask(lpdbv ->dbcv_unitmask)); MessageBox (hwnd, szMsg, "WM_DEVICECHANGE", MB_OK); } } break; default: /* Process other WM_DEVICECHANGE notifications for other devices or reasons. */ ; } } /*------------------------------------------------------------------ FirstDriveFromMask (unitmask) Description Finds the first valid drive letter from a mask of drive letters. The mask must be in the format bit 0 = A, bit 1 = B, bit 3 = C, etc. A valid drive letter is defined when the corresponding bit is set to 1. Returns the first drive letter that was found. --------------------------------------------------------------------*/ char FirstDriveFromMask (ULONG unitmask) { char i; for (i = 0; i < 26; i) { if (unitmask & 0x1) break; unitmask = unitmask >> 1; } return (i 'A'); } </textarea>
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2008-03-10 15:31:16 IP:61.220.xxx.xxx 訂閱
參考看看

[所有][發表] 偵測USB儲存裝置的插拔和磁碟代號(含SourceCode)
http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=91493
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
系統時間:2024-04-26 10:22:08
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!