当前位置: 首页 > news >正文

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;
}

http://www.dtcms.com/a/295314.html

相关文章:

  • NumPy 线性代数
  • 分布式推客系统开发全解:微服务拆分、佣金结算与风控设计
  • Sklearn 机器学习 数值标准化
  • 变量和函数底层工作原理
  • Mysql常用武功招式
  • 大脑各脑区功能解析:从痛觉处理到动作执行的协作机制
  • runc源码解读(一)——runc create
  • 技术赋能与体验升级:高端网站建设的核心要义
  • 【VSCode】复制到下一行快捷键
  • SparkSQL 子查询 IN/NOT IN 对 NULL 值的处理
  • 【分布式锁】什么是分布式锁?分布式锁的作用?
  • Windows计算器项目全流程案例:从需求到架构到实现
  • 宝塔通过docker部署JupyterHub指南【常见错误处理】
  • 深入解析文件操作(下)- 文件的(顺序/随机)读写,文件缓冲区,更新文件
  • 【AI】Jupyterlab中数据集的位置和程序和Pycharm中的区别
  • 20-ospf技术
  • MIT线性代数01_方程组的几何解释
  • 绿色转向的时代红利:创新新材如何以技术与标准主导全球铝业低碳重构
  • 旅行短视频模糊的常见原因及应对方法
  • 内网穿透:打破网络限制的利器,内外网概念、穿透原理、实际操作方法步骤
  • 【LeetCode 热题 100】39. 组合总和——(解法一)选或不选
  • 【物联网】基于树莓派的物联网开发【16】——树莓派GPIO控制LED灯实验
  • 暑期算法训练.7
  • 97.2%灵敏度,桐树基因MSI NGS 2249 Panel——低肿瘤含量MSI检测的王者
  • CIRL:因果启发的表征学习框架——从域泛化到奖励分解的因果革命
  • LLM:Day1
  • 【Linux】linux基础开发工具(一) 软件包管理器yum、编辑器vim使用与相关命令
  • Web前端:JavaScript some()迭代方法
  • 前端如何利用多通道发布(MCP)打造高效AI驱动应用?
  • Hadoop磁盘I/O瓶颈的监控与优化:从iostat指标到JBOD vs RAID的深度解析