|  | 
| <br />原帖由 <i>qdaijchf</i> 于 2010-5-28 08:23 发表 <a href="http://bbs.wuyou.net/redirect.php?goto=findpost&pid=1959777&ptid=164013" target="_blank"><img src="http://bbs.wuyou.net/images/common/back.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" onmousewheel="return imgzoom(this);" alt="" /></a><br />在txtsetup.sif中加了两行,基本上搞定,比进入桌面后加载dummy.sys前进了一步,因为不用重启shell,但还是要插拔一次U盘。<br />
 有没有办法不用插拔U盘,完成一次相当于插拔的动作,即弹出然后加载U盘。
 在程序中加入以下代码,调用HT_Replug即可(参数为USB设备的ID)
 
 
 #define DWORD_PTR DWORD
 #define ULONG_PTR ULONG
 #include <cfgmgr32.h>
 #pragma comment(lib,"cfgmgr32.lib")
 
 BOOL FindDevNode(DEVINST dnHere, PDEVINST dnResult, PCHAR partialName)
 {
 if(0 == dnHere)
 return FALSE;
 
 DEVINST dnChild = 0;
 DEVINST dnSibling = 0;
 char deviceid[256];
 
 CONFIGRET cr=CM_Get_Device_ID(dnHere, deviceid, sizeof(deviceid), 0);
 if((CR_SUCCESS == cr) && (0 == strnicmp(deviceid, partialName, strlen(partialName))))
 {
 *dnResult = dnHere;
 return TRUE;
 }
 
 if(CR_SUCCESS == (cr = CM_Get_Child(&dnChild, dnHere, 0)))
 if(FindDevNode(dnChild, dnResult, partialName))
 return TRUE;
 
 if(CR_SUCCESS == (cr = CM_Get_Sibling(&dnSibling, dnHere, 0)))
 if(FindDevNode(dnSibling, dnResult, partialName))
 return TRUE;
 
 return FALSE;
 }
 
 BOOLEAN RestartDevice(
 IN HDEVINFO DeviceInfoSet,
 IN OUT PSP_DEVINFO_DATA DeviceInfoData
 )
 {
 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_STOP;
 params.Scope       = DICS_FLAG_CONFIGSPECIFIC;
 params.HwProfile   = 0; // current profile
 
 char szBuf[512];
 // prepare for the call to SetupDiCallClassInstaller (to stop the device)
 if( !SetupDiSetClassInstallParams( DeviceInfoSet,
 DeviceInfoData,
 (PSP_CLASSINSTALL_HEADER) ¶ms,
 sizeof(SP_PROPCHANGE_PARAMS)
 ) )
 {
 sprintf(szBuf,"in RestartDevice(): couldn't set the install parameters!\n"\
 "Error: %u\n",GetLastError());
 MessageBox(NULL,szBuf,"UMSAPI2K",MB_OK);
 return (FALSE);
 }
 
 // stop the device
 if( !SetupDiCallClassInstaller( DIF_PROPERTYCHANGE,
 DeviceInfoSet,
 DeviceInfoData )
 )
 {
 sprintf(szBuf,"in RestartDevice(): call to class installer (STOP) failed!\n"\
 "Error: %u\n",GetLastError());
 MessageBox(NULL,szBuf,"UMSAPI2K",MB_OK);
 return (FALSE);
 }
 
 // restarting the device
 params.StateChange = DICS_START;
 
 // prepare for the call to SetupDiCallClassInstaller (to stop the device)
 if( !SetupDiSetClassInstallParams( DeviceInfoSet,
 DeviceInfoData,
 (PSP_CLASSINSTALL_HEADER) ¶ms,
 sizeof(SP_PROPCHANGE_PARAMS)
 ) )
 {
 sprintf(szBuf,"in RestartDevice(): couldn't set the install parameters!\n"\
 "Error: %u\n",GetLastError());
 MessageBox(NULL,szBuf,"UMSAPI2K",MB_OK);
 return (FALSE);
 }
 
 // restart the device
 if( !SetupDiCallClassInstaller( DIF_PROPERTYCHANGE,
 DeviceInfoSet,
 DeviceInfoData )
 )
 {
 sprintf(szBuf,"in RestartDevice(): call to class installer (START) failed!\n"\
 "Error: %u\n",GetLastError());
 MessageBox(NULL,szBuf,"UMSAPI2K",MB_OK);
 return (FALSE);
 }
 
 installParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
 
 // same as above, the call will succeed, but we still need to check status
 if( !SetupDiGetDeviceInstallParams( DeviceInfoSet,
 DeviceInfoData,
 &installParams )
 )
 {
 sprintf(szBuf,"in RestartDevice(): couldn't get the device install params!\n"\
 "Error: %u\n",GetLastError());
 MessageBox(NULL,szBuf,"UMSAPI2K",MB_OK);
 return (FALSE);
 }
 
 // to see if the machine needs to be rebooted
 if( installParams.Flags & DI_NEEDREBOOT )
 {
 return (FALSE);
 }
 
 // if we get this far, then the device has been stopped and restarted
 return (TRUE);
 }
 
 // BOOL stopDevice(char *szVen,char *szProd)
 // {
 //    char DEVICEID[64];
 //    DEVINST dn = NULL;
 //    sprintf(DEVICEID,"USB\\Vid_%s&Pid_%s",szVen,szProd);
 //
 //    if(CR_SUCCESS!=CM_Locate_DevNode(&dn,0,0))
 //    {
 //       MessageBox(NULL,"!CM_LocateDevNode fail!","UMSAPI2K",MB_OK);
 //       return FALSE;
 //    }
 //
 //    PNP_VETO_TYPE  type;
 //    if(FindDevNode(dn, &dn, (PCHAR)DEVICEID))
 //    {
 //       char szVetoName[_MAX_PATH];
 //       if(CR_SUCCESS!=CM_Request_Device_Eject(dn,&type,szVetoName,_MAX_PATH,0))
 //       {
 //          //AfxMessageBox("Remove fail!");
 //          return FALSE;
 //       }
 //    }
 //    return TRUE;
 // }
 
 BOOL HT_Replug(HWND hwnd,char *szVen,char *szProd)
 {
 char DEVICEID[64];
 sprintf(DEVICEID,"USB\\Vid_%s&Pid_%s",szVen,szProd);
 
 GUID guid;
 DWORD dw;
 
 SetupDiClassGuidsFromName("HIDClass",&guid,1,&dw);
 HDEVINFO hInfo=SetupDiGetClassDevs(&guid,DEVICEID,hwnd,DIGCF_PRESENT);
 if(hInfo==INVALID_HANDLE_VALUE) return FALSE;
 
 BOOL bRet=TRUE;
 SP_DEVINFO_DATA devInfoData;
 devInfoData.cbSize=sizeof(SP_DEVINFO_DATA);
 for(int deviceIndex=0;
 SetupDiEnumDeviceInfo( hInfo, deviceIndex, &devInfoData );
 deviceIndex++)
 {
 if(!RestartDevice(hInfo,&devInfoData)) bRet=FALSE;
 }
 SetupDiDestroyDeviceInfoList( hInfo );
 return bRet;
 }
 | 
 |