wmi读取硬盘物理序列号 exe中正常 dll中无法读出
下面的代码在exe中执行,能正确的用wmi读取到硬件信息,但是放到dll中就不行了,请高手们指点,如果分数不够,还可以加。
//--------------------------------------------------------------------------
#include <comdef.h>
#include <wbemidl.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
char *AllWmiClasses[] =
{
"Win32_1394Controller" ,
"Win32_BaseBoard" ,
"Win32_Battery" ,
"Win32_BIOS" ,
"Win32_Bus" ,
"Win32_CacheMemory" ,
"Win32_CDROMDrive" ,
"Win32_CurrentProbe" ,
"Win32_DesktopMonitor" ,
"Win32_DeviceMemoryAddress" ,
"Win32_DiskDrive" ,
"Win32_DisplayConfiguration" ,
"Win32_DisplayControllerConfiguration",
"Win32_DMAChannel" ,
"Win32_Fan" ,
"Win32_FloppyController" ,
"Win32_FloppyDrive" ,
"Win32_HeatPipe" ,
"Win32_IDEController" ,
"Win32_InfraredDevice" ,
"Win32_IRQResource" ,
"Win32_Keyboard" ,
"Win32_MemoryArray" ,
"Win32_MemoryDevice" ,
"Win32_MotherboardDevice" ,
"Win32_NetworkAdapter" ,
"Win32_NetworkAdapterConfiguration" ,
"Win32_OnBoardDevice" ,
"Win32_ParallelPort" ,
"Win32_PCMCIAController" ,
"Win32_PhysicalMemory" ,
"Win32_PhysicalMemoryArray" ,
"Win32_PnPEntity" ,
"Win32_PointingDevice" ,
"Win32_PortableBattery" ,
"Win32_PortConnector" ,
"Win32_PortResource" ,
"Win32_POTSModem" ,
"Win32_PowerManagementEvent" ,
"Win32_Printer" ,
"Win32_PrinterConfiguration" ,
"Win32_PrintJob" ,
"Win32_Processor" ,
"Win32_Refrigeration" ,
"Win32_SerialPort" ,
"Win32_SerialPortConfiguration" ,
"Win32_SMBIOSMemory" ,
"Win32_SoundDevice" ,
"Win32_SystemEnclosure" ,
"Win32_SystemMemoryResource" ,
"Win32_SystemSlot" ,
"Win32_TapeDrive" ,
"Win32_TemperatureProbe" ,
"Win32_UninterruptiblePowerSupply" ,
"Win32_USBController" ,
"Win32_VideoConfiguration" ,
"Win32_VideoController" ,
"Win32_VoltageProbe" ,
NULL
};
void GetWmiInfo(TStrings *lpList, WideString wsClass)
{
IWbemLocator *pWbemLocator = NULL;
if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
{
IWbemServices *pWbemServices = NULL;
UnicodeString wsNamespace = (L"root\\cimv2");
if(pWbemLocator->ConnectServer(wsNamespace.c_str(), NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
{
IEnumWbemClassObject *pEnumClassObject = NULL;
UnicodeString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
if(pWbemServices->ExecQuery(wsWQL.c_str(), wsQuery.c_str(), WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
{
IWbemClassObject *pClassObject = NULL;
ULONG uCount = 1, uReturned;
if(pEnumClassObject->Reset() == S_OK)
{ //MessageBox(NULL,L"enter1",L"aabb",MB_OK);
int iEnumIdx = 0;
while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
{
lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
//MessageBox(NULL,L"enter2",L"aab",MB_OK);
SAFEARRAY *pvNames = NULL;
if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
{
long vbl, vbu;
SafeArrayGetLBound(pvNames, 1, &vbl);
SafeArrayGetUBound(pvNames, 1, &vbu);
for(long idx=vbl; idx<=vbu; idx++)
{
long aidx = idx;
wchar_t *wsName = 0;
VARIANT vValue;
VariantInit(&vValue);
SafeArrayGetElement(pvNames, &aidx, &wsName);
BSTR bs = SysAllocString(wsName);
HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
SysFreeString(bs);
if(hRes == S_OK)
{
AnsiString s;
Variant v = *(Variant*)&vValue;
if(v.IsArray())
{
for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
{
Variant a = v.GetElement(i);
if(!s.IsEmpty())
s+=", ";
s+=VarToStr(a);
}
}
else
{
s = VarToStr(v);
}
lpList->Add(AnsiString(wsName)+"="+s);
}
VariantClear(&vValue);
SysFreeString(wsName);
}
}
if(pvNames)SafeArrayDestroy(pvNames);
iEnumIdx++;
}
} else MessageBox(NULL,L"err4",L"aa",MB_OK);
if(pClassObject)pClassObject->Release();
} else MessageBox(NULL,L"err3",L"aa",MB_OK);
if(pEnumClassObject)pEnumClassObject->Release();
} else MessageBox(NULL,L"err2",L"aa",MB_OK);
if(pWbemServices)pWbemServices->Release();
}else MessageBox(NULL,L"err1",L"aa",MB_OK);
if(pWbemLocator)pWbemLocator->Release();
}
UnicodeString __fastcall Pub_getHD0(String id,bool toMD5) {
CoInitialize(NULL);
HRESULT hr=CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
if (hr!= S_OK && hr!=RPC_E_TOO_LATE) {
//ShowMessage("无法获取权限!"); Application->Terminate();
//gPermission=1;//只能读取1次,否则会出错
CoUninitialize(); return "NoPermission";
}
TStringList*st1=new TStringList;
GetWmiInfo(st1, "Win32_DiskDrive"); //"Win32_PhysicalMedia");//
CoUninitialize();
str=st1->Text; MessageBox(NULL,String(str).c_str(),L"aa",MB_OK);
return str;
}