BIOS Seriennummer mit WMI auslesen (Win32_BIOS-Klasse)

AnsiString GetBiosSerialNum()
{
    AnsiString ret;
    CoInitializeEx(0, COINIT_MULTITHREADED) ;
    CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);

    IWbemLocator *t_Locator = NULL ;
    HRESULT t_Result = CoCreateInstance (CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown, ( void ** ) & t_Locator);

    if ( SUCCEEDED ( t_Result ) )
    {
        BSTR t_Namespace = SysAllocString ( L"rootcimv2" ) ;
        if ( t_Namespace )
        {
            IWbemServices *t_Service = NULL ;
            t_Result = t_Locator->ConnectServer (t_Namespace, NULL, NULL, NULL, 0, NULL, NULL, &t_Service) ;

            if ( SUCCEEDED ( t_Result ) )
            {
                // die Property "SerialNumber" der Klasse "Win32_BIOS" auslesen
                BSTR t_Query = SysAllocString ( L"SELECT SerialNumber FROM Win32_BIOS" );
                BSTR t_QueryLanguage = SysAllocString ( L"WQL" );

                if ( t_Query && t_QueryLanguage )
                {
                    // Query starten
                    IEnumWbemClassObject *t_Enumeration = NULL ;
                    t_Result = t_Service->ExecQuery (t_QueryLanguage, t_Query, 0, NULL, & t_Enumeration);

                    // wenn Query erfolgreich
                    if ( SUCCEEDED ( t_Result ) )
                    {
                        t_Enumeration->Reset ();
                        IWbemClassObject *t_ClassObject = NULL;
                        ULONG t_Count = 0;

                        while ( t_Enumeration->Next ( INFINITE, 1, &t_ClassObject, &t_Count ) == WBEM_NO_ERROR )
                        {
                            BSTR t_PropertyName = NULL;
                            CIMTYPE t_CimType;
                            VARIANT t_Variant;
                            VariantInit ( & t_Variant );

                            t_ClassObject->BeginEnumeration ( WBEM_FLAG_NONSYSTEM_ONLY );

                            // alle von der Query angeforderten Properties der Klasse durchgehen
                            while ( t_ClassObject->Next ( 0, &t_PropertyName, &t_Variant, &t_CimType, NULL ) == WBEM_NO_ERROR )
                            {
                                // String zusammenbauen
                                ret += t_Variant.bstrVal;
                                SysFreeString ( t_PropertyName );
                                VariantClear ( & t_Variant );
                            }

                            t_ClassObject->Release ();
                        }

                        t_Enumeration->Release ();
                    }
                }

                SysFreeString ( t_Query );
                SysFreeString ( t_QueryLanguage );
            }

            t_Service->Release ();
            SysFreeString ( t_Namespace );
        }
    }

    t_Locator->Release () ;

    return ret;
}

Kommentare sind geschlossen.