请问这样得到硬盘的物理序列号 请问这样得到硬盘的物理序列号,,有没有VC的源代码啊谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=195689 转贴:http://www.xnovo.com/doc/html/aebbbgbgbbbaaacznlwfhmlt.html WMI取出来的是不是你需要的// Get harddisk information.//tested by onega VC20003,windows 2003#include "stdafx.h"#define _WIN32_DCOM#include <iostream>using namespace std;#include <comdef.h>#include <Wbemidl.h>#include <atlcomcli.h># pragma comment(lib, "wbemuuid.lib")struct InitOle{ InitOle() { hres = CoInitializeEx(0, COINIT_MULTITHREADED); } ~InitOle() { ::CoUninitialize(); } HRESULT hres;} _init_InitOle_;HRESULT PrintObject(IWbemClassObject* spInstance){ if(!spInstance) return S_OK; std::cout<<__FUNCTION__<<" start "<<std::endl; LPSAFEARRAY psa = NULL; HRESULT hres; hres = spInstance->GetNames( NULL, WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY, NULL, &psa); long lLower, lUpper; SafeArrayGetLBound(psa , 1, &lLower); SafeArrayGetUBound(psa , 1, &lUpper); for (long i = lLower; i <= lUpper; ++i) { CComBSTR bstrPropName; if (S_OK != (hres = SafeArrayGetElement(psa, &i, &bstrPropName)) ) { if (NULL != psa) SafeArrayDestroy(psa); return hres; } std::cout<<" "<<(LPCTSTR)_bstr_t(bstrPropName); _variant_t varProperty; HRESULT hr = spInstance->Get(bstrPropName,0,&varProperty,NULL,0); std::cout<<" = "; if(varProperty.vt !=VT_EMPTY && VT_NULL != varProperty.vt) { _variant_t vDest; hr = VariantChangeType(&vDest, &varProperty,0,VT_BSTR); if(SUCCEEDED(hr)) std::cout<< (LPCTSTR)_bstr_t(varProperty); else { if(varProperty.vt == (VT_ARRAY|VT_I4)) { SAFEARRAY* psa = varProperty.parray; int *pIntArray = NULL; SafeArrayAccessData(psa,(VOID**)&pIntArray); UINT uDim = SafeArrayGetDim(psa); if(1==uDim) { long lLbound,lRbound; SafeArrayGetLBound(psa,1,&lLbound); SafeArrayGetUBound(psa,1,&lRbound); for(long i=lLbound;i<=lRbound;i++) { std::cout<< pIntArray[i]<<" "; } } SafeArrayUnaccessData(psa); } else { std::cout<< varProperty.vt << " type "; } } } else { if(varProperty.vt == VT_EMPTY) std::cout<< "VT_EMPTY "; else std::cout<< "VT_NULL"; } std::cout<<std::endl; } if (NULL != psa) SafeArrayDestroy(psa); std::cout<<__FUNCTION__<<" end "<<std::endl; return S_OK;}int main(int argc, char **argv){ HRESULT hres = S_OK; if (FAILED(_init_InitOle_.hres)) { cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl; return 1; // Program has failed. } // Step 2: -------------------------------------------------- // Set general COM security levels -------------------------- // Note: If you are using Windows 2000, you need to specify - // the default authentication credentials for a user by using // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ---- // parameter of CoInitializeSecurity ------------------------ hres = CoInitializeSecurity( NULL, -1, // COM authentication NULL, // Authentication services NULL, // Reserved RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation NULL, // Authentication info EOAC_NONE, // Additional capabilities NULL // Reserved ); if (FAILED(hres)) { cout << "Failed to initialize security. Error code = 0x" << hex << hres << endl; return 1; // Program has failed. } // Step 3: --------------------------------------------------- // Obtain the initial locator to WMI ------------------------- CComPtr<IWbemLocator> pLoc;//IWbemLocator *pLoc = NULL; hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc); if (FAILED(hres)) { cout << "Failed to create IWbemLocator object." << " Err code = 0x" << hex << hres << endl; return 1; // Program has failed. } // Step 4: ----------------------------------------------------- // Connect to WMI through the IWbemLocator::ConnectServer method CComPtr<IWbemServices> pSvc;//IWbemServices *pSvc = NULL; // Connect to the root\cimv2 namespace with // the current user and obtain pointer pSvc // to make IWbemServices calls. hres = pLoc->ConnectServer( _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace NULL, // User name. NULL = current user NULL, // User password. NULL = current 0, // Locale. NULL indicates current NULL, // Security flags. 0, // Authority (e.g. Kerberos) 0, // Context object &pSvc // pointer to IWbemServices proxy ); if (FAILED(hres)) { cout << "Could not connect. Error code = 0x" << hex << hres << endl; return 1; // Program has failed. } cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl; // Step 5: -------------------------------------------------- // Set security levels on the proxy ------------------------- hres = CoSetProxyBlanket( pSvc, // Indicates the proxy to set RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx NULL, // Server principal name RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx NULL, // client identity EOAC_NONE // proxy capabilities ); if (FAILED(hres)) { cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl; return 1; // Program has failed. } // Step 6: -------------------------------------------------- // Use the IWbemServices pointer to make requests of WMI ---- _bstr_t wmi_query_str("SELECT * FROM Win32_DiskDrive"); CComPtr<IEnumWbemClassObject> pEnumerator;//IEnumWbemClassObject* pEnumerator = NULL; hres = pSvc->ExecQuery( bstr_t("WQL"), wmi_query_str, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { cout << "Query for Win32_DiskDrive failed." << " Error code = 0x" << hex << hres << endl; return 1; // Program has failed. } // Step 7: ------------------------------------------------- // Get the data from the query in step 6 ------------------- ULONG uReturn = 0; while (pEnumerator) { CComPtr<IWbemClassObject> pclsObj;//IWbemClassObject *pclsObj; HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if(0 == uReturn) { break; } PrintObject(pclsObj); } return 0; // Program successfully completed.} C++问题,在通信中选择了以结构体传输, 大家讨论讨论! 封装成OCX 大家进来瞧瞧!!请各位大峡路见不平一声吼救一下小弟!! 关于StringTable的问题 可以发一份XTreme.Toolkit.Pro.v9.6.0.1的函数使用说明文档我吗?my email: 君卿本无意,来者犹可知,散分 关于数据库的一个小问题, 谢谢 开发报表的问题 如何在已经创建好的SDI程序中加入一个登录框,它的要求是: test N多VC QQ群,欢迎大家加入及补充 请问哪里有关于COM的教学的书下载,谢谢!
// Get harddisk information.
//tested by onega VC20003,windows 2003
#include "stdafx.h"
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
#include <atlcomcli.h>
# pragma comment(lib, "wbemuuid.lib")
struct InitOle
{
InitOle()
{
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
}
~InitOle() { ::CoUninitialize(); }
HRESULT hres;
} _init_InitOle_;
HRESULT PrintObject(IWbemClassObject* spInstance)
{
if(!spInstance)
return S_OK;
std::cout<<__FUNCTION__<<" start "<<std::endl;
LPSAFEARRAY psa = NULL;
HRESULT hres;
hres = spInstance->GetNames( NULL,
WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY,
NULL,
&psa);
long lLower, lUpper;
SafeArrayGetLBound(psa , 1, &lLower);
SafeArrayGetUBound(psa , 1, &lUpper);
for (long i = lLower; i <= lUpper; ++i)
{
CComBSTR bstrPropName;
if (S_OK != (hres = SafeArrayGetElement(psa, &i, &bstrPropName)) )
{
if (NULL != psa)
SafeArrayDestroy(psa);
return hres;
}
std::cout<<" "<<(LPCTSTR)_bstr_t(bstrPropName);
_variant_t varProperty;
HRESULT hr = spInstance->Get(bstrPropName,0,&varProperty,NULL,0);
std::cout<<" = ";
if(varProperty.vt !=VT_EMPTY && VT_NULL != varProperty.vt)
{
_variant_t vDest;
hr = VariantChangeType(&vDest, &varProperty,0,VT_BSTR);
if(SUCCEEDED(hr))
std::cout<< (LPCTSTR)_bstr_t(varProperty);
else
{
if(varProperty.vt == (VT_ARRAY|VT_I4))
{
SAFEARRAY* psa = varProperty.parray;
int *pIntArray = NULL;
SafeArrayAccessData(psa,(VOID**)&pIntArray);
UINT uDim = SafeArrayGetDim(psa);
if(1==uDim)
{
long lLbound,lRbound;
SafeArrayGetLBound(psa,1,&lLbound);
SafeArrayGetUBound(psa,1,&lRbound);
for(long i=lLbound;i<=lRbound;i++)
{
std::cout<< pIntArray[i]<<" ";
}
}
SafeArrayUnaccessData(psa);
}
else
{
std::cout<< varProperty.vt << " type ";
}
}
}
else
{
if(varProperty.vt == VT_EMPTY)
std::cout<< "VT_EMPTY ";
else
std::cout<< "VT_NULL";
}
std::cout<<std::endl;
}
if (NULL != psa)
SafeArrayDestroy(psa);
std::cout<<__FUNCTION__<<" end "<<std::endl;
return S_OK;
}
int main(int argc, char **argv)
{
HRESULT hres = S_OK;
if (FAILED(_init_InitOle_.hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------ hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
CComPtr<IWbemLocator> pLoc;//IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
CComPtr<IWbemServices> pSvc;//IWbemServices *pSvc = NULL;
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
); if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
_bstr_t wmi_query_str("SELECT * FROM Win32_DiskDrive");
CComPtr<IEnumWbemClassObject> pEnumerator;//IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
wmi_query_str,
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for Win32_DiskDrive failed."
<< " Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
ULONG uReturn = 0;
while (pEnumerator)
{
CComPtr<IWbemClassObject> pclsObj;//IWbemClassObject *pclsObj;
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
PrintObject(pclsObj);
}
return 0; // Program successfully completed.
}