dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 19

Thread: BIOS Serial (Code Included)

Hybrid View

  1. #1
    Join Date
    Apr 2005
    Posts
    42

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

  2. #2
    Join Date
    May 2004
    Location
    Michigan, United States
    Posts
    457

    Re: BIOS Serial (Code Included)

    1. What's the error you're receiving?
    2. Can you put your code in a code tag?
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  3. #3
    Join Date
    Apr 2005
    Posts
    42

    Re: BIOS Serial (Code Included)

    Thx a lot for replying. I get the following error message.
    Please also see the attached file.
    Error:

    Unhandled exception at 0x0041d956 in wmii_test.exe: 0xC0000005: Access violation reading location 0x00000000.

    First-chance exception at 0x0041d956 in wmii_test.exe: 0xC0000005: Access violation reading location 0x00000000.
    Unhandled exception at 0x0041d956 in wmii_test.exe: 0xC0000005: Access violation reading location 0x00000000.
    Attached Files Attached Files

  4. #4
    Join Date
    Dec 2003
    Posts
    45

    Re: BIOS Serial (Code Included)

    ....
    ....
    hres = pSvc->ExecQuery(
    bstr_t("WQL"),
    bstr_t("SELECT * FROM Win32_BIOS "),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &pEnumerator);

    IWbemClassObject* pInstance;
    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
    {
    ULONG dwCount = 0;

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

    } while (hres == WBEM_S_NO_ERROR);

    }

    VARIANT v;
    VariantInit(&v);

    // IWbemClassObject * pClassObject = NULL;
    BSTR strClassProp = SysAllocString(L"SerialNumber");

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,059

    Re: BIOS Serial (Code Included)

    I made a few changes to the program (see attached):

    1. Used the _COM_SMARTPTR_TYPEDEF macros defined in <comdef.h> to convert all the IWbemXXX interfaces to smart pointers - makes calling IWbemXXX->Release() unnecessary.
    2. Used the _variant_t wrapper instead of VARIANT
    3. Moved the IWbemClassObjectPtr->Get() call inside the do/while loop.
    4. Changed the "Serial Number" property to "SerialNumber" as ShelleyWang suggested.
    5. Moved the Bios retrieval code into a function so CoUninitialize doesn't get called before the smart pointers go out of scope (a scoping block would have worked equally as well).
    6. Replaced the call to IWbemServices->ExecQuery() with a call to IWbemServices->CreateInstanceEnum(). I couldn't get the ExecQuery call to work with your code and I didn't have time to look into the Synchronous, Asynchronous, or SemiSynchronous modes. (As George Carlin once said: "It either flams or it doesn't flam." ). Anyway, CreateInstanceEnum works fine here.
    Arjay
    Attached Files Attached Files

  6. #6
    Join Date
    Apr 2005
    Posts
    42

    Smile Re: BIOS Serial (Code Included)

    Thanks a lot for your help. I really appreciate the time you spend or debugging this code. The code although is giving no errors, but it also not displaying the required BIOS information ..

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)