सी # विधियों को कॉल करने के लिए सी # विधि को अलग-अलग कारणों से जेएनआई का उपयोग करके सीधे जावा में नहीं कहा जा सकता है। तो सबसे पहले हमें सी ++ का उपयोग करके सी # के लिए एक रैपर लिखना होगा, फिर डीएलएल बनाएं और जावा में जेएनआई के माध्यम से इसका इस्तेमाल करें।जावा प्रोग्राम
मुझे सी ++ में सी # कोड को कॉल करने में समस्या है। मैं एक सी ++ प्रोजेक्ट में सी # .netmodule
फ़ाइल जोड़ रहा हूं। कोड नीचे चिपकाया गया है। अगर मैं कुछ गलत कर रहा हूं तो कृपया मुझे मार्गदर्शन करें।
यह मेरा कामयाब सी ++ वर्ग UsbSerialNum.h
है:
#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();
}
};
सी # UsbSerialNumberCSharp.cs
फ़ाइल मैं .netmodule
फ़ाइल बना ली है जहाँ से:
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 करने के लिए कॉल ध्यान नहीं देते हैं ++ सरल सी का उपयोग तो मेरे कोड जावा में ठीक काम कर रहा है। असल में सी ++ प्रबंधित कक्षा ठीक से काम नहीं कर रही है। कृपया मुझे मार्गदर्शन करें। धन्यवाद।
किस बिंदु पर आपको त्रुटि मिल रही है? – Illuminati
समस्या क्या है? क्या यह प्रबंधित सी ++ है? मैं '__gc' बिट्स – flipchart
को पहचान नहीं पा रहा हूं मैंने प्रश्न संपादित किया है। कृपया यह देखें। धन्यवाद। – HashimR