class="markdown_views prism-atom-one-light">
//枚举设备,储存起来
BOOL CMFCtestDlg::GetDeviceList(LPGUID lpGuid)
{
BOOL bFlag = TRUE;
do
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// 得到设备 HDEVINFO ,若是加上了| DIGCF_ALLCLASSES 则表示查询出所有设备的信息,lpGuid这个参数无效。
hDevInfo = SetupDiGetClassDevs(lpGuid, 0, 0, DIGCF_PRESENT /*| DIGCF_ALLCLASSES*/ );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf ("SetUpDi ERR!");
bFlag = FALSE;
break;
}
cout<<"hDevInfo:"<());
} while (FALSE);
return bFlag;
}
//设置设备状态(启用/停用),1为启用,0为停用
BOOL CMFCtestDlg::SetDeviceStatus(DeviceInfo &theDevice,BOOL bStatusFlag)
{
BOOL bFlag = TRUE;
do
{
SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
HDEVINFO hDevInfo;
// 得到设备 HDEVINFO
hDevInfo = SetupDiGetClassDevs(&theDevice.Guid, 0, 0, DIGCF_PRESENT /*| DIGCF_ALLCLASSES */);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf ("SetUpDi ERR!
");
bFlag = FALSE;
break;
}
//判断是否有这个设备
//bFlag = FALSE;
int index = 0;
//枚举所得的信息
while (SetupDiEnumDeviceInfo(hDevInfo, index ++, &DeviceInfoData))
{
if (DeviceInfoData.DevInst == theDevice.dwDevIns)
{
bFlag = TRUE;
break;
}
}
if (!bFlag)
{
printf ("Dev Not Found!
");
}
else
{
//初始化属性
SP_PROPCHANGE_PARAMS propChange;
propChange.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
propChange.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
propChange.Scope = DICS_FLAG_GLOBAL; //使修改的属性保存在所有的硬件属性文件
propChange.StateChange = STATE/*bStatusFlag ? DICS_START: DICS_STOP*/; //问题1
propChange.HwProfile = 0;
//为设备信息集或者是一个实际的设备信息单元设置或清除类安装参数,需要传入SP_PROPCHANGE_PARAMS结构指针,这个结构是负责协调DIF_PROPERTYCHANGE (propertychange)安装请求
if (SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER*)&propChange, sizeof(propChange)))
{
if (!SetupDiChangeState(hDevInfo, &DeviceInfoData))
{
/*printf("Change Device ERR!
");
bFlag = FALSE;*/
DWORD dwErrCode = ERROR_NO_CLASSINSTALL_PARAMS;
dwErrCode= GetLastError();
printf("SetupDiChangeState. Error Code:%x
",GetLastError());
bFlag = FALSE;
}
}
else
{
printf("SetupDiSetClassInstallParams ERR!
"); //?
bFlag = FALSE;
}
// //改变设备属性
//if (!SetupDiSetClassInstallParams( hDevInfo,
// &DeviceInfoData,
// (SP_CLASSINSTALL_HEADER *)&propChange,
// sizeof(propChange)))
//{
// OutputDebugString("SetupDiSetClassInstallParams FAILED");
// return FALSE;
//}
}
// 释放
SetupDiDestroyDeviceInfoList(hDevInfo);
} while (FALSE);
return bFlag;
}
其中在OnInitDialog函数中:
LPGUID Class = (LPGUID)malloc(sizeof(LPGUID));
//class是USB输入设备的的类GUID
Class->Data1=0x745a17a0;
Class->Data2=0x74d3;
Class->Data3=0x11d0;
Class->Data4[0]=0xb6;
Class->Data4[1]=0xfe;
Class->Data4[2]=0x00;
Class->Data4[3]=0xa0;
Class->Data4[4]=0xc9;
Class->Data4[5]=0x0f;
Class->Data4[6]=0x57;
Class->Data4[7]=0xda;
LPGUID lpGuid=NULL;
GetDeviceList(Class);
SetDeviceStatus(theItem,0);
其中,重要的变量定义如:
DeviceInfo theItem; #define CLOSESTATE DICS_DISABLE #define OPENSTATE DICS_ENABLE #define STATE OPENSTATE
特别说明vs要以管理员身份运行。