转载

如何从java调用C#函数

参见英文答案 > Calling C# method within a Java program 2个

我需要从java调用C#函数,并且我创建了以下内容.

我有一个创建一个java头文件Authenticator.h,这里是代码:

#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

然后我创建了一个认证的C#函数

namespace SharpAuthenticator
{
    public class Authenticator
    {



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

    }
}

然后我尝试使用下面的代码从C(项目创建一个DLL)调用C#函数;

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.创建了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

首先,让我们像这样创建一个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

翻译自:https://stackoverflow.com/questions/33779069/how-to-call-c-sharp-function-from-java

原文  https://codeday.me/bug/20190111/500609.html
正文到此结束
Loading...