在程序中移除U盘

说明:

cr = CM_Request_Device_Eject( DeviceInfoData.DevInst, &pnpvietotype, vetoname, len, 0 ); //没有气泡提示
cr = CM_Request_Device_Eject( DeviceInfoData.DevInst, &pnpvietotype, NULL, 0, 0 ); //这样就有气泡提示了。

对于CM_Request_Device_Eject这类在DDK中的函数,可以有如下几种方法
1.把DDK的头文件路径和库包含进来,但头文件包含必须使用
extern "C"
{
#include "hidsdi.h"
}
2. 把DDK里的相关文件拷到工程里来
3. 用GetProcAddress从相关DLL中取得函数来执行。


这种办法只能移除所有的U盘,没法移除某一盘符对应的U盘

#include
#include
#include

#define DWORD_PTR DWORD
#define ULONG_PTR DWORD


extern "C" {
#include "hidsdi.h"
}
// 需加入hid.lib

#include
#include
#pragma comment(lib,"setupapi.lib")

#include
#include

#include
#pragma comment(lib,"cfgmgr32.lib")

#include
//#include
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;
}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值