CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2002
    Posts
    18

    How to get Hardware information?

    (In visual C++6)
    I want get some information: disk driver(Quantum, Seagate...), and Serial of BIOS...
    can you help me!

    Thank very much!

  2. #2
    Join Date
    Apr 2005
    Posts
    42

    Exclamation BIOS Serial (Code Included)

    This code reads the BIOS serial number using WMI but generates an error i'm unable to resolve. Help needed regarding this.

    #include "stdafx.h"
    #define _WIN32_DCOM
    #include <iostream>
    using namespace std;
    #include <comdef.h>
    #include <Wbemidl.h>

    # pragma comment(lib, "wbemuuid.lib")

    int main(int argc, char **argv)
    {
    HRESULT hres;

    // Initialize COM.
    hres = CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hres))
    {
    cout << "Failed to initialize COM library. "
    << "Error code = 0x"
    << hex << hres << endl;
    return 1; // Program has failed.
    }

    // Initialize
    hres = CoInitializeSecurity(
    NULL,
    -1, // COM negotiates service
    NULL, // Authentication services
    NULL, // Reserved
    RPC_C_AUTHN_LEVEL_DEFAULT, // authentication
    RPC_C_IMP_LEVEL_IMPERSONATE, // Impersonation
    NULL, // Authentication info
    EOAC_NONE, // Additional capabilities
    NULL // Reserved
    );


    if (FAILED(hres))
    {
    cout << "Failed to initialize security. "
    << "Error code = 0x"
    << hex << hres << endl;
    CoUninitialize();
    return 1; // Program has failed.
    }

    // Obtain the initial locator to Windows Management
    // on a particular host computer.
    IWbemLocator *pLoc = 0;

    hres = CoCreateInstance(
    CLSID_WbemLocator,
    0,
    CLSCTX_INPROC_SERVER,
    IID_IWbemLocator, (LPVOID *) &pLoc);

    if (FAILED(hres))
    {
    cout << "Failed to create IWbemLocator object. "
    << "Error code = 0x"
    << hex << hres << endl;
    CoUninitialize();
    return 1; // Program has failed.
    }

    IWbemServices *pSvc = 0;

    // 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"), // WMI namespace
    NULL, // User name
    NULL, // User password
    0, // Locale
    NULL, // Security flags
    0, // Authority
    0, // Context object
    &pSvc // IWbemServices proxy
    );

    if (FAILED(hres))
    {
    cout << "Could not connect. Error code = 0x"
    << hex << hres << endl;
    pLoc->Release();
    CoUninitialize();
    return 1; // Program has failed.
    }

    cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;

    // Set the IWbemServices proxy so that impersonation
    // of the user (client) occurs.
    hres = CoSetProxyBlanket(

    pSvc, // the proxy to set
    RPC_C_AUTHN_WINNT, // authentication service
    RPC_C_AUTHZ_NONE, // authorization service
    NULL, // Server principal name
    RPC_C_AUTHN_LEVEL_CALL, // authentication level
    RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
    NULL, // client identity
    EOAC_NONE // proxy capabilities
    );

    if (FAILED(hres))
    {
    cout << "Could not set proxy blanket. Error code = 0x"
    << hex << hres << endl;
    pSvc->Release();
    pLoc->Release();
    CoUninitialize();
    return 1; // Program has failed.
    }


    // Use the IWbemServices pointer to make requests of WMI.
    // Make requests here:

    // For example, query for print queues that
    // have more than 10 jobs
    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
    bstr_t("WQL"),
    bstr_t("SELECT * Win32_BIOS "),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &pEnumerator);

    if (FAILED(hres))
    {
    cout << "Query for print queues failed. "
    << "Error code = 0x"
    << hex << hres << endl;
    pSvc->Release();
    pLoc->Release();
    CoUninitialize();
    return 1; // Program has failed.
    }
    else
    {
    do
    {
    IWbemClassObject* pInstance = NULL;
    ULONG dwCount = NULL;

    hres = pEnumerator->Next(
    WBEM_INFINITE,
    1,
    &pInstance,
    &dwCount);

    } while (hres == WBEM_S_NO_ERROR);

    }

    VARIANT v;

    IWbemClassObject * pClassObject = NULL;
    BSTR strClassProp = SysAllocString(L"Serial Number");
    hres = pClassObject->Get(strClassProp, 0, &v, 0, 0);
    SysFreeString(strClassProp);

    _bstr_t bstrPath = &v; //Just to convert BSTR to ANSI


    char* strPath=(char*)bstrPath;
    if (SUCCEEDED(hres))
    printf (strPath);
    else
    printf ("Error in getting object");
    VariantClear( &v );

    // Cleanup
    // ========

    pSvc->Release();
    pLoc->Release();
    CoUninitialize();



    return 0; // Program successfully completed.
    }
    Attached Files Attached Files
    Last edited by SoftLock; April 21st, 2005 at 04:36 AM.

  3. #3
    Join Date
    May 2005
    Posts
    4

    Re: How to get Hardware information?

    I've been battling around trying to implement WMI into my MFC application. I'm trying to get Windows XP informations such as product key, activation status, ect. I used COM init to interface with Win32_WindowsProductActivation class. I have problems with changing Windows XP Product Key. How can I run SetProductKey method with parameters??? I went through Microsoft docs but it's not clear to me at all. Any hits or sample code??? I've seen VB script but i have difficulty implementing it with C++.
    Thank you all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured