Einzelnen Beitrag anzeigen

Horst0815

Registriert seit: 23. Mai 2011
Ort: Görlitz
150 Beiträge
 
Delphi XE Starter
 
#1

Hilfe bei übersetzung einer CPP schleife

  Alt 19. Jul 2012, 16:50
da andere beispiele(auch Programme) über WMI das SMBios auszulesen mit Fehler80041001 enden muss ich leider diese CPP Funktion verwenden da sie auf meinen Rechner funktioniert


hab erstmal so weit wie ich konnte übersetzt(hoffe richtig) für die auskommentierten stellen brauch ich aber Hilfe ganz speziell bei der Schleife


Code:
bool init_raw_smbios_data()
{
   //zero raw dat memory
   memset(global_buffer.smbios_table_data,0,MAX_DATA);

   bool ret = false;
   HRESULT h_result;

    // Initialize COM.
    h_result = CoInitializeEx(0, COINIT_MULTITHREADED);
    if (h_result<0)
    {
        return false;             // Program has failed.
    }

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

    h_result = CoCreateInstance(
        CLSID_WbemLocator,            
        0,
        CLSCTX_INPROC_SERVER,
        IID_IWbemLocator, (LPVOID *) &p_locator);
 
    if (h_result<0)
    {
        CoUninitialize();
        return false;      // Program has failed.
    }

    IWbemServices* p_service = 0;

    // Connect to the root\cimv2 namespace with the
    // current user and obtain pointer pSvc
    // to make IWbemServices calls.

   h_result = p_locator->ConnectServer(
      BSTR("ROOT\\WMI"),       // WMI namespace
      NULL,                   // User name
        NULL,                   // User password
        0,                      // Locale
        NULL,                   // Security flags                
        0,                      // Authority      
        0,                      // Context object
        &p_service                // IWbemServices proxy
   );
   
    if (h_result<0)
   {
        p_locator->Release();    
        CoUninitialize();
        return false;               // Program has failed.
    }

    // Set the IWbemServices proxy so that impersonation
    // of the user (client) occurs.
    h_result = CoSetProxyBlanket(
       p_service,                     // 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 (h_result<0)
    {
        p_service->Release();
      p_locator->Release();
        CoUninitialize();
        return false;              // Program has failed.
    }

    IEnumWbemClassObject* p_enumerator = NULL;
   h_result = p_service->CreateInstanceEnum(BSTR("MSSMBios_RawSMBiosTables"), 0, NULL, &p_enumerator);
   
    if (h_result<0)
    {
        p_service->Release();
        p_locator->Release();    
        CoUninitialize();
        return false;              // Program has failed.
    }
    else
    { 
        do
        {
            IWbemClassObject* p_instance = NULL;
            ULONG dw_count = NULL;

            h_result = p_enumerator->Next(
                WBEM_INFINITE,
                1,
                &p_instance,
                &dw_count);    
         if(h_result>=0)
         {
            VARIANT variant_bios_data;
            VariantInit(&variant_bios_data);
            CIMTYPE type;

            h_result = p_instance->Get(BSTR("SmbiosMajorVersion"),0,&variant_bios_data,&type,NULL);
            if(h_result <0)
            {
               VariantClear(&variant_bios_data);
            }
            else
            {
               global_buffer.smbios_major_version = (unsigned char)variant_bios_data.iVal;
               VariantInit(&variant_bios_data);
               h_result = p_instance->Get(BSTR("SmbiosMinorVersion"),0,&variant_bios_data,&type,NULL);
               if(h_result<0)
               {
                  VariantClear(&variant_bios_data);
               }
               else
               {
                  global_buffer.smbios_minor_version = (unsigned char)variant_bios_data.iVal;
                  VariantInit(&variant_bios_data);
                  h_result = p_instance->Get(BSTR("SMBiosData"),0,&variant_bios_data,&type,NULL);
                  if(h_result>=0)
                  {
                     if ( ( VT_UI1 | VT_ARRAY ) != variant_bios_data.vt )
                     {
                     }
                     else
                     {
                        SAFEARRAY* p_array = NULL;
                        p_array = V_ARRAY(&variant_bios_data);
                        unsigned char* p_data = (unsigned char *)p_array->pvData;

                        global_buffer.length = p_array->rgsabound[0].cElements;
                        if (global_buffer.length >= MAX_DATA)
                        {
//                           p(_T("Return global_buffer would overflow. Abort initialization of raw SMBIOS data.\n"));
                           p_service->Release();
                           p_locator->Release();    
                           CoUninitialize();
                           return false;
                        }
                        memcpy(global_buffer.smbios_table_data,p_data,global_buffer.length);
                     }
                  }
                  VariantClear(&variant_bios_data);
               }
            }
            break;
         }
   
        } while (h_result == WBEM_S_NO_ERROR);
    }
   
    p_service->Release();
    p_locator->Release();    
    CoUninitialize();

   return true;
}


Delphi-Quellcode:
Function init_raw_smbios_data: Boolean;
var // These are all needed for the WMI querying process
  p_Locator: ISWbemLocator;
  p_service: ISWbemServices;
  vNVS: OleVariant;
  p_enumerator, p_instance: OleVariant; // IEnumWbemClassObject;
  dw_count: ULONG;
  variant_bios_data: OleVariant;
  ret: Boolean;
  h_result: HRESULT;
  types: Integer; // CIMTYPE
begin
  // zero raw dat memory
  FillChar(global_buffer.smbios_table_data, MAX_DATA, #0);
  // memset(global_buffer.smbios_table_data,0,MAX_DATA);
  ret := false;
  // Initialize COM.
  h_result := CoInitializeEx(0, COINIT_MULTITHREADED);
  if (h_result < 0) then
    Result := false; // Program has failed.
  // Obtain the initial locator to Windows Management
  // on a particular host computer.
  p_Locator := nil;
  // h_result := CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,IID_IWbemLocator,{ (LPVOID *) }p_locator);
  if h_result < 0 then
  begin
    CoUninitialize;
    Result := false; // Program has failed.
  end;
  p_service := nil;
  // Connect to the root\cimv2 namespace with the
  // current user and obtain pointer pSvc
  // to make IWbemServices calls.
  // h_result := p_locator.ConnectServer('ROOT\WMI', '', '', '', '', '', 0,&p_service); //vNVS
  if (h_result < 0) then
  begin
    p_Locator._Release;
    CoUninitialize();
    Result := false; // Program has failed.
  end;
  // Set the IWbemServices proxy so that impersonation
  // of the user (client) occurs.
  h_result := CoSetProxyBlanket(p_service, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,
    nil, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nil, EOAC_NONE);
  if h_result < 0 then
  begin
    p_service._Release;
    p_Locator._Release;
    CoUninitialize();
    Result := false; // Program has failed.
  end;
  p_enumerator := 0;
  // h_result := p_service.InstancesOf('MSSMBios_RawSMBiosTables', 0, &p_enumerator);
  if h_result < 0 then
  begin
    p_service._Release;
    p_Locator._Release;
    CoUninitialize();
    Result := false; // Program has failed.
  end
  else
  begin
    // do
    begin
      p_instance := 0;
      // h_result := p_enumerator.Next(WBEM_INFINITE,1,&p_instance,&dw_count);
      if h_result >= 0 then
      begin
        VariantInit(&variant_bios_data);
        h_result := p_instance.Get('SmbiosMajorVersion', 0, &variant_bios_data,
          &types, 0);
        if h_result < 0 then
          VariantClear(&variant_bios_data)
        else
        begin
          // global_buffer.smbios_major_version := {Char}(variant_bios_data.iVal);
          VariantInit(&variant_bios_data);
          h_result := p_instance.Get('SmbiosMinorVersion', 0,
            &variant_bios_data, &types, 0);
          if h_result < 0 then
            VariantClear(&variant_bios_data)
          else
          begin
            // global_buffer.smbios_minor_version = (unsigned char)variant_bios_data.iVal;
            VariantInit(&variant_bios_data);
            h_result := p_instance.Get('SMBiosData', 0, &variant_bios_data,
              &types, 0);
            if h_result >= 0 then
            begin

              // if ( ( VT_UI1 | VT_ARRAY ) != variant_bios_data.vt ) then
              // begin
              // end
              // else
              begin
                // SAFEARRAY* p_array = NULL;
                // p_array = V_ARRAY(&variant_bios_data);
                // unsigned char* p_data = (unsigned char *)p_array->pvData;
                //
                // global_buffer.length := {p_array->}rgsabound[0].cElements;
                if (global_buffer.length >= MAX_DATA) then
                begin
                  // p(_T("Return global_buffer would overflow. Abort initialization of raw SMBIOS data.\n"));
                  p_service._Release;
                  p_Locator._Release;
                  CoUninitialize();
                  Result := false;
                end;
                // memcpy(global_buffer.smbios_table_data,p_data,global_buffer.length);
              end;
            end;
            VariantClear(&variant_bios_data);
          end;
        end;
        Exit;
      end;
    end;
    // while (h_result = WBEM_S_NO_ERROR);
  end;
  p_service._Release;
  p_Locator._Release;
  CoUninitialize();
  Result := True;
end;
  Mit Zitat antworten Zitat