如何使用C应用获得WMI数据? [英] How to obtain data from WMI using a C Application?

查看:124
本文介绍了如何使用C应用获得WMI数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个纯C应用程序发出的IOCTL调用我的适配器驱动程序和显示的信息,这是使用Visual Studio的开发者不过5编(非管理code)...我需要从不过得到一些信息使用WMI我的适配器....我的谷歌搜索显示的努力,我会需要使用COM实现与WMI的任何形式的通信或C#在.NET应用程序编写C ++应用程序
A),是真的吗?没有工作围绕我的C应用程序?
b)若上述是真实的,什么是我需要做我的项目/ WP /工作区设置的最低水平变化?

I have a pure C application that issues IOCTL calls to my adapter driver and displays info ,this is however compiled using Visual Developer Studio 5(non-managed code) ... I need to get some info however from my adapter using WMI .... My googling efforts show that i would need to write a C++ Application using COM to achieve any form of communication with wMI or a C# with .NET app a) Is that really the case? NO work around for my C application? b) If above is true,what are the minimum level changes that i would need to do my project/wp /workspace settings?

谢谢
索姆

推荐答案

您可以调用从C COM的语法比C ++的稍差友好的,但它的作品。 COM最初设计为从C或C ++的工作,和原生C语言支持包含在COM和WMI的头文件。这将是长期的,但...你的程序将负责分配所有必需的对象,检查每个错误情况和每一个COM调用,并释放它初始化的对象。

You can invoke COM from C. The syntax is somewhat less friendly than that of C++, but it works. COM was initially designed to work from either C or C++, and native C language support is included in COM and WMI header files. It will be long though... your program will be responsible for allocating all the necessary objects, checking for error conditions from each and every COM call, and for releasing the objects it instantiated.

在使用记住用C ++编写文档,转换表格的COM调用:

When using documentation written with C++ in mind, convert COM calls of the form:

pSomething->Method(arg1, ...); // C++

pSomething->lpVtbl->Method(pSomething, arg1, ...); // C

下面是我能得到真正拉开距离WMI的一些信息C code的最短的那块。如果成功的话,它应该列出你的计算机上的处理器,以MHz的时钟频率一起。该计划负责处理它分配资源,但它(你应该看看这些HR值继续每个步骤之前)没有错误检查任何责任。

Below is the shortest piece of C code I could get to actually pull some information from WMI. If successful, it should list the processors on your computer, along with their clock frequency in MHz. The program takes care of disposing resources it allocates, but it does no error checking whatsoever (you should look at those hr values before continuing each step).

这是一个Visual Studio 2008的Win32控制台应用程序与重命名为.c扩展主文件,而额外stdafx文件中删除。为了让程序链接,确保包括在项目属性wbemuuid.lib,在配置属性/链接器/输入/附加依赖。它在我的Vista框成功运行。

This is a visual studio 2008 "Win32 Console Application" with the main file renamed to a .c extension, and the extra stdafx files removed. To get the program to link, make sure to include wbemuuid.lib in the project properties, under Configuration Properties/Linker/Input/Additional Dependencies. It ran successfully on my Vista box.

#define _WIN32_WINNT 0x0400
#define _WIN32_DCOM

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <wbemidl.h>

void _tmain(int argc, _TCHAR* argv[])
{
    // result code from COM calls
    HRESULT hr = 0;

    // COM interface pointers
    IWbemLocator         *locator  = NULL;
    IWbemServices        *services = NULL;
    IEnumWbemClassObject *results  = NULL;

    // BSTR strings we'll use (http://msdn.microsoft.com/en-us/library/ms221069.aspx)
    BSTR resource = SysAllocString(L"ROOT\\CIMV2");
    BSTR language = SysAllocString(L"WQL");
    BSTR query    = SysAllocString(L"SELECT * FROM Win32_Processor");

    // initialize COM
    hr = CoInitializeEx(0, COINIT_MULTITHREADED);
    hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);

    // connect to WMI
    hr = CoCreateInstance(&CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, &IID_IWbemLocator, (LPVOID *) &locator);
    hr = locator->lpVtbl->ConnectServer(locator, resource, NULL, NULL, NULL, 0, NULL, NULL, &services);

    // issue a WMI query
    hr = services->lpVtbl->ExecQuery(services, language, query, WBEM_FLAG_BIDIRECTIONAL, NULL, &results);

    // list the query results
    if (results != NULL) {
        IWbemClassObject *result = NULL;
        ULONG returnedCount = 0;

        // enumerate the retrieved objects
        while((hr = results->lpVtbl->Next(results, WBEM_INFINITE, 1, &result, &returnedCount)) == S_OK) {
            VARIANT name;
            VARIANT speed;

            // obtain the desired properties of the next result and print them out
            hr = result->lpVtbl->Get(result, L"Name", 0, &name, 0, 0);
            hr = result->lpVtbl->Get(result, L"MaxClockSpeed", 0, &speed, 0, 0);
            wprintf(L"%s, %dMHz\r\n", name.bstrVal, speed.intVal);

            // release the current result object
            result->lpVtbl->Release(result);
        }
    }

    // release WMI COM interfaces
    results->lpVtbl->Release(results);
    services->lpVtbl->Release(services);
    locator->lpVtbl->Release(locator);

    // unwind everything else we've allocated
    CoUninitialize();

    SysFreeString(query);
    SysFreeString(language);
    SysFreeString(resource);
}

这篇关于如何使用C应用获得WMI数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆