C语言实现BIOS定义的WMI调用
该代码展示了如何使用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 COMhres = 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 levelshres = CoInitializeSecurity(NULL,-1, // COM authenticationNULL, // Authentication servicesNULL, // ReservedRPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation NULL, // Authentication infoEOAC_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 WMIIWbemLocator *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 methodIWbemServices *pSvc = NULL;hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\WMI"), // WMI namespaceNULL, // User nameNULL, // User password0, // Locale0, // Security flags0, // Authority0, // 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 proxyhres = CoSetProxyBlanket(pSvc, // Indicates the proxy to setRPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxxRPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxxNULL, // Server principal name RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxxNULL, // client identityEOAC_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 callIWbemClassObject *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 methodIWbemClassObject *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 parametersIWbemClassObject *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 DataULONG64 a= 0x1234;WCHAR buffer[8];swprintf(buffer, 8, L"%I64u", a); // 将数值转换为字符串// Set the input parameterVARIANT 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 methodIWbemClassObject *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 valueVARIANT 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 upVariantClear(&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;
}