谁有在win 7下能获取硬盘序列号的代码,要能用的,分是少了点
谁有在win 7下能获取硬盘序列号的代码,要能用的,分是少了点,现在很急,是软件保护用,
能获取CPU ID等等都行
[解决办法]
LZ很顾家啊,这分确实不多,呵呵!
[解决办法]
哎
都用Windows7跟上潮流了
我还在用XP 无法给你测试
[解决办法]
我昨天在你的另一贴中发过的
http://topic.csdn.net/u/20100323/14/88c92d01-d825-4cd6-bce1-c23dd1ab8f1d.html
而且我的测试程序就是在win7下用BCB2010写的,以下是我的测试程序,供参考:
//---------------------------------------
#include <vcl.h>
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "bcbatl.lib")
#pragma hdrstop
void __stdcall _com_issue_error(HRESULT) throw(_com_error)
{
ShowMessage("com error");
}
#include "wmi.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HRESULT hRes;
IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject = NULL;
VARIANT v;
//hRes=CoInitializeEx(0, COINIT_MULTITHREADED); //win7 下失败
hRes=CoInitialize(0);
if (FAILED(hRes))
{
ShowMessage(" CoInitializeEx error");
return;
}
//Security needs to be initialized in XP first
if(CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0
) != S_OK) //这个初始化只能一次,重新调用要出错的
{
CoUninitialize();
ShowMessage(" CoInitializeSecurity error");
return;
}
BSTR bstrNamespace = (L"\\\\你的机器\\root\\CIMV2");
BSTR name=NULL;
BSTR pass=NULL;
hRes = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pIWbemLocator
);
if (SUCCEEDED(hRes))
{
hRes = pIWbemLocator->ConnectServer(
bstrNamespace, // Namespace
name, // Userid
pass, // PW
NULL, // Locale
0, // flags
NULL, // Authority
NULL, // Context
&pWbemServices
);
}
else
{
ShowMessage(" CoCreateInstance error");
CoUninitialize();
return;
}
hRes = CoSetProxyBlanket(
pWbemServices, //Proxy
RPC_C_AUTHN_DEFAULT, //Authentication service
RPC_C_AUTHZ_DEFAULT, //Authorization service
COLE_DEFAULT_PRINCIPAL, //Server principal name used by authentication service
RPC_C_AUTHN_LEVEL_DEFAULT, //Authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, //Impersonation level
COLE_DEFAULT_AUTHINFO, //Client identity
EOAC_DEFAULT //Capability flags
);
if (FAILED(hRes))
{
ShowMessage(" CoSetProxyBlanket error");
CoUninitialize();
return;
}
//WCHAR temp[200],classname[100];
WCHAR temp[200],*classname; //win7 的 TComboBox的TEXT就是windstring类的
//comb->Text.WideChar(classname,comb->Text.WideCharBufSize());
classname=comb->Text;
wcscpy(temp,L"select * from ");
wcscat( temp,classname);
BSTR strQuery=SysAllocString(temp);
BSTR strQL = (L"WQL");
hRes = pWbemServices->ExecQuery(strQL,
strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject);
if (FAILED(hRes))
{
ShowMessage(" ExecQuery error ");
CoUninitialize();
return;
}
ULONG uCount = 1, uReturned;
IWbemClassObject * pClassObject = NULL;
hRes = pEnumObject->Reset();
hRes = pEnumObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned);
if (FAILED(hRes))
{
ShowMessage(" get objectclass error ");
CoUninitialize();
return;
}
SAFEARRAY*psarProp = NULL;
longlLower, lUpper;
BSTR strClassProp=NULL;
long row,i,col;
pClassObject->GetNames(NULL,WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY,NULL,&psarProp);
pClassObject->Release();
pClassObject = NULL;
SafeArrayGetLBound(psarProp, 1, &lLower);
SafeArrayGetUBound(psarProp, 1, &lUpper);
sg->ColCount = lUpper-lLower+1;
for (i = lLower, col = 0; i <= lUpper; ++i, ++col)
{
hRes = SafeArrayGetElement(psarProp, &i, &strClassProp);
sg->Cells[col][0]=strClassProp;
// Memo1->Lines->Add(strClassProp);
hRes = pEnumObject->Reset();
row=0;
while (SUCCEEDED(hRes = pEnumObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned)))
{
if (pClassObject ==NULL)
break;
// hRes = pClassObject->GetObjectText(0,&(v.bstrVal));
hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0);
if (SUCCEEDED(hRes))
{
row++;
if(v.vt==8)
sg->Cells[col][row]=v.bstrVal;
else
if (v.vt==3 || v.vt==1 )
sg->Cells[col][row]=v.iVal;
else
sg->Cells[col][row]=String("@")+v.vt;
}
else
{
ShowMessage("Error in getting object");
break;
}
pClassObject->Release();
pClassObject = NULL;
VariantClear( &v );
}
}
SysFreeString(strQuery);
SafeArrayDestroy(psarProp);
pIWbemLocator->Release();
pWbemServices->Release();
pEnumObject->Release();
CoUninitialize();
}
//---------------------------------------