如何从Java调用C#函数 [英] How to call C# function from java

查看:72
本文介绍了如何从Java调用C#函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Java调用C#函数,为此,我创建了以下代码.我创建了一个Java头文件Authenticator.h,代码如下:

I need to call C# function from java and to this I have created the following. I have a create a java header file Authenticator.h , here is the code:

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

I have then create a C# function that Authenticate

namespace SharpAuthenticator
{
    public class Authenticator
    {



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

    }
}

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

Then I am trying to call the C# function from C++(project to create a dll) using the code below;

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中却收到此错误日志.我可能会想念什么.

And finnally create a dll that i need to call from java. The dll is created and I load it well in java but I get this error log in java. What could I be Missing.

#
# 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#文件,如下所示:

First of all lets create a C# file like this:

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

然后使用以下命令进行编译:

Then compile this with command below:

csc.exe /target:module Test.cs  

您可以在.NET Framework的安装路径中找到 csc.exe .之后,创建Java文件:

You can find csc.exe in install path of .NET framework. After that create java file:

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 ++代码.

javah -jni Test This generates a Test.h file which will be included in C++ code.

此后,我们需要创建我们的C ++文件:

After that we need to create our C++ file:

#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#的基本示例.

I hope this helps you. A basic example to use function C# in Java.

来源: 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆