请问这样得到硬盘的物理序列号,,有没有VC的源代码啊
谢谢

解决方案 »

  1.   

    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=195689
      

  2.   

    转贴:http://www.xnovo.com/doc/html/aebbbgbgbbbaaacznlwfhmlt.html
      

  3.   

    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.
    }