使用 JAVA 为窗口中的自定义硬件编写 WMI 提供程序



我有一个连接到Windows计算机的自定义硬件设备。我想将此设备的静态信息和动态数据提供给使用 WMI 查询的计算机的其他服务。

根据我的研究,我发现我必须编写一个 WMI 提供程序。我当前的软件应用程序使用JPOS来连接硬件。因此,我必须将WMI连接到Java应用程序。

我已经看过 C# 和 C++ 示例来完成这项任务。我目前的理解是编写一个C++ wmi 提供程序并使用 JNI 集成到我当前的应用程序中。我已经看到了更多使用 JNA 使用 WMI 进行查询的示例。然而,我的研究并没有产生任何关于用JNA编写提供者的有用信息。

编写C++并通过 JNI 集成是处理这种情况的最佳方式吗?还是有更好的解决方案?

以下是我目前如何为任何想尝试的人解决这个问题的一些提示。

1. 创建自定义 WMI 类。

Windows wbemtest.exe工具是你的朋友。此工具可以挽救您的生命,因为它可以生成新的 wmi 类并对其进行编辑。使用管理权限打开时,它可以生成自定义 wmi 类、添加属性和修改。

或者,可以编写 .mof 文件来创建自定义类。.mof 文件的示例如下所示。

#pragma namespace("\\.\Root\Cimv2")
class MyTestClass
{
[Key] uint32 KeyProperty = 1;
string Version = "1.1.1";
};

有关运行此 .mof 文件的信息可在此处找到。

2. 添加属性

虽然webmtester和.mof方法可以用来添加属性,但我发现Powershell命令很有用。一组强大的powershell命令由Stephane van Gulick提供。

3. 以编程方式检索属性

以编程方式检索属性的示例 C++ 程序如下所示。

// PropertyRetrieve.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// WMI query to list all properties and values of the root/CIMV2:Detagger class.
// This C++ code was generated using the WMI Code Generator, Version 10.0.13.0
// https://www.robvanderwoude.com/wmigen.php
//
// The generated code was derived from sample code provided by Microsoft:
// https://msdn.microsoft.com/en-us/library/aa390423(v=vs.85).aspx
// The sample code was modified to display multiple properties.
// Most of the original comments from the sample code were left intact.
// Limited testing has been done in Microsoft Visual C++ 2010 Express Edition.
#define _WIN32_DCOM
#include <iostream>
#include <iomanip>
#include <string>
#include <comdef.h>
#include <Wbemidl.h>
using namespace std;
#pragma comment( lib, "wbemuuid.lib" )
HRESULT hr;
IWbemClassObject *pclsObj = NULL;
void DisplayProperty(LPCWSTR propertyname)
{
VARIANT vtProperty;
VariantInit(&vtProperty);
try
{
hr = pclsObj->Get(propertyname, 0, &vtProperty, 0, 0);
if (vtProperty.vt == VT_DISPATCH)
{
wcout << vtProperty.pdispVal;
}
else if (vtProperty.vt == VT_BSTR)
{
wcout << vtProperty.bstrVal;
}
else if (vtProperty.vt == VT_UI1)
{
wcout << vtProperty.uiVal;
}
else if (vtProperty.vt == VT_EMPTY)
{
wcout << L"[NULL]";
}
}
catch (...)
{
wcout.clear();
wcout << resetiosflags(std::ios::showbase);
}
VariantClear(&vtProperty);
}
int main(int argc, char **argv)
{
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cerr << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// 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))
{
cerr << "Failed to initialize security. Error code = 0x" << hex << hres << endl;
CoUninitialize();
return 1;                    // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *)&pLoc
);
if (FAILED(hres))
{
cerr << "Failed to create IWbemLocator object. Err code = 0x" << hex << hres << endl;
CoUninitialize();
return 1;                 // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Connect to the rootCIMV2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"root\CIMV2"), // Object path of WMI namespace
NULL,                      // User name. NULL = current user
NULL,                      // User password. NULL = current
0,                         // Locale. NULL indicates current
NULL,                      // Security flags.
0,                         // Authority (for example, Kerberos)
0,                         // Context object
&pSvc                      // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
cerr << "Could not connect. Error code = 0x" << hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1;                // Program has failed.
}
cerr << "Connected to root\CIMV2 WMI namespace" << endl;
// Step 5: --------------------------------------------------
// 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))
{
cerr << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;               // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT Name,TestValue,Version FROM Detagger"),
NULL,
NULL,
&pEnumerator
);
if (FAILED(hres))
{
cerr << "Query of Detagger class failed. Error code = 0x" << hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;               // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
ULONG uReturn = 0;
while (pEnumerator)
{
hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (hr != 0)
{
break;
}
wcout << "Name      : ";
DisplayProperty((LPCWSTR)L"Name");
wcout << endl;
wcout << "TestValue : ";
DisplayProperty((LPCWSTR)L"TestValue");
wcout << endl;
wcout << "Version   : ";
DisplayProperty((LPCWSTR)L"Version");
wcout << endl;
pclsObj->Release();
}
// Cleanup
// =======
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
cout << "Press anykey to exit.";
cin.ignore();
cin.get();
return 0;   // Program successfully completed.
}

4. 以编程方式修改属性

以编程方式修改属性的示例 C++ 代码如下所示。此选项需要管理权限。

_variant_t  var2(L"15");

IWbemClassObject *detaggerClass = NULL;
HRESULT dflkj = pSvc->GetObjectW(L"Detagger", 0, NULL, &detaggerClass, NULL);
IWbemClassObject *detaggerInstance = NULL;
dflkj = detaggerClass->SpawnInstance(0, &detaggerInstance);
detaggerInstance->Put(L"TestValue", 0, &var2, CIM_UINT8)
|| Fail("Put failed for 'TestValue'");
HRESULT er = pSvc->PutInstance(detaggerInstance, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL);

在这里,我修改了名为"TestValue"的无符号int 8变量的值

5. 下一步

我的下一个选择是通过 JNA 将 c++ 应用程序连接到主 Java 应用程序。

最新更新