Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to:using routines of a dll  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a third party dll xyz.dll(without documentation), I can load it by calling:

System.load(dllpath+"\\xyz.dll").

However, how do i come to know wat are the routines that are present in the dll, so that I can use them ?

--thanks in advance
 
Sheriff
Posts: 21256
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not how Java works. You can't just call functions in a DLL.

The DLLs you load through System.load and System.loadLibrary are only used for implementation of native methods. So to be able to use the functionality in that DLL file you will need two extra layers: a Java class with native methods, and a JNI bridge between the Java class and the functions in the DLL.
 
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there and welcome to Javaranch!

What language is the DLL written in? I suspect you're going to have to use JNI to interrogate that native object....
 
subhajit paul
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i have to write the java class with declaration of native methods, i need to know what are the methods present in the dll.

for example, if abc.dll is having a method

void abc1(void)

it can be done by:

public native void abc1();

public Constructor(){System.loadLibrary("abc");}


however, i want to know if i get abc.dll without any documentation, is there a way that can be used to know that there is a method abc1 which takes no arguments and returns nothing?
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know to be honest, you'd be best off using some sort of MS tool to inspect it, anyone?
 
author
Sheriff
Posts: 23394
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

however, i want to know if i get abc.dll without any documentation, is there a way that can be used to know that there is a method abc1 which takes no arguments and returns nothing?



JNI doesn't work like that -- you can't use it to call any DLL. To be quite honest, if you don't know what's in a DLL, is it really a good idea to call it?

In this example "native void abc1()" will execute a call to a function with a name based on both the package and name, and actually pass it parameters (at minimum, the java context). To get the actual C/C++ signature, you have use the javah tool to generate an include file (.h file) from the class file.

And with the include file, you are supposed to write the implementations, and generate a DLL. Now... of course, from the C/C++ code (your implementation), you can call the functions in this other DLL, but again, if you don't know what it does, is it really a good idea to use it?

Henry
[ December 17, 2008: Message edited by: Henry Wong ]
 
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

JNI doesn't work like that -- you can't use it to call any DLL.

No, but you can use JNI wrappers to do all the legwork for you, so you don't have to write your own JNI DLLs/bridges. For example, JNIWrapper. The resulting program will be faster writing your own bridge, but if speed isn't essential...
 
Rob Spoor
Sheriff
Posts: 21256
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
at minimum, the java context


Actually, at minimum the Java context and either the object (for non-static methods) or class object (for static methods). The rest is the actual method parameters.

But I agree with the general consensus - if you don't know what's in the DLL, don't call it. What if one of the functions deletes your entire hard drive?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!