该代码展示了如何使用WMI (Windows Management Instrumentation) API与BIOS交互。
使用GCC代码编译在Windows中的运行程序以及需要的头文件及库文件;在VC中编译比较简单,但在GCC等编译的环境下,需要搭配的库文件及头文件尤为重要;以下是实例代码,具体的实现看你BIOS中的MOF是怎么定义的;如果用C#调用的话,可能就几行代码就搞定了,C语言嘛,细节多点;
#include <windows.h>
#include <stdio.h>
#include <wbemidl.h>
#include <comdef.h>
//
// Here is the lib for gcc you can assign it in compiler
// Linker = -lwbemuuid -lole32 -loleaut32
//
//#pragma comment(lib, "wbemuuid.lib")
//#pragma comment(lib, "ole32.lib")
//#pragma comment(lib, "oleaut32.lib")
int main(int argc, char **argv)
{
HRESULT hres;
// Initialize COM
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
printf("Failed to initialize COM library. Error code = 0x%08X\n", hres);
return 1;
}
// Set general COM security levels
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))
{
printf("Failed to initialize security. Error code = 0x%08X\n", hres);
CoUninitialize();
return 1;
}
// Obtain the initial locator to WMI
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID *)&pLoc
);
if (FAILED(hres))
{
printf("Failed to create IWbemLocator object. Error code = 0x%08X\n", hres);
CoUninitialize();
return 1;
}
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\WMI"), // WMI namespace
NULL, // User name
NULL, // User password
0, // Locale
0, // Security flags
0, // Authority
0, // Context object
&pSvc
);
if (FAILED(hres))
{
printf("Could not connect to WMI namespace. Error code = 0x%08X\n", hres);
pLoc->Release();
CoUninitialize();
return 1;
}
printf("Connected to ROOT\\WMI WMI namespace\n");
// 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))
{
printf("Could not set proxy blanket. Error code = 0x%08X\n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// Prepare the method call
IWbemClassObject *pClass = NULL;
hres = pSvc->GetObject(_bstr_t(L"TestClass"), 0, NULL, &pClass, NULL);
if (FAILED(hres))
{
printf("Could not get TestClass class. Error code = 0x%08X\n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// Get the method
IWbemClassObject *pInParamsDefinition = NULL;
hres = pClass->GetMethod(L"TestMethod", 0, &pInParamsDefinition, NULL);
if (FAILED(hres))
{
printf("Could not get TestMethod method. Error code = 0x%08X\n", hres);
pClass->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// Create instance of input parameters
IWbemClassObject *pInParams = NULL;
hres = pInParamsDefinition->SpawnInstance(0, &pInParams);
if (FAILED(hres))
{
printf("Could not spawn instance of input parameters. Error code = 0x%08X\n", hres);
pInParamsDefinition->Release();
pClass->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// Prepare the Data
ULONG64 a= 0x1234;
WCHAR buffer[8];
swprintf(buffer, 8, L"%I64u", a); // 将数值转换为字符串
// Set the input parameter
VARIANT var;
VariantInit(&var);
var.vt = VT_BSTR;
var.bstrVal = SysAllocString(buffer);
hres = pInParams->Put(L"Data", 0, &var, 0);
VariantClear(&var);
if (FAILED(hres))
{
printf("Could not set Data parameter. Error code = 0x%08X\n", hres);
pInParams->Release();
pInParamsDefinition->Release();
pClass->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// Execute the method
IWbemClassObject *pOutParams = NULL;
hres = pSvc->ExecMethod(
_bstr_t(L"TestClass.InstanceName='ACPI\\PNP0C14\\1_1'"),
_bstr_t(L"TestMethod"),
0,
NULL,
pInParams,
&pOutParams,
NULL
);
if (FAILED(hres))
{
printf("Could not execute method. Error code = 0x%08X\n", hres);
pInParams->Release();
pInParamsDefinition->Release();
pClass->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// Get the return value
VARIANT varReturn;
VariantInit(&varReturn);
hres = pOutParams->Get(L"Return", 0, &varReturn, NULL, 0);
if (SUCCEEDED(hres))
{
printf("Out Parameters:\n");
printf("Return: %x\n", varReturn.lVal);
}
else
{
printf("Could not get Return value. Error code = 0x%08X\n", hres);
}
// Clean up
VariantClear(&varReturn);
if (pOutParams) pOutParams->Release();
if (pInParams) pInParams->Release();
if (pInParamsDefinition) pInParamsDefinition->Release();
if (pClass) pClass->Release();
if (pSvc) pSvc->Release();
if (pLoc) pLoc->Release();
CoUninitialize();
return 0;
}