如何检查CPU是否至少有1.5GHz(每个单核)

问题描述 投票:0回答:1

如何在 InnoSetup 中预先检查 CPU 至少有 1.5GHz(每个单核),以便用户在将我的应用程序安装到他们的设备时不会失望,而且速度太慢?

这段代码应该用 C++ 来完成,但是我如何将其转换为 InnoSetup 的 Pascal?

#include <iostream>
#include <windows.h>
#include <comdef.h>
#include <Wbemidl.h>

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

bool IsCpuAtLeast1_5GHz() {
    HRESULT hres;

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

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

    if (FAILED(hres)) {
        std::cout << "Failed to initialize security. Error code = 0x" 
                  << std::hex << hres << std::endl;
        CoUninitialize();
        return false;
    }

    // Obtain the initial locator to WMI.
    IWbemLocator *pLoc = NULL;

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

    if (FAILED(hres)) {
        std::cout << "Failed to create IWbemLocator object. "
                  << "Error code = 0x" 
                  << std::hex << hres << std::endl;
        CoUninitialize();
        return false;
    }

    // Connect to WMI.
    IWbemServices *pSvc = NULL;
    hres = pLoc->ConnectServer(
         _bstr_t(L"ROOT\\CIMV2"), // WMI namespace
         NULL,                    // User name (NULL = current user)
         NULL,                    // User password (NULL = current user)
         0,                       // Locale             
         NULL,                    // Security flags
         0,                       // Authority        
         0,                       // Context object        
         &pSvc                    // IWbemServices proxy
    );

    if (FAILED(hres)) {
        std::cout << "Could not connect to WMI. Error code = 0x" 
                  << std::hex << hres << std::endl;
        pLoc->Release();     
        CoUninitialize();
        return false;
    }

    // Set security levels on the proxy.
    hres = CoSetProxyBlanket(
       pSvc,                        // Indicates the proxy to set
       RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
       RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
       NULL,                        // Server principal name 
       RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
       RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
       NULL,                        // Client identity
       EOAC_NONE                    // Proxy capabilities 
    );

    if (FAILED(hres)) {
        std::cout << "Could not set proxy blanket. Error code = 0x" 
                  << std::hex << hres << std::endl;
        pSvc->Release();
        pLoc->Release();     
        CoUninitialize();
        return false;
    }

    // Use the IWbemServices pointer to make requests of WMI.
    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"), 
        bstr_t("SELECT MaxClockSpeed FROM Win32_Processor"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        &pEnumerator);

    if (FAILED(hres)) {
        std::cout << "WMI query failed. Error code = 0x" 
                  << std::hex << hres << std::endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return false;
    }

    IWbemClassObject *pclsObj = NULL;
    ULONG uReturn = 0;

    // Get the data from the query.
    while (pEnumerator) {
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);

        if (0 == uReturn) {
            break;
        }

        VARIANT vtProp;
        hr = pclsObj->Get(L"MaxClockSpeed", 0, &vtProp, 0, 0);
        
        // Check if the clock speed is greater than or equal to 1500 MHz.
        if (vtProp.intVal >= 1500) {
            pclsObj->Release();
            pSvc->Release();
            pLoc->Release();
            CoUninitialize();
            return true;
        }

        pclsObj->Release();
    }

    // Cleanup.
    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    CoUninitialize();

    return false;
}

int main() {
    if (IsCpuAtLeast1_5GHz()) {
        std::cout << "CPU is 1.5 GHz or higher." << std::endl;
    } else {
        std::cout << "
inno-setup
1个回答
0
投票

基于我对Is there a way to read the system's information in Inno Setup的回答,您可以使用如下代码:

[Code]

function WbemQuery(WbemServices: Variant; Query: string): Variant;
var
  WbemObjectSet: Variant;
begin
  Result := Null;
  WbemObjectSet := WbemServices.ExecQuery(Query);
  if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
  begin
    Result := WbemObjectSet.ItemIndex(0);
  end;
end;

function InitializeSetup: Boolean;
var
  Query: string;
  WbemLocator, WbemServices: Variant;
  Processor: Variant;
  Message: string;
begin
  Result := True;
  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WbemServices := WbemLocator.ConnectServer('.', 'root\CIMV2');
  
  Query := 'SELECT MaxClockSpeed FROM Win32_Processor';
  Processor := WbemQuery(WbemServices, Query);
  if VarIsNull(Processor) then
  begin
    Log('Cannot retrieve processor information');
  end
    else
  begin
    if Processor.MaxClockSpeed >= 2000 then
    begin
      Log(Format('Processor max clock speed is %d, what is enough', [
        Processor.MaxClockSpeed]));
    end
      else
    begin
      Message :=
        'Your processor max clock speed is %.2f GHz, ' +
          'what is not enough for this application. '#13#13 +
        'Do you really want to continue with installation?';
      Message := Format(Message, [Extended(Processor.MaxClockSpeed) / 1000]);
      if MsgBox(Message, mbConfirmation, MB_OKCANCEL) = IDCANCEL then
        Result := False;
    end;
  end;
end;
© www.soinside.com 2019 - 2024. All rights reserved.