다른 이유로 인해 JNI를 사용하여 Java에서 C# 메서드를 직접 호출 할 수 없습니다. 먼저 C++ 용 래퍼를 작성한 다음 dll을 만들고 Java 용 JNI를 통해 사용해야합니다.Java 프로그램 내에서 C# 메서드 호출
C++에서 C# 코드를 호출 할 때 문제가 있습니다. 나는 C# .netmodule
파일을 C++ 프로젝트에 추가하려고합니다. 아래에 코드가 붙여 넣어집니다. 내가 틀린 일을하고 있다면 안내해 줘.
이는 내 관리되는 C++ 클래스 UsbSerialNum.h
입니다 : 내가 .netmodule
파일을 만든있는
#using <mscorlib.dll>
#include <iostream>
#using "UsbSerialNumberCSharp.netmodule"
using namespace std;
using namespace System;
public __gc class UsbSerialNum
{
public:
UsbSerialNumberCSharp::UsbSerialNumberCSharp __gc *t;
UsbSerialNum() {
cout<<"Hello from C++";
t = new UsbSerialNumberCSharp::UsbSerialNumberCSharp();
}
void CallUsbSerialNumberCSharpHello() {
t->hello();
}
};
C# UsbSerialNumberCSharp.cs
파일 : 여기
using System.Collections.Generic;
using System.Text;
namespace UsbSerialNumberCSharp
{
public class UsbSerialNumberCSharp
{
public UsbSerialNumberCSharp(){
Console.WriteLine("hello");
}
public static void hello()
{
Console.WriteLine("hello");
}
public void helloCSharp()
{
Console.WriteLine("helloCSharp");
}
}
}
는 makeDLL.dll
이 만들어지고 내 주요 makeDLL.cpp
파일이되는 :
#include "jni.h"
#include <iostream>
// This is the java header created using the javah -jni command.
#include "testDLL.h"
// This is the Managed C++ header that contains the call to the C#
#include "UsbSerialNum.h"
using namespace std;
JNIEXPORT void JNICALL Java_testDLL_hello
(JNIEnv *, jobject) {
// Instantiate the MC++ class.
UsbSerialNum* serial = new UsbSerialNum();
serial->CallUsbSerialNumberCSharpHello();
}
여기 내 자바 클래스입니다 :
public class testDLL {
static {
System.loadLibrary("makeDLL");
}
/**
* @param args
*/
public static void main (String[] args) {
// new testDLL().GetUSBDevices("SCR3", 100);
new testDLL().hello();
}
public native void hello();
}
편집 :
단순히 즉 내 주요 파일에 UsbSerial.h에 대한 호출을 무시하면 다음 내 코드는 자바에서 잘 작동 ++ 간단한 C를 사용합니다. 기본적으로 C++ 관리 클래스가 제대로 작동하지 않습니다. 나를 안내하십시오. 감사.
어디에서 오류가 있습니까? – Illuminati
무엇이 문제입니까? 이것은 C++ 관리입니까? 나는'__gc' 비트를 인식하지 못합니다. – flipchart
질문을 편집했습니다. 그것을 확인하시기 바랍니다. 감사. – HashimR