CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  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 / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

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

  7. #7
    Join Date
    Dec 2003
    Posts
    45

    Re: BIOS Serial (Code Included)

    Hello Soft, Do you mean my codes still run improperly?

    But on my computer, it's run normally and get the right Serial Number.

  8. #8
    Join Date
    Apr 2005
    Posts
    42

    Re: BIOS Serial (Code Included)

    Your code runs perfectly alright, but it doesn't display any output. Can you plz send the exe file or preferably the project files.

    Thanks.

  9. #9
    Join Date
    Dec 2003
    Posts
    45

    Re: BIOS Serial (Code Included)

    I have two computer, one runs properly and get the right Serial Number, then other runs properly too but it cann't gets the Serial Number, it appears "none".

    I don't know what's the mater with this program.

    Anywhere , if you want the program, please give me you E-Mail address

  10. #10
    Join Date
    Apr 2005
    Posts
    42

    Smile Re: BIOS Serial (Code Included)

    Last edited by SoftLock; May 8th, 2005 at 02:09 PM.

  11. #11
    Join Date
    Apr 2005
    Posts
    42

    Unhappy Re: BIOS Serial (Code Included)

    Can anyone comment on this, why it is not displaying the BIOS information?

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

    Re: BIOS Serial (Code Included)

    I'd suggest installing the WMI Tools from the sdk and using the CIM Studio to determine if the SerialNumber property is actually set. Btw, did you run the modified wmii_test code that I attached. I tried it out on a couple of computers and it worked on both.

    [Arjay revision] Actually it did work, but until I looked at the output and compared it with CIM Studio, I didn't know that Win32_BIOS is the wrong class to use. Instead use Win32_BaseBoard.

    See the attachment for a sample that uses this data.

    Arjay

    Code:
    IWbemClassObjectPtr spBaseBoard = NULL;
    if( FAILED( hres 
    = spSvc->GetObject(_bstr_t("Win32_BaseBoard.Tag=\"Base Board\""),
    	0,
    	NULL,
    	&spBaseBoard,
    	NULL)) )
    {
    cout << "Unable to Win32_BaseBoard Object. "
    	<< "Error code = 0x"
    	<< hex << hres << endl;
    return hres;
    }
     
    variant_t v;
    if( FAILED( hres 
    = spBaseBoard->Get(bstr_t( L"SerialNumber" ), 0, &v, 0, 0)) )
    {
    cout << "Unable to retrieve SerialNumber property. "
    	<< "Error code = 0x" 
    	<< hex << hres << endl;
    return hres;
    }
    cout << "SerialNumber: " << bstr_t(v) << endl;
     
    
    Attached Files Attached Files
    Last edited by Arjay; May 8th, 2005 at 04:51 PM.

  13. #13
    Join Date
    Dec 2003
    Posts
    45

    Re: BIOS Serial (Code Included)

    Hello Arjay, Can you give me all your source code of 'SystemInfo'?

    Thanks, 201117@163.com
    Last edited by ShelleyWang; May 9th, 2005 at 06:29 AM.

  14. #14
    Join Date
    Apr 2005
    Posts
    42

    Smile Re: BIOS Serial (Code Included)

    Hello Arjay, that was really considerate and kind of you to look into the code in detail. It helped a lot and you cant imagine that how accelerating that was in simulating an aspect of ma project. thanx a lot again, God bless ya!

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

    Re: BIOS Serial (Code Included)

    Quote Originally Posted by ShelleyWang
    Hello Arjay, Can you give me all your source code of 'SystemInfo'?

    Thanks, 201117@163.com
    Unfortunately, not as this time as it would take quite a bit of work to refactor it. I did write some similar code for a sample in another forum called Using WMI... The sample is in C#, but can be easily ported back to C++.

    Arjay

Page 1 of 2 12 LastLast

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