15

C# methods cannot be called directly in Java using JNI due to different reasons. So first we have to write a wrapper for C# using C++ then create the dll and use it through JNI in Java.

I have problem in calling C# code in C++. I'm adding C# .netmodule file to a C++ project. Code is pasted below. Please guide me if i'm doing anything wrong.

This is my managed C++ class 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();
        }
};

C# UsbSerialNumberCSharp.cs file from which i've created the .netmodule file:

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");
        }
    }
}

Here is my main makeDLL.cpp file from which makeDLL.dll is created:

#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();
}

Here is my java class:

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();

}

EDIT:

If i simply ignore the call to UsbSerial.h in my main file i.e. use simple C++ then my code is working fine in Java. Basically C++ managed class is not working properly. Please guide me. Thanks.

14
  • at which point you are getting the error? Commented Nov 18, 2011 at 11:05
  • What's the problem? Is this managed C++? I don't recognize the __gc bits Commented Nov 18, 2011 at 11:05
  • i've edited the question. please check it out. thanks. Commented Nov 18, 2011 at 11:27
  • @BumbleBee: when pointer of UsbSerial serial* is created and constructor is called. That is where i'm getting the error. Commented Nov 18, 2011 at 11:28
  • @flipchart: __gc is used to make a garbage collectable instance to be compatible with java... Commented Nov 18, 2011 at 11:29

2 Answers 2

8

It would be useful to know what you need this interoperability for exactly. In any case, you should look into IKVM; alternatively you can (as has been suggested for a similar problem) use COM as a bridge: expose the C#/CLR as a COM interface and then use com4j in Java.

Sign up to request clarification or add additional context in comments.

1 Comment

I want to get the serial number of all attached USBs, have already written the code in C#, want to use it in Java.
0
+50

You can avoid C# and still can query WMI using C++ only. See Using WMI to call method on objects

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.