JNI - WMI Connection Error

I have a java Web application to collect some data from local and remote windows machine. I am using wmi connection to connect to machine. I use tomcat as Web server. For wmi connection i wrote c++ code and connect java and c++ using JNI. When i start the server and enter login details, tomcat crashes.

In log file error is..

# 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]

I am using tomcat 8.5.28 and jdk1.8.0_162

Here is my java code:

import javax.servlet.*;
import javax.servlet.http.*;

public class Verify_ extends HttpServlet{

public native int connect();

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Verify_ verify = new Verify_();


    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");


public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

Here is JNI interface code: 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" {
 * Class:     Verify_
 * Method:    connect
 * Signature: ()I
JNIEXPORT jint JNICALL Java_Verify_1_connect
  (JNIEnv *, jobject);

#ifdef __cplusplus

Here is C++ code: 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"\\\\\\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)) {
        return 2;

    IWbemLocator *pLoc = 0;
    hr = CoCreateInstance(CLSID_WbemLocator, 0, 
        CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
    if (FAILED(hr)){
        return 3;

    IWbemServices *pSvc = NULL;
    // Connect to the root\default 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)){
        return 4;
//  pSvc->Release();
//  pLoc->Release();
    return 0;

i am trying for past 5 days. Pls help me. If there is any other way tell me..

Thanks in advance



thanks Ralph Erdt

i changed this part

BSTR pszRoot,pszUserName,pszPassword;
pszRoot = ::SysAllocString(L"\\\\\\ROOT\\cimv2");
pszUserName = ::SysAllocString(L"Administrator");
pszPassword = ::SysAllocString(L"qwerty1233");