Link to home
Start Free TrialLog in
Avatar of liluqun
liluqun

asked on

on java native method?

/*
 Hi all,
   I want to use a windows native method which in advapi32.dll, the founction is GetUserName(String s, long n),
  But it alway got error:

  The result is:
java.lang.UnsatisfiedLinkError: GetUserName
     at untitled25.Frame1.GetUserName(Native Method)
     at untitled25.Frame1.<init>(Frame1.java:15)
     at untitled25.Frame1.main(Frame1.java:22)
    Can anyone tell me the whys? who can help me to make it?
    Best whishes,

*/

package untitled25;

import java.awt.*;
import javax.swing.JFrame;

public class Frame1 extends JFrame {
  static {
      System.loadLibrary("advapi32");


  }
  public native long GetUserNameW(String s,long n);
  public Frame1() {
    String s="   ";
    long k=  GetUserNameW(s,255l);
    System.out.println( GetUserNameW(s,255l));
    System.out.println(s);

  }

  public static void main(String[] args) {
    Frame1 frame1 = new Frame1();
     java.awt.Image im=Toolkit.getDefaultToolkit().getImage("e:\\me.jpg");

    frame1.setIconImage(im);
    frame1.setSize(300,300);
    frame1.setVisible(true);

  }
}
/*
  The result is:
java.lang.UnsatisfiedLinkError: GetUserName
     at untitled25.Frame1.GetUserName(Native Method)
     at untitled25.Frame1.<init>(Frame1.java:15)
     at untitled25.Frame1.main(Frame1.java:22)
        */
Avatar of rjackman
rjackman

u can't directly call the dll from java i think u need a bridge to invoke a methods from dll
try refering
www.linar.com
cheers
RJ
Try giving full file name

ie.,System.loadLibrary("advapi32.dll");


 
Hi lilugun

Why can't u use System Property to know the current user name

Why do u need a native libraries to do that


Shyam
Follow the comment from rjackman, you need a wrapper do access existing DLL's.
Try javah -jni on your class you will get a C-header file
and see the signature (Name of the native method) searched from the JVM.
Write a native method with the name you got from the header taking the input parms from Java translate them to C parms.
Call the GetUserName(...) method and return the translated result to the JVM.

Create your own dll with a C compiler .
Load this library instead of advapi32.dll
Ensure both dll's the new one and advapi32.dll are in the PATH and run your Java application.

More information and examples you will find for example at
http://java.sun.com/docs/books/tutorial/native1.1/index.html

cheers
jf
Avatar of liluqun

ASKER

I mean if I got a *.dll file, and I know it contains some functions, how can I use the fountions aa a native method?
Hi lilugun,

I beleive I am very near to solve the problem. The theory says the native methods should be ended with semicolon in the body of the method.
Now in your case the method acts like abstract without implementation. So everything is fine just change your native methods put the semicolon into the block or body of the method.
Try this as I don't have Jdk with me to try at the moment.

Thanks
ImrML
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
Mr ImrML
What happened
Why do u post so many messages

Shyam
Avatar of liluqun

ASKER

Please me useful advice!
I mean if I got a *.dll file, and I know it contains some functions, how can I use the fountions as
a native method?
 IN VISUAL BASIC we know we can use any *.DLL if we know the function we can use it freely!
 IN JAVA ,can we made it?
liluqun,

as rjackman said before, you need a wrapper-DLL.

Why?
public class Frame1 extends JFrame {
 
 // the native method
 public native long GetUserNameW(String s,long n);
 // some more code for the class
 ...
 }

If you run you will get the name of the method in your native dll in your example:
JNIEXPORT jlong JNICALL Java_Frame1_GetUserName   (JNIEnv *, jobject, jstring, jlong);

I'm sure there will be no method inside advapi32.dll with
this name.

you need a piece of C-code which will do the mapping:

#include <Frame1.h> // output from cmd  javah -jni Frame1
// the wrapper method
JNIEXPORT jlong JNICALL Java_Frame1_GetUserName   (JNIEnv * env, jobject obj, jstring jstr , jlong myLong)
{
 char buf[1024]; // buffer for converting jstring to C
 long retValue = -1;
 const char *str = (*env)->GetStringUTFChars(env,jstr,0);
 retValue = GetUserName(str, myLong);
 // releasing the str/jstr pair
 (*env)->ReleaseStringUTFChars(env,jstr,str);
 return retValue;
}

Compile and Link this file and load the resulting DLL.

Hope this helps
jf
liluqun,

as rjackman said before, you need a wrapper-DLL.

Why?
public class Frame1 extends JFrame {
 
 // the native method
 public native long GetUserNameW(String s,long n);
 // some more code for the class
 ...
 }

If you run you will get the name of the method in your native dll in your example:
JNIEXPORT jlong JNICALL Java_Frame1_GetUserName   (JNIEnv *, jobject, jstring, jlong);

I'm sure there will be no method inside advapi32.dll with
this name.

you need a piece of C-code which will do the mapping:

#include <Frame1.h> // output from cmd  javah -jni Frame1
// the wrapper method
JNIEXPORT jlong JNICALL Java_Frame1_GetUserName   (JNIEnv * env, jobject obj, jstring jstr , jlong myLong)
{
 char buf[1024]; // buffer for converting jstring to C
 long retValue = -1;
 const char *str = (*env)->GetStringUTFChars(env,jstr,0);
 retValue = GetUserName(str, myLong);
 // releasing the str/jstr pair
 (*env)->ReleaseStringUTFChars(env,jstr,str);
 return retValue;
}

Compile and Link this file and load the resulting DLL.

Hope this helps
jf
liluqun,

as rjackman said before, you need a wrapper-DLL.

Why?
public class Frame1 extends JFrame {
 
 // the native method
 public native long GetUserNameW(String s,long n);
 // some more code for the class
 ...
 }

If you run you will get the name of the method in your native dll in your example:
JNIEXPORT jlong JNICALL Java_Frame1_GetUserName   (JNIEnv *, jobject, jstring, jlong);

I'm sure there will be no method inside advapi32.dll with
this name.

you need a piece of C-code which will do the mapping:

#include <Frame1.h> // output from cmd  javah -jni Frame1
// the wrapper method
JNIEXPORT jlong JNICALL Java_Frame1_GetUserName   (JNIEnv * env, jobject obj, jstring jstr , jlong myLong)
{
 char buf[1024]; // buffer for converting jstring to C
 long retValue = -1;
 const char *str = (*env)->GetStringUTFChars(env,jstr,0);
 retValue = GetUserName(str, myLong);
 // releasing the str/jstr pair
 (*env)->ReleaseStringUTFChars(env,jstr,str);
 return retValue;
}

Compile and Link this file and load the resulting DLL.

Hope this helps
jf
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.12050000.html
https://www.experts-exchange.com/questions/Q.20015585.html
https://www.experts-exchange.com/questions/Q.20074385.html
https://www.experts-exchange.com/questions/Q.20074388.html
https://www.experts-exchange.com/questions/Q.20074386.html
https://www.experts-exchange.com/questions/Q.20074390.html
https://www.experts-exchange.com/questions/Q.20139915.html
https://www.experts-exchange.com/questions/Q.20139916.html
https://www.experts-exchange.com/questions/Q.20139913.html
https://www.experts-exchange.com/questions/Q.20159531.html
https://www.experts-exchange.com/questions/Q.20165862.html
https://www.experts-exchange.com/questions/Q.20190596.html
https://www.experts-exchange.com/questions/Q.20244233.html
https://www.experts-exchange.com/questions/Q.20254620.html




PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- To be PAQ'ed and points refunded
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial