如何在 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 中读取系统信息问题的回答,您可以使用IDispatch COM 自动化代码,如下所示: