Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use Windows dll file in Java??

Status
Not open for further replies.

Bartec

Programmer
Jan 21, 2005
54
PL
Hi!

I know that's a lot of tutorials about JNI and smth like that but....:) In my situation I have dll file but without any source so only dll (probably written in VB). So now I wrote a code looks like this:
----------------------------------------------------------
public class Hello {

public native void printHello();

static {
System.loadLibrary("hello");
}

public Hello() {
}

public static void main(String[] args) {
// TODO code application logic here
Hello h = new Hello();

for(int i = 0; i<=900; i++)
h.printHello();

}
}
---------------------------------------------------------
Then I have error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: printHello
at javaapplication9.Hello.printHello(Native Method)
at javaapplication9.Hello.main(Hello.java:40)
-------------------------------------------------
Why errors has occured? I don't know if I can do that, or better I should do like this: Java -> wrapper class(written in C++) -> my dll file

So please advise me what I should to do (some samples would be nice:)

Thanks for any response

Best regards

Bartec

 
I know that has been written in VB and I know the functions name

 
which is exported as a DLL hook as ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I have only hello.dll
In a sample code above I written you can see hello.dll.
I my real code (application) I have dll file from some company. Errors looks the same. So this company told me only methods used in this dll. I don't know nothing more.

 
To use JNI to call a DLL function, you need to know the DLL's exported function name - if you do not know this, then your stuffed.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I exactly know the function name but still have this problem

 
And the function name is (as I asked before) ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
In my right dll file ( I mean in my project) function name sound: void pokazOkno()

I have user manual from company which I have this schecomm.dll. Author write there : Public Sub pokazOkno()



 
So your Java native method should be called :

public native void pokazOkno();

NOT :

public native void printHello();

then surely ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I know, I know but this is only sample which also doesn't work. I have the same problem with pokazOkno() :)

 
I think his first one was hypothetical

Binary Intelligence, true or false?
 
I think he posted his actual code with new names, maybe because pokazOkno is a scaring name.

I've played a lot with JNI, but from what sedj posted I get a serious doubt: can you declare a native Java method and link it to any library just knowing the exported name? Doesn't the library need to be generating according JNI standards or some kind of interface as OLE or COM to communicate with the Java side?

Cheers,
Dian
 
Thats a damned fine point Dian ... re-reading the JNI docs again :


While "in the real world" you probably have existing C functions that you wish to integrate with Java programs, you will still need to modify the signatures for these C functions to work with the JNI. To be sure that you use the correct signatures, it is best to begin by writing and compiling the Java code, as described here.

EG :

Code:
JNIEXPORT void JNICALL 
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) 
{
    printf("Hello world!\n");
    return;
}

this would then map to Java code :

Code:
public class HelloWorld {
   public native void displayHelloWorld();
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Then I think Bartec should go the second way he posted: define a new JNI signature, with a new native method and from there (C/C++) call the existing pokazOkno method.

Cheers,
Dian
 
I can see that I did not say clear.Sory for that!!
Maybe once again
The code I've written in my thread is ONLY SAMPLE and HelloWorld is all right.
pokazOkno method is in my RIGHT PROJECT I actually make in my work, and problem is the same like in the sample!


But now I know that if I want to load dll first of all I need to make step like sedj told.

So my situation looks like that:

My java program -> My code in C++ -> dll which I have.

I think there is no other way to load dll in java if you have ONLY *.dll file.
Anyway thanks for your help.

Cheers
Bartec





 
Can you write a wrapper in c++ to call a dll, adding another (adapter) layer:

java -> c++ (with JNI signature) -> call foreign dll

and:
Public Sub pokazOkno()
doesn't look like c/ c++, but more like publicSubLanguage: Basic - does it?

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top