2015-11-18 48 views
8

Ho bisogno di chiamare la funzione C# da Java e per questo ho creato quanto segue. Ho un creare un file di intestazione java Authenticator.h, ecco il codice:Come chiamare la funzione C# da java

#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 

ho quindi creare una funzione di C# che autenticano

namespace SharpAuthenticator 
{ 
    public class Authenticator 
    { 



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

    } 
} 

Poi sto cercando di chiamare la funzione C# da C++ (progetto per creare una dll) usando il codice qui sotto;

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; 
} 

E infine creare una DLL che ho bisogno di chiamare da java. La DLL è stata creata e io la carica bene in java ma ottengo questo log degli errori in java. Cosa potrei essere mancante.

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

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

+0

@phantom Non ho molta conoscenza utilizzando C++. potrebbe aiutarmi come posso chiamare la funzione C# da C++ – MorganM

+0

proverò ma non ora forse in 2 ore. – phantom

risposta

2

Prima di tutto permette di creare un file C# in questo modo:

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

quindi compilare questo con il comando di seguito:

csc.exe /target:module Test.cs 

Potete trovare csc.exe nel percorso di installazione di .NET framework . Dopo di che creare il file 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 Questo genera un Test.class.

javah -jni Test Questo genera un file Test.h che verrà incluso nel codice C++ .

Dopo di che abbiamo bisogno di creare il nostro file 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); 
} 

Infine:

c:\>java Test 

Spero che questo ti aiuta. Un esempio di base per utilizzare la funzione C# in Java.

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

+0

Ho creato il file C# e java e ho compilato il mio problema è questa parte del C++ dove lo includo #using "Test.NetModule" sto usando Visual Studio 2013 – MorganM

+0

Am ancora ottenere questo errore # Un errore fatale è stato rilevato dal Java Runtime Environment: # # Errore interno (0xe0434352), pid = 5800, tid = 5792 # versione # JRE : 7.0-B147 # Java VM: Java HotSpot (TM) Cliente VM (21.0-B17 modalità mista, la condivisione di windows-x86) # telaio problematico: # C [KERNELBASE.dll + 0x812f] # # Impossibile scrivere core dump I minidump non sono abilitati per impostazione predefinita nelle versioni client di Windows # # Un file di segnalazione errori con più informazioni viene salvato come: – MorganM

Problemi correlati