我有一个连接到 Windows 计算机的自定义硬件设备。我想将该设备的静态信息和动态数据提供给使用WMI查询的计算机的其他服务。
根据我的研究,我发现我必须写一个 WMI provider 。我当前的软件应用程序使用 JPOS 与硬件连接。因此我必须将 WMI 连接到 Java 应用程序。
我见过C#和 c++实现此任务的示例。我目前的理解是编写一个C++ wmi提供程序并使用JNI集成到我当前的应用程序中。我已经看到了 JNA 用于 query using wmi 的更多示例。然而,我的研究并没有产生任何有关使用 JNA 编写提供程序的有效信息。
编写 C++ 并通过 JNI 集成是处理这种情况的最佳方法吗?或者有什么更好的解决办法吗?
最佳答案
以下是我目前如何解决此问题的一些提示,供任何想要尝试的人使用。
<强>1。创建自定义 wmi 类。
Windows wbemtest.exe 工具是您的 friend 。这个工具可以拯救你的生命,因为它可以生成新的 wmi 类并编辑它们。当以管理权限打开时,它可以生成自定义wmi类、添加属性和修改。
或者可以编写 .mof 文件来创建自定义类。 .mof 文件示例如下。
#pragma namespace("\\\\.\\Root\\Cimv2")
class MyTestClass
{
[Key] uint32 KeyProperty = 1;
string Version = "1.1.1";
};
有关运行此 .mof 文件的信息可以在 here 中找到。 .
<强>2。添加属性
虽然 webmtester 和 .mof 方法可用于添加属性,但我发现 powershell commandlet 很有用。一组强大的 powershell commandlet 是 here作者:斯蒂芬·范·古利克。
<强>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 root\CIMV2 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”的 unsigned int 8 变量的值
<强>5。下一步
我的下一个选择是通过 JNA 将 C++ 应用程序连接到主 Java 应用程序。
关于java - 使用 JAVA 在 Windows 中为自定义硬件编写 WMI 提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57567804/