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;
}
そして、私はjavaから呼び出す必要があるdllをfinnally作成します。 DLLが作成され、私はJavaでうまく読み込みますが、このエラーログはjavaで取得されます。何が私は行方不明になる可能性があります。あなたは、.NET Frameworkのインストールパスでcsc.exe
を見つけることができます
csc.exe /target:module Test.cs
:
using System;
public class Test{
public Test(){}
public String ping(){
return "C# is here.";
}
}
が続いて以下のコマンドでこれをコンパイル:すべての
#
# 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
チェックこの:http://stackoverflow.com/questions/8181344/calling-c-sharp-method-within-a-java-program – phantom
@phantom私が使用して多くの知識を持っていけませんC++。どのようにC#関数をC++から呼び出すことができますか? – MorganM
2時間後に試してみます。 – phantom