Chinaunix首页 | 论坛 | 博客
  • 博客访问: 336692
  • 博文数量: 64
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 589
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-08 15:50
文章分类

全部博文(64)

文章存档

2015年(52)

2014年(3)

2013年(9)

我的朋友

分类: WINDOWS

2015-07-23 18:27:24

1. 去微软网站下载WinDDK,现改名叫WDK,最新版本是GRMWDK_EN_7600_1.ISO,安装它。因为必须的文件hidsdi.h,hidpi.h,hidusage.h,hid.lib, hidclass.lib, hidparse.lib在VC中没自带。 style="color:#333333;font-family:Arial;font-size:14px;line-height:26px;white-space:normal;background-color:#FFFFFF;" /> 2. 将WDK安装目录加入到VC设定中,加入到工程设定中是没用的
   menu:Tools/Options/Projects and Solutions/VC++ Directories,右边Show directories for中选择Include files,将两个目录添加进去,顺序如下:
   C:/WinDDK/7600.16385.1/inc/ddk
   $(VCInstallDir)include
   $(VCInstallDir)atlmfc/include
   C:/WinDDK/7600.16385.1/inc/api
   $(WindowsSdkDir)/include
   $(FrameworkSDKDir)include
   即保证 C:/WinDDK/7600.16385.1/inc/api 在 $(WindowsSdkDir)/include 前面,这样#include <windows.h>等才用的是WDK中的文件,否则不匹配导致编译失败。
   menu:Tools/Options/Projects and Solutions/VC++ Directories,右边Show directories for中选择Library files,添加 C:/WinDDK/7600.16385.1/lib/wxp/i386

    代码:
#include <windows.h>
#include <dbt.h>
#include <stdio.h>
#include <math.h>
#if 1
//#include <winioctl.h>
#include "setupapi.h"
#include "cfgmgr32.h" //此头文件需要安装wdk,然后配置vs的wdk 环境
//#include "usbiodef.h"  
 #pragma comment(lib,"setupapi.lib")  
extern "C" {
#include "hidsdi.h"//note: part of Windows DDK

#pragma comment(lib,"hid.lib")  

DEVINST GetDrivesDevInstByDiskNumber(long DiskNumber) {


GUID* guid = (GUID*)(void*)&GUID_DEVINTERFACE_DISK;


// Get device interface info set handle for all devices attached to system
HDEVINFO hDevInfo = SetupDiGetClassDevs(guid, NULL, NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);


if (hDevInfo == INVALID_HANDLE_VALUE) {
return 0;
}

// Retrieve a context structure for a device interface of a device
// information set.
DWORD dwIndex = 0;
SP_DEVICE_INTERFACE_DATA devInterfaceData = {0};
devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
BOOL bRet = FALSE;


PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd;
SP_DEVICE_INTERFACE_DATA spdid;
SP_DEVINFO_DATA spdd;
DWORD dwSize;


spdid.cbSize = sizeof(spdid);


while ( true ) {
bRet = SetupDiEnumDeviceInterfaces(hDevInfo, NULL, guid, dwIndex,
&devInterfaceData);
if (!bRet) {
break;
}


SetupDiEnumInterfaceDevice(hDevInfo, NULL, guid, dwIndex, &spdid);


dwSize = 0;
SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, NULL, 0, &dwSize,NULL);


if ( dwSize ) {
pspdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, dwSize);
if ( pspdidd == NULL ) {
continue; // autsch
}
pspdidd->cbSize = sizeof(*pspdidd);
ZeroMemory((PVOID)&spdd, sizeof(spdd));
spdd.cbSize = sizeof(spdd);


long res = SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid,pspdidd, dwSize, &dwSize, &spdd);
if ( res ) {
HANDLE hDrive = CreateFile(pspdidd->DevicePath, 0,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
if ( hDrive != INVALID_HANDLE_VALUE ) {
STORAGE_DEVICE_NUMBER sdn;
DWORD dwBytesReturned = 0;
res = DeviceIoControl(hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER,NULL, 0, &sdn, sizeof(sdn), &dwBytesReturned, NULL);
if ( res ) {
if ( DiskNumber == (long)sdn.DeviceNumber ) {
CloseHandle(hDrive);
SetupDiDestroyDeviceInfoList(hDevInfo);
return spdd.DevInst;
}
}
CloseHandle(hDrive);
}
}
HeapFree(GetProcessHeap(), 0, pspdidd);
}
dwIndex++;
}


SetupDiDestroyDeviceInfoList(hDevInfo);


return 0;
}
static int UninstallUsb(char *discId)
{
DWORD accessMode = GENERIC_WRITE | GENERIC_READ;
DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
HANDLE hDevice;
long bResult = 0;
DWORD retu = 0;
DWORD dwError;
DWORD dwBytesReturned;
DWORD dwSleepAmount;
int nTryCount;
char szDriv[10];


if(discId == NULL){
return 0;
}
dwSleepAmount = LOCK_TIMEOUT/LOCK_RETRIES;
sprintf(szDriv,"\\\\.\\%s:",discId);
hDevice = CreateFile(szDriv,accessMode,shareMode,NULL,OPEN_EXISTING,0,NULL);
if(hDevice == INVALID_HANDLE_VALUE){
printf("uninstallusb createfile failed error:%d\n",GetLastError());
return -1;
}



//使用CM_Request_Device_Eject弹出USB设备

STORAGE_DEVICE_NUMBER sdn;
long DiskNumber = -1;
long res = DeviceIoControl(hDevice,IOCTL_STORAGE_GET_DEVICE_NUMBER,NULL,0,&sdn,sizeof(sdn),&dwBytesReturned,NULL);
if(!res){
printf("DeviceIoControl IOCTL_STORAGE_GET_DEVICE_NUMBER failed:%d\n",GetLastError());
CloseHandle(hDevice);
return -1;
}
CloseHandle(hDevice);
DiskNumber = sdn.DeviceNumber;
if(DiskNumber == -1){
printf("DiskNumber == -1\n");
return -1;
}
DEVINST DevInst = GetDrivesDevInstByDiskNumber(DiskNumber);
if(DevInst == 0){
printf("GetDrivesDevInstDiskNumber failed\n");
return -1;
}
ULONG Status = 0;
ULONG ProblemNumber = 0;
PNP_VETO_TYPE VetoType = PNP_VetoTypeUnknown;
char VetoName[MAX_PATH];
bool bSuccess = false;

res = CM_Get_Parent(&DevInst,DevInst,0); //disk's parent, e.g. the USB bridge, the SATA controller....
res = CM_Get_DevNode_Status(&Status,&ProblemNumber,DevInst,0);
bool IsRemovable = ((Status & DN_REMOVABLE) != 0);

printf("isremovable:%d\n",IsRemovable);
long i;
for(i = 0;i < 3;i++){
VetoName[0] = '\0';
if(IsRemovable){
res = CM_Request_Device_Eject(DevInst,&VetoType,VetoName,MAX_PATH,0);
}else{
res = CM_Query_And_Remove_SubTree(DevInst,&VetoType,VetoName,MAX_PATH,0);
}
bSuccess = (res == CR_SUCCESS && VetoName[0] == '\0');
if(bSuccess){
break;
}else{
Sleep(200);
}
}
if(bSuccess){
printf("Success\n\n");
}else{
printf("failed\n");

}
return 0;
}

int main()
{
    char DiscId = "H";        //要弹出的U盘盘符
    UninstallUsb(DiscId);
    return 0;
}
阅读(4024) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~