0

i got dll from the customer ,this dll has methode which return class object e.g. DLL code is like this-

namespace mDLL
{ 
class myDll
{
      public:
      char* getName()
      {
            return "Hello World";
      }
};
}
extern "C" _declspec(dllimport) mDLL::myDll* GetDllInstance(__in__ const        char* const Name);

i want to use this dll insdie my JAVA code and call methode GetDllInstance ,please help me in implementing this . i tried JNA but dont know how to retrieve class pointer object

  • Note that the only use you can make of this pointer in Java, is to send it back to C++ in some other JNI call. – Alex Cohn May 30 '16 at 03:49
  • In general you are going to create an interface class that extends Library and calls `Native.loadLibrary("TheDLLname", YourClassName.class);` and then define that method with `String` argument. The return value depends on what you're going to do with it and/or the DLL. At a minimum it will be a `Pointer`. More likely you'll want a `PointerByReference`. Will you be calling other methods of that DLL? Please tell us what further action you want to take with the returned class pointer. – Daniel Widdis May 30 '16 at 05:05

1 Answers1

0

The inclusion of a string (char *) parameter in your GetDllInstance method is undocumented in your question. I am answering under the assumption that you would pass the name of the DLL as an argument here, e.g., "MyDll". If this assumption is incorrect, please comment and I will update this answer.

You will begin by creating an interface for the DLL that extends JNA's Library interface, inside which you specify the signature of the API's methods

public interface MyDll extends Library {
    MyDLL INSTANCE = (MyDLL) Native.loadLibrary("MyDll", MyDll.class);

    String getName();
}

Following this, you can access the method of this DLL simply within your code by calling the instance:

String name = MyDll.INSTANCE.getName();

There is no need for you to keep track of the pointer to the DLL instance; by inheriting from JNA's Library class all that work is done for you by JNA.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63