本人需要做一个程序,实现删除U盘这样的效果,哪位做过此功能的高手介绍下经验?U盘检测我已经实现了,就剩删除U盘功能了,想了很久都做不出来
解决方案 »
- CTabView中标签的切换按钮无法响应的问题。求高手
- error MSB6006: “rc.exe”已退出,代码为 1
- 文件打开的问题???为什么得不到文件???
- 我用vfw编程获取摄像头信息,可是获得的图象分辨率总是320×240,现在想得到640×480或者更高的,该怎么编程实现,谢谢啦。
- 我是初学者,请问改哪里的initinstance?具体代码有例子么?谢谢
- 程序访问别的机器上的数据库出错!!
- 高分相送!如何将打开的位图翻转???
- ATL工程选择合并PS,合并之后的stub功能在什么文件里面实现呢?
- 怎样用指针或引用传递一个float型的2维数组?
- 我想将我的程序嵌入到WORD中,请指点一二。
- 静态CString赋值的问题
- 批处理判断复制是否成功
include <tchar.h>
#include <stdio.h>
#include <windows.h>
#include <devguid.h>
#define DWORD_PTR DWORD
#define ULONG_PTR DWORD
extern "C" {
#include "hidsdi.h"
}
// 需加入hid.lib
#include <setupapi.h>
// 需加入setupapi.lib
#include <regstr.h>
#include <winbase.h>
#include <cfgmgr32.h>
// 需要加入cfgmgr32.lib
#include <initguid.h>
//#include <usbiodef.h>
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,
0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
#define GUID_CLASS_USB_DEVICE GUID_DEVINTERFACE_USB_DEVICE
int main(int argc, _TCHAR* argv[])
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
//--------------------------------------------------------------------------
// 获取设备信息
hDevInfo = SetupDiGetClassDevs((LPGUID)&GUID_CLASS_USB_DEVICE,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE );
if (hDevInfo == INVALID_HANDLE_VALUE) {
// 查询信息失败
printf("ERROR - SetupDiGetClassDevs()");
return 1;
}
//--------------------------------------------------------------------------
// 枚举每个USB设备
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData);i++)
{
LPTSTR buffer = NULL;
PVOID buffer2 = NULL;
DWORD buffersize = 0;
ULONG len;
CONFIGRET cr;
PNP_VETO_TYPE pnpvietotype;
CHAR vetoname[MAX_PATH];
ULONG ulStatus;
ULONG ulProblemNumber;
cr = CM_Get_DevNode_Status( &ulStatus,
&ulProblemNumber,
DeviceInfoData.DevInst,
0);
if ( CR_SUCCESS == cr ) {
printf("OK - CM_Get_DevNode_Status()[%d]\n", cr);
printf("OK - CM_Get_DevNode_Status() sts [%x]\n", ulStatus);
printf("OK - CM_Get_DevNode_Status() pro [%x]\n", ulProblemNumber);
} else {
printf("ERROR - CM_Get_DevNode_Status()[%d]\n", cr);
printf("ERROR - CM_Get_DevNode_Status()[%d]\n", GetLastError());
}
// DN_DISABLEABLE or DN_REMOVABLE
if ((DN_DISABLEABLE & ulStatus ) != 0 ) {
printf("HAS - DN_DISABLEABLE()[%x]\n", DN_DISABLEABLE & ulStatus);
} else {
continue;
}
if ((DN_REMOVABLE & ulStatus ) != 0 ) {
printf("HAS - DN_REMOVABLE()[%x]\n", DN_REMOVABLE & ulStatus);
} else {
continue;
}
len = MAX_PATH;
// pnpvietotype = PNP_VetoDevice;
cr = CM_Request_Device_Eject(
DeviceInfoData.DevInst,
&pnpvietotype,
vetoname,
len,
0
);
if ( CR_SUCCESS == cr ) {
printf("OK - CM_Request_Device_Eject()[%d]\n", cr);
} else {
printf("ERROR - CM_Request_Device_Eject()[%d]\n", cr);
printf("ERROR - CM_Request_Device_Eject()[%d]\n", GetLastError());
}
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
1. CM_Request_Device_EjectA
http://www.withopf.com/tools/deveject/deveject-src-11.zip
2. DeviceIoControl calls with IOCTL_STORAGE_EJECT_MEDIA, FSCTL_DISMOUNT_VOLUME, etc
http://msdn.microsoft.com/en-us/library/aa940953%28WinEmbedded.5%29.aspx
3. CM_Query_And_Remove_SubTreeW
http://www.codeproject.com/KB/system/RemoveDriveByLetter.aspx