阅读:2207回复:5
软件实现U盘的插拔整个动作
在用一款软件时,可以对U盘进行重置。效果就像先拔除U盘后再插上。不知道这个是怎么实现的,请问各位大牛们。如果单独的删除USB设备,这个是可以的,但是怎么让程序实现自动再插上U盘呢?
|
|
沙发#
发布于:2007-01-17 10:00
怎么没有人回答阿?
|
|
板凳#
发布于:2007-01-17 10:31
见 武安河 书
|
|
地板#
发布于:2007-01-18 07:24
ddk\src\setup\devcon
search for 'remove', 'install', 'disable', 'enable' |
|
|
地下室#
发布于:2007-01-18 12:12
引用第3楼rayyang2000于2007-01-18 07:24发表的“”: SetupDixxx |
|
|
5楼#
发布于:2007-06-26 16:42
#include "StdAfx.h"
#include "SetupDIMgr.h" #include "Setupapi.h" #include "afxwin.h" #pragma comment(lib,"Setupapi.lib") HDEVINFO m_devInfo; SP_DEVINFO_DATA m_devInfoData; int m_nDevIndex; //define the GUID for your device class GUID hidGuid = {0x4D36E96D, 0xE325, 0x11CE, {0xBF,0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18}}; BOOL GetRegProperty(CString &sProperty, DWORD dwWhich) { DWORD regDataType = 0; LPTSTR deviceName = (LPTSTR) GetDeviceRegistryProperty( m_devInfo, &m_devInfoData, dwWhich, ®DataType ); if(NULL != deviceName) { // just to make sure we are getting the expected type of buffer if(REG_SZ != regDataType) { if(REG_MULTI_SZ == regDataType) { LPTSTR sScanner = deviceName; sProperty.Empty(); while(0 != _tcslen(sScanner)) { sProperty += sScanner; sScanner += _tcslen(sScanner) + sizeof(TCHAR); if(0 != _tcslen(sScanner)) { sProperty += ","; } } free(deviceName); return TRUE; } TRACE("registry key is not an SZ or MULTI_SZ!\n"); free(deviceName); return FALSE; } // if the device name starts with \Device, cut that off (all // devices will start with it, so it is redundant) if(_tcsncmp(deviceName, _T("\\Device"), 7) == 0) { memmove(deviceName, deviceName+7, (_tcslen(deviceName)-6)*sizeof(_TCHAR)); } sProperty = deviceName; free(deviceName); return TRUE; } return FALSE; } BOOL Enum(void) { if(INVALID_HANDLE_VALUE == m_devInfo) { return FALSE; } // as per DDK docs on SetupDiEnumDeviceInfo m_devInfoData.cbSize = sizeof(m_devInfoData); // step through the list of devices for this handle // get device info at index deviceIndex, the function returns FALSE // when there is no device at the given index. if(!SetupDiEnumDeviceInfo(m_devInfo, m_nDevIndex, &m_devInfoData)) { //TRACE("Done enumerating...\n"); return FALSE; } m_nDevIndex++; return TRUE; } BOOL RestartDevice(void) { CString sHardwareID; // get a list of devices which support the given interface TCHAR sEnumerator[] = _T("USB"); m_devInfo = SetupDiGetClassDevs(NULL, sEnumerator, 0, DIGCF_ALLCLASSES | (bPresentOnly ? DIGCF_PRESENT : 0)); if(m_devInfo == INVALID_HANDLE_VALUE ) { TRACE("got INVALID_HANDLE_VALUE!\n"); } m_nDevIndex = 0; //get m_devInfoData,updata m_nDevIndex Enum(); if( GetRegProperty(sHardwareID, SPDRP_HARDWAREID)) { do { SP_PROPCHANGE_PARAMS params; // SP_DEVINSTALL_PARAMS installParams; // for future compatibility; this will zero out the entire struct, rather // than just the fields which exist now memset(¶ms, 0, sizeof(SP_PROPCHANGE_PARAMS)); // initialize the SP_CLASSINSTALL_HEADER struct at the beginning of the // SP_PROPCHANGE_PARAMS struct, so that SetupDiSetClassInstallParams will // work params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER); params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; // initialize SP_PROPCHANGE_PARAMS such that the device will be stopped. params.StateChange = DICS_PROPCHANGE; params.Scope = DICS_FLAG_CONFIGSPECIFIC; params.HwProfile = 0; // current profile // prepare for the call to SetupDiCallClassInstaller (to stop the device) if(!SetupDiSetClassInstallParams(m_devInfo, &m_devInfoData, (PSP_CLASSINSTALL_HEADER) ¶ms, sizeof(SP_PROPCHANGE_PARAMS)) ) { TRACE("in RestartDevice(): couldn't set the install parameters!"); TRACE(" error: %u\n", GetLastError()); return FALSE; } // stop the device if(!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, m_devInfo, &m_devInfoData)) { TRACE("in RestartDevice(): call to class installer (STOP) failed!"); TRACE(" error: %u\n", GetLastError() ); return FALSE; } } return TRUE; } return FALSE; } |
|