-
May 9th, 2011, 04:45 AM
#1
Using JNA to access export native classes
Hi everyone,
Currently I am working on java project which uses methods and classes present inside a DLL. Suppose I have such a method inside the DLL(named MyDLL.dll):
Method inside my DLL code
extern "C" __declspec(dllexport) int myMethod();
corresponding JNA implementation
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
/** Simple example of native library declaration and usage. */
public class HelloWorld {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary("MyDLL", CLibrary.class);
int myMethod();
}
public static void main(String[] args) {
String libPath = "path to MyDLL.dll";
System.setProperty("jna.library.path", libPath);
int RetVar = CLibrary.INSTANCE.myMethod();
} ////main function ends
} ////class ends
So, my doubt is that like the function present inside the DLL, I have also got a class inside the DLL which needs to be exported:
Class inside my DLL code
extern "C" __declspec(dllexport) Class myClass
{
//////code inside class
};
So, now the question is how I am gonna use this native class inside my java code like i did for the native method?
-
May 9th, 2011, 04:58 AM
#2
Re: Using JNA to access export native classes
Or I have an alternative doubt to it:
if the method inside my DLL is declared as follows:
extern "C" __declspec(dllexport) int myMethod();
that means i just need to declare a native equivalent of 'myMethod' to use it in my java code like:
public native int myMethod();
Now i can call the function as follows: int var = myMethod();
//********************Now the NEW part***********************\\
Inside the DLL, now I also have a class along with the method as follows:
class MyClass
{
public:
int var;
};
extern "C" __declspec(dllexport) MyClass __cdecl myMethod()
{
MyClass obj;
obj.var = 45;
return obj;
}
///////////////////////////DLL code ends\\\\\\\\\\\\\\\\\\\\\\\\
I hope you all noticed that now my return type is an object of a class rather than a normal int.
So, now the bigger question is, how I am going to use this native function (myMethod) inside my JAVA code? Previously the return type was just an integer, so it was easy. But now what or rather how?
See, I have mentioned my 2 doubts. So, clarification of either of the doubt will do.
It's very urgent. So, i would really be grateful if someone can provide me with any kind of solutions or helpful links?
Thanks in advance,
With regards,
Satya Prakash.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|