JNI - WMI 连接错误
JNI - WMI connection error
我有一个java Web应用程序来收集本地和远程Windows机器的一些数据。我正在使用 wmi 连接连接到机器。我使用tomcat作为Web服务器。对于 wmi 连接,我编写了 c++ 代码并使用 JNI 连接 java 和 c++。当我启动服务器并输入登录详细信息时,tomcat 崩溃了。
日志文件中的错误是..
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffd1e6e2931, pid=3940, tid=0x0000000000000a6c
#
# JRE version: Java(TM) SE Runtime Environment (8.0_162-b12) (build 1.8.0_162-b12)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.162-b12 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [Verify_.dll+0x2931]
#
我正在使用雄猫 8.5.28 和jdk1.8.0_162
这是我的java代码:Verify_.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Verify_ extends HttpServlet{
public native int connect();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
System.loadLibrary("Verify_");
Verify_ verify = new Verify_();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String ServerName = request.getParameter("servername");
String UserName = request.getParameter("username");
String Password = request.getParameter("password");
int status = verify.connect();
if(status == 0)
out.println("Connected to " + ServerName);
else if(status == 1)
out.println("failed to initialize CoInitializeEx");
else if(status == 2)
out.println("failed to initialize CoInitializeSecurity");
else if(status == 3)
out.println("failed to initialize CoCreateInstance");
else if(status == 4)
out.println("failed to connect to Server");
else
out.println(status);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
doGet(request,response);
}}
这是 JNI 接口代码:Verify_.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Verify_ */
#ifndef _Included_Verify_
#define _Included_Verify_
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Verify_
* Method: connect
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Verify_1_connect
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
这是C++代码:Verify_.cpp
#include <iostream>
#include <wbemidl.h>
#include <conio.h>
#include <windows.h>
#include <comdef.h>
#include <wincred.h>
#include <string.h>
#include "Verify_.h"
#pragma comment(lib, "credui.lib")
#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "Ole32.lib")
#define _WIN32_DCOM
JNIEXPORT jint JNICALL Java_Verify_1_connect (JNIEnv *, jobject){
BSTR pszRoot,pszUserName,pszPassword;
wcscpy(pszRoot, BSTR(L"\\172.21.111.250\ROOT\cimv2"));
wcscpy(pszUserName, BSTR(L"Administrator"));
wcscpy(pszPassword, BSTR(L"qwerty1233"));
HRESULT hr;
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr)) {
return 1;
}
hr = CoInitializeSecurity(
NULL, // Security descriptor
-1, // COM negotiates authentication service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication level for proxies
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
NULL, // Authentication info
EOAC_NONE, // Additional capabilities of the client or server
NULL); // Reserved
if (FAILED(hr)) {
CoUninitialize();
return 2;
}
IWbemLocator *pLoc = 0;
hr = CoCreateInstance(CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hr)){
CoUninitialize();
return 3;
}
IWbemServices *pSvc = NULL;
// Connect to the rootdefault namespace with the current user.
hr = pLoc->ConnectServer(
pszRoot, //namespace
pszUserName, // User name
pszPassword, // User password
NULL, // Locale
NULL, // Security flags
NULL, // Authority
NULL, // Context object
&pSvc); // IWbemServices proxy
if (FAILED(hr)){
pLoc->Release();
CoUninitialize();
return 4;
}
// pSvc->Release();
// pLoc->Release();
CoUninitialize();
return 0;
}
我正在尝试过去 5 天。请帮助我。 如果有其他方法告诉我..
提前致谢
0xc0000005
-> 访问冲突,主要是因为使用了 NULL 指针。
BSTR pszRoot,pszUserName,pszPassword;
wcscpy(pszRoot, BSTR(L"\\172.21.111.250\ROOT\cimv2"));
使用 wcscpy 时,您必须分配内存。因此,wcscopy 使用无效(随机(地址(在调试模式下:使用 NULL(调用。
我不确定你是否正确处理了 BSTR(我不知道 BSTR(。参见 BSTR 的定义:
typedef OLECHAR *BSTR;
为什么不直接分配?
pszRoot = BSTR(L"\\172.21.111.250\ROOT\cimv2");
感谢拉尔夫·埃尔特
我更改了这部分
BSTR pszRoot,pszUserName,pszPassword;
pszRoot = ::SysAllocString(L"\\172.21.111.250\ROOT\cimv2");
pszUserName = ::SysAllocString(L"Administrator");
pszPassword = ::SysAllocString(L"qwerty1233");
相关文章:
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 使用 bfs 解决连接组件问题时得到错误的答案
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- CMake 错误"源似乎不包含 CMakeLists.txt",路径/库连接问题
- 当客户端在 write() 期间终止连接时,由对等套接字错误重置连接
- 从 Linux OMI (omicli) 到 Windows WMI 的连接失败,并出现与 DMTF 相关的错误
- C++ 中连接 4 游戏的输入逻辑错误
- Eclipse:错误:初始化模式失败(无法连接到目标)
- C++所有 AT 命令的短信返回错误 |PC<->通过蓝牙和winsocket的移动连接
- 如何修复数据库连接的C 程序运行时间错误(0xc0000005)
- Mongocxx 连接错误
- 连接到套接字失败 - 错误编号 88 (CPP)
- 尝试重新连接到服务器时获取错误提升 asio 连接超时
- 字符和 int 连接何时会导致 seg 错误?
- 在 ZeroMQ 中绑定订阅者套接字并连接发布者套接字会在代码运行时给出错误.为什么
- JNI - WMI 连接错误
- boost :: Asio连接错误的界面
- 如何在两个设备之间与TCP通信时修复错误"连接被拒绝"