java - 如何从java调用C#函数

标签 java c# c++

<分区>

我需要从 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 Framework 的安装路径中找到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

关于java - 如何从java调用C#函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33779069/

相关文章:

c# - 无法使用 SmtpClient 发送邮件

c++ - 奇怪的 GetTickCount 问题

c++ - 生成文件 : multiple definition of _start 中的错误

java - Android:同时使用 adjustPan 和 adjustResize 不起作用,但我需要两者

c# - Ruby 的以下行做了什么? "integer >> integer"

Java 抽象方法参数类型

c# - 没有 DeploymentItemAttribute 的 MSTest 部署

c++ - 比较c++中的两个数组并根据元素的匹配或不匹配返回值

java - 如何将图标转换为图像

java - 构造函数中的可重写方法调用有什么问题?