繁体   English   中英

通过Java调用非托管C ++

[英]Calling unmanaged C++ via Java

我需要一个Java应用程序来调用非托管C ++。 我将Visual Studio 2008 Redist路径中的MSVCR90.dll手动复制到了vmware的Windows Server数据中心。

这是我得到的错误:

Java运行时环境检测到致命错误:

  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x73b4ae7a, pid=1108, tid=2272 JRE version: 6.0_38-b05 Java VM: Java HotSpot(TM) Client VM (20.13-b02 mixed mode, sharing windows-x86 ) Problematic frame: C [MSVCR90.dll+0x3ae7a] An error report file with more information is saved as: ...\\hs_err_pid1108.log If you would like to submit a bug report, please visit: http://java.sun.com/webapps/bugreport/crash.jsp The crash happened outside the Java Virtual Machine in native code. See problematic frame for where to report the bug. 

这是C ++代码:

 #include "stdafx.h"

 #include <stdio.h>

 #include "CCCheckString.h"

 #include <vector>

 #include <String>

 using namespace std;
 #include "jobHandler.h"

JNIEXPORT jbolean JNICALL Java_CCCheckString_Login
  (JNIEnv *env, jobject object, jstring host, jstring UserName, jstring Domain, jstring Password)
{
    bool result;

    jobHandler *handler = new jobHandler(); 

     const char *hostStr = (env)->GetStringUTFChars(host, NULL);
     string hostS(hostStr);
     const char *UserNameStr = (env)->GetStringUTFChars(UserName, NULL);
     string UserNameS(UserNameStr);
     const char *DomainStr = (env)->GetStringUTFChars(Domain, NULL);
     string DomainS(DomainStr);
     const char *PasswordStr = (env)->GetStringUTFChars(Password, NULL);
     string PasswordS(PasswordStr); 

         //if comment this line everthing is okey 
     **result = handler->Login(hostS,UserNameS,DomainS,PasswordS);**

    (env)->ReleaseStringUTFChars(host, NULL);
    (env)->ReleaseStringUTFChars(UserName, NULL);
    (env)->ReleaseStringUTFChars(Domain, NULL);
    (env)->ReleaseStringUTFChars(Password, NULL);


    delete handler;

    return result;

}

以下是Java中的处理代码:

CCCheckString ccCheckString = new CCCheckString();

 result=ccCheckString.Login("xxx", "xxx", "xx", "xxx");

我该如何解决错误?

我解决了

问题是Login()接受std :: string,但是我们无法发送它

我们重组了Login(),它必须使用const char []

例如:

bool ClassName::Login(std::string host,std::string userName,std::string userDomain,std::string userPassword)
{ //Orginal Code Here ....}

bool ClassName::Login(const char host[],const char userName[],const char userDomain[],const char userPassword[])
{
std::string strHost(host);
std::string strUserName(userName);
std::string strUserDomain(userDomain);
std::string strUserPassword(userPassword);
return Login(strHost,strUserName,strUserDomain,strUserPassword);

}

一切顺利。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM