编译环境:VC6.0,Directx 9.0
代码
BOOL FindDevicesPid(long pid)
{
BOOL myCamera_exist = FALSE;
WCHAR CameraPid[256] = {0}; //摄像头PID值
_itow( pid, CameraPid, 16 ); //十进制转化为十六进制存储在字符串中
wstring sCameraPid(CameraPid);
int length = sCameraPid.length();
for(;length<4;length++)
sCameraPid= L"0" + sCameraPid;
sCameraPid= L"pid_"+sCameraPid;
// 枚举是有视频设备
ICreateDevEnum *pCreateDevEnum=0;
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum, (void**)&pCreateDevEnum);
if(hr != NOERROR)
{
MessageBoxW(NULL,L"创建失败!",L"系统提示",NULL);
return FALSE;
}
IEnumMoniker *pEm=0;
hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEm, 0);
if(hr != NOERROR)
{
MessageBoxW(NULL,L"没有视频设备!",L"系统提示",NULL);
return FALSE;
}
pEm->Reset();
ULONG cFetched;
IMoniker *pM;
while(hr = pEm->Next(1, &pM, &cFetched), hr==S_OK)
{
IPropertyBag *pBag=0;
hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag);
if(SUCCEEDED(hr))
{
//读取设备名
VARIANT var;
var.vt = VT_BSTR;
hr = pBag->Read(L"FriendlyName", &var, NULL);
if (hr == NOERROR)
{
//读取显示名称,其中包含了设备PID和VID值
//如:@device:pnp:\\?\usb#vid_1b17&pid_6687&mi_00#6&1d285357&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global
WCHAR * wzDisplayName = NULL;
hr = pM->GetDisplayName( NULL , NULL, &wzDisplayName );
wstring displayName = wzDisplayName;
//查找摄像头PID是否正确
if (displayName.find(sCameraPid) != wstring::npos)
{
myCamera_exist = TRUE;
break;
}
}
}
}
return myCamera_exist;
}
{
BOOL myCamera_exist = FALSE;
WCHAR CameraPid[256] = {0}; //摄像头PID值
_itow( pid, CameraPid, 16 ); //十进制转化为十六进制存储在字符串中
wstring sCameraPid(CameraPid);
int length = sCameraPid.length();
for(;length<4;length++)
sCameraPid= L"0" + sCameraPid;
sCameraPid= L"pid_"+sCameraPid;
// 枚举是有视频设备
ICreateDevEnum *pCreateDevEnum=0;
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum, (void**)&pCreateDevEnum);
if(hr != NOERROR)
{
MessageBoxW(NULL,L"创建失败!",L"系统提示",NULL);
return FALSE;
}
IEnumMoniker *pEm=0;
hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEm, 0);
if(hr != NOERROR)
{
MessageBoxW(NULL,L"没有视频设备!",L"系统提示",NULL);
return FALSE;
}
pEm->Reset();
ULONG cFetched;
IMoniker *pM;
while(hr = pEm->Next(1, &pM, &cFetched), hr==S_OK)
{
IPropertyBag *pBag=0;
hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag);
if(SUCCEEDED(hr))
{
//读取设备名
VARIANT var;
var.vt = VT_BSTR;
hr = pBag->Read(L"FriendlyName", &var, NULL);
if (hr == NOERROR)
{
//读取显示名称,其中包含了设备PID和VID值
//如:@device:pnp:\\?\usb#vid_1b17&pid_6687&mi_00#6&1d285357&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global
WCHAR * wzDisplayName = NULL;
hr = pM->GetDisplayName( NULL , NULL, &wzDisplayName );
wstring displayName = wzDisplayName;
//查找摄像头PID是否正确
if (displayName.find(sCameraPid) != wstring::npos)
{
myCamera_exist = TRUE;
break;
}
}
}
}
return myCamera_exist;
}