我编写了一个C ++函数,它通过WMI查询获取USB驱动器的PNPDeviceID,并在Win32-Console-Application中对其进行测试。它工作得很好,但是当我将代码放入CredentialProvider-DLL时,CoInitialize()
和CoInitializeSecurity()
函数失败了,因为DLL调用进程(Windows CredUI-Subsystem)已经调用了它们。那么如何让WMI查询在该DLL上运行?我需要重置默认情况下似乎无法执行的COM安全设置。
这是功能代码:
std::wstring GetHardwareID(char driveLetter)
{
std::wstring returnString = L"";
wchar_t volumeAccessPath[] = L"\\\\.\\X:";
volumeAccessPath[4] = driveLetter;
HANDLE deviceHandle = CreateFileW(volumeAccessPath,
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes
DWORD bytes;
STORAGE_DEVICE_NUMBER devd;
STORAGE_BUS_TYPE busType = BusTypeUnknown;
if (DeviceIoControl(deviceHandle,
IOCTL_STORAGE_GET_DEVICE_NUMBER ,
NULL, 0,
&devd, sizeof(devd),
&bytes, NULL))
{
HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if((FAILED(hRes = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0))))
{
return returnString;
}
IWbemLocator* pLocator = NULL;
if(FAILED(hRes = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pLocator))))
{ return returnString;}
IWbemServices* pService = NULL;
if(FAILED(hRes = pLocator->ConnectServer(L"root\\CIMV2", NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &pService)))
{
pLocator->Release();
dbg(convertInt(hRes).c_str());
return returnString;
}
IEnumWbemClassObject* pEnumerator = NULL;
if(FAILED(hRes = pService->ExecQuery(L"WQL", L"SELECT * FROM Win32_DiskDrive ", WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumerator)))
{
pLocator->Release();
pService->Release();
dbg(convertInt(hRes).c_str());
return returnString;
}
IWbemClassObject* clsObj = NULL;
int numElems;
while((hRes = pEnumerator->Next(WBEM_INFINITE, 1, &clsObj, (ULONG*)&numElems)) != WBEM_S_FALSE)
{
if(FAILED(hRes))
break;
VARIANT vRet;
VariantInit(&vRet);
if(SUCCEEDED(clsObj->Get(L"DeviceID", 0, &vRet, NULL, NULL)) && vRet.vt == VT_BSTR)
{
bool found = false;
std::wstring ws(vRet.bstrVal);
if (ws[17] == '0' + devd.DeviceNumber)
found = true;
VariantClear(&vRet);
if(SUCCEEDED(clsObj->Get(L"PNPDeviceID", 0, &vRet, NULL, NULL)) && vRet.vt == VT_BSTR && found)
{
std::wstring retStr(vRet.bstrVal);
VariantClear(&vRet);
std::wstring k(L"&");
int pos =retStr.rfind(k);
returnString = retStr.substr(0, pos);
}
}
clsObj->Release();
}
pEnumerator->Release();
pService->Release();
pLocator->Release();
}
return returnString;
}
提前致谢
答案 0 :(得分:1)
解决方案可以是将所有用于获取USB驱动器的PNPDeviceID的代码放入Windows服务中。并通过命名管道在凭证提供程序和Windows服务之间建立IPC。
P.S。这些方法也很好,所有“硬”的东西都在windows服务中完成(具有更多功能),而且你的提供者轻巧简单,这将有助于你避免将来出现问题。