2015-11-18 70 views
8

我需要從java調用C#函數,併爲此我創建了以下內容。 我有一個創建Java頭文件Authenticator.h,這裏是代碼:如何從java調用C#函數

#include <jni.h> 
/* Header for class Authenticator */ 

#ifndef _Included_Authenticator 
#define _Included_Authenticator 
#ifdef __cplusplus 
extern "C" { 
#endif 
/* 
* Class:  Authenticator 
* Method: authenticate 
* Signature: (Ljava/lang/String;Ljava/lang/String;)Z 
*/ 
JNIEXPORT jboolean JNICALL Java_Authenticator_authenticate 
    (JNIEnv *, jobject, jstring, jstring); 

#ifdef __cplusplus 
} 
#endif 
#endif 

我然後創建一個身份驗證

namespace SharpAuthenticator 
{ 
    public class Authenticator 
    { 



     public bool Authenticate(String username,String password) 
     { 
      return username == "user" && password == "login"; 
     } 

    } 
} 

然後我試圖調用C#功能的C#功能從C++(項目創建一個DLL)使用下面的代碼;

String^ toString(const char *str) 
{ 
    int len = (int)strlen(str); 
    array<unsigned char>^ a = gcnew array<unsigned char>(len); 
    int i = 0; 
    while (i < len) 
    { 
     a[i] = str[i]; 
     i++; 
    } 
    return Encoding::UTF8->GetString(a); 
} 
bool authenticate(const char *username, const char *password) 
{ 
    SharpAuthenticator::Authenticator::Authenticate(toString(username), toString(password)); 

} 
JNIEXPORT jboolean JNICALL Java_Authenticator_authenticate 
(JNIEnv *env, jobject c, jstring name, jstring pass) 
{ 
    jboolean result; 

    jboolean isCopyUsername; 
    const char * username = env->GetStringUTFChars(name, &isCopyUsername); 
    jboolean isCopypassword; 
    const char * password = env->GetStringUTFChars(pass, &isCopypassword); 

    result = authenticate(username, password); 
    env->ReleaseStringUTFChars(name, username); 
    env->ReleaseStringUTFChars(pass, password); 
    return result; 
} 

並finnally創建一個dll,我需要從java調用。 DLL被創建,我在Java中加載它,但我得到這個錯誤日誌在Java。我可能會失去什麼。

# 
# A fatal error has been detected by the Java Runtime Environment: 
# 
# Internal Error (0xe0434352), pid=9708, tid=7756 
# 
# JRE version: 7.0-b147 
# Java VM: Java HotSpot(TM) Client VM (21.0-b17 mixed mode, sharing windows-x86) 
# Problematic frame: 
# C [KERNELBASE.dll+0x812f] 
# 
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows 
+1

檢查:http://stackoverflow.com/questions/8181344/calling-c-sharp-method-within-a-java-program – phantom

+0

@phantom我沒有使用多少知識C++。可以幫助我如何從C++調用C#函數 – MorganM

+0

我會嘗試,但不是現在可能在2小時內。 – phantom

回答

2

首先讓我們創建一個C#文件是這樣的:

using System; 
public class Test{ 
    public Test(){} 
    public String ping(){ 
    return "C# is here."; 
    } 
} 

然後用下面的命令編譯此:

csc.exe /target:module Test.cs 

您可以在安裝.NET框架的路徑csc.exe 。之後,創建的Java文件:

public class Test{ 
    public native String ping(); 
    public static void main(String[] args){ 
    System.load("/path/to/dll"); 
    System.out.println("Java is running."); 
    Test t = new Test(); 
    System.out.println("Trying to catch C# " + r.ping()); 
    } 
} 

javac Test.java這生成一個Test.class

javah -jni Test這會生成一個Test.h文件,該文件將包含在 C++代碼中。

之後,我們需要創建我們的C++文件:

#include "stdafx.h" 
#include "JAVA/Test.h" 
#include "MCPP/Test.h" 
#pragma once 
#using <mscorlib.dll> 
#using "Test.netmodule" 
JNIEXPORT jstring JNICALL Java_Test_ping(JNIEnv *env, jobject obj){ 
    Test^ t = gcnew Test(); 
    String^ ping = t->ping(); 
    char* str = static_cast<char*>((System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(ping)).ToPointer()); 

    char cap[128]; 
    strcpy_s(cap, str); 

    return env->NewStringUTF(cap); 
} 

最後:

c:\>java Test 

我希望這可以幫助你。在Java中使用C#函數的基本示例。

來源: https://www.quora.com/How-common-is-the-problem-of-calling-C-methods-from-Java-Do-many-developers-come-across-such-necessity

+0

我已經創建了C#和java文件,並且編譯我的問題是C++的這一部分,我在哪裏包含它#using「Test」。netmodule」我使用的Visual Studio 2013 – MorganM

+0

我仍然收到此錯誤由Java運行時環境已檢測 #致命錯誤: # #內部錯誤(0xe0434352),PID = 5800 TID = 5792 # #JRE版本:7.0-B147 #Java虛擬機:爪哇的HotSpot(TM)客戶機VM(21.0-B17混合模式下,共享的窗口86) #有問題的幀: #C [KERNELBASE.dll + 0x812f] # #未能寫入核心轉儲Minidumps在客戶端版本的Windows上未默認啓用 # #包含更多信息的錯誤報告文件另存爲: – MorganM