• Post Reply Bookmark Topic Watch Topic
  • New Topic

JNA to call advapi32 GetUserName function

 
Jade Jadaaih
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I am trying JNA to get the logged in user name in a windows machine, I am unable to succeed in it. My code is as follows,

package com.jgk.mains;

import java.nio.ByteBuffer;

import com.sun.jna.Library;
import com.sun.jna.Native;

public class SystemUserName {

public interface Advapi32 extends Library {

Advapi32 INSTANCE = (Advapi32)Native.loadLibrary("advapi32", Advapi32.class);


boolean GetUserNameA(byte[] name, long nSize);

}

/**
* @param args
*/
public static void main(String[] args) {
try{
byte[] bytes = new byte[2007];
String name = new String();
long n = 0;
Boolean bool = Advapi32.INSTANCE.GetUserNameA(bytes, n);
System.out.println("Result : "+bool);
}catch(Exception e){
e.printStackTrace();
}
}

}

I am getting the following error,

#
# An unexpected error has been detected by Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x77fe1de8, pid=1468, tid=2316
#
# Java VM: Java HotSpot(TM) Client VM (10.0-b22 mixed mode, sharing windows-x86)
# Problematic frame:
# C [Secur32.dll+0x1de8]
#
# An error report file with more information is saved as:
# D:\Workspace\Cafe\hs_err_pid1468.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

http://www.astercity.net/~azakrze3/html/win32_api_functios.html#GetUserName is where I found win32 api.

The pid file contents are as follows,

#
# An unexpected error has been detected by Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x77fe1de8, pid=1468, tid=2316
#
# Java VM: Java HotSpot(TM) Client VM (10.0-b22 mixed mode, sharing windows-x86)
# Problematic frame:
# C [Secur32.dll+0x1de8]
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

--------------- T H R E A D ---------------

Current thread (0x00386400): JavaThread "main" [_thread_in_native, id=2316, stack(0x003a0000,0x003f0000)]

siginfo: ExceptionCode=0xc0000005, reading address 0x00000000

Registers:
EAX=0x7ffde000, EBX=0x00000008, ECX=0x00000002, EDX=0x00000008
ESP=0x003ef7f0, EBP=0x003ef80c, ESI=0x00000000, EDI=0x00386400
EIP=0x77fe1de8, EFLAGS=0x00010206

Top of Stack: (sp=0x003ef7f0)
0x003ef7f0: 00386400 003ef930 00000008 77df9830
0x003ef800: 77fe0000 77e46014 00000008 003ef820
0x003ef810: 77dfd4de 00010002 030a8050 00000000
0x003ef820: 003ef838 1000da37 030a8050 00000000
0x003ef830: 00000000 00386240 003ef898 10009182
0x003ef840: 10008eb0 003ef880 00000010 00000001
0x003ef850: 003efb20 77dfd4c9 003ef84c 003ef850
0x003ef860: 003ef930 6d9c2b5f 003864f4 02a3dc0c

Instructions: (pc=0x77fe1de8)
0x77fe1dd8: ec 83 ec 10 53 56 57 64 a1 18 00 00 00 8b 75 10
0x77fe1de8: 8b 0e 8b 40 30 d1 e1 51 33 db 53 ff 70 18 ff 15


Stack: [0x003a0000,0x003f0000], sp=0x003ef7f0, free space=317k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [Secur32.dll+0x1de8]
C [ADVAPI32.dll+0x2d4de]
C [jna36577.tmp+0xda37]
C [jna36577.tmp+0x9182]
C [jna36577.tmp+0x2161]
C [jna36577.tmp+0x2849]
v ~BufferBlob::Interpreter
v ~BufferBlob::Interpreter
v ~BufferBlob::Interpreter
v ~BufferBlob::Interpreter
v ~BufferBlob::Interpreter
v ~BufferBlob::Interpreter
v ~BufferBlob::StubRoutines (1)

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
v ~BufferBlob::Interpreter
v ~BufferBlob::Interpreter
v ~BufferBlob::Interpreter
v ~BufferBlob::Interpreter
v ~BufferBlob::Interpreter
v ~BufferBlob::Interpreter
v ~BufferBlob::StubRoutines (1)

--------------- P R O C E S S ---------------

Java Threads: ( => current thread )
0x02a86800 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=2168, stack(0x02d00000,0x02d50000)]
0x02a81400 JavaThread "CompilerThread0" daemon [_thread_blocked, id=2492, stack(0x02cb0000,0x02d00000)]
0x02a80000 JavaThread "Attach Listener" daemon [_thread_blocked, id=260, stack(0x02c60000,0x02cb0000)]
0x02a7f400 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=3120, stack(0x02c10000,0x02c60000)]
0x02a42400 JavaThread "Finalizer" daemon [_thread_blocked, id=2360, stack(0x02bc0000,0x02c10000)]
0x02a3e000 JavaThread "Reference Handler" daemon [_thread_blocked, id=1328, stack(0x02b70000,0x02bc0000)]
=>0x00386400 JavaThread "main" [_thread_in_native, id=2316, stack(0x003a0000,0x003f0000)]

Other Threads:
0x02a3cc00 VMThread [stack: 0x02b20000,0x02b70000] [id=2532]
0x02a98c00 WatcherThread [stack: 0x02d50000,0x02da0000] [id=652]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
def new generation total 960K, used 362K [0x22960000, 0x22a60000, 0x22e40000)
eden space 896K, 33% used [0x22960000, 0x229aa978, 0x22a40000)
from space 64K, 100% used [0x22a50000, 0x22a60000, 0x22a60000)
to space 64K, 0% used [0x22a40000, 0x22a40000, 0x22a50000)
tenured generation total 4096K, used 133K [0x22e40000, 0x23240000, 0x26960000)
the space 4096K, 3% used [0x22e40000, 0x22e61788, 0x22e61800, 0x23240000)
compacting perm gen total 12288K, used 305K [0x26960000, 0x27560000, 0x2a960000)
the space 12288K, 2% used [0x26960000, 0x269ac5b0, 0x269ac600, 0x27560000)
ro space 8192K, 66% used [0x2a960000, 0x2aeb0e10, 0x2aeb1000, 0x2b160000)
rw space 12288K, 52% used [0x2b160000, 0x2b7b04c8, 0x2b7b0600, 0x2bd60000)

Dynamic libraries:
0x00400000 - 0x00423000 C:\Java\jdk1.6.0_06\bin\javaw.exe
0x7c900000 - 0x7c9b0000 C:\WINDOWS\system32\ntdll.dll
0x7c800000 - 0x7c8f5000 C:\WINDOWS\system32\kernel32.dll
0x77dd0000 - 0x77e6b000 C:\WINDOWS\system32\ADVAPI32.dll
0x77e70000 - 0x77f02000 C:\WINDOWS\system32\RPCRT4.dll
0x77fe0000 - 0x77ff1000 C:\WINDOWS\system32\Secur32.dll
0x7e410000 - 0x7e4a0000 C:\WINDOWS\system32\USER32.dll
0x77f10000 - 0x77f57000 C:\WINDOWS\system32\GDI32.dll
0x76390000 - 0x763ad000 C:\WINDOWS\system32\IMM32.DLL
0x7c340000 - 0x7c396000 C:\Java\jdk1.6.0_06\jre\bin\msvcr71.dll
0x6d870000 - 0x6dac0000 C:\Java\jdk1.6.0_06\jre\bin\client\jvm.dll
0x76b40000 - 0x76b6d000 C:\WINDOWS\system32\WINMM.dll
0x6d320000 - 0x6d328000 C:\Java\jdk1.6.0_06\jre\bin\hpi.dll
0x76bf0000 - 0x76bfb000 C:\WINDOWS\system32\PSAPI.DLL
0x6d820000 - 0x6d82c000 C:\Java\jdk1.6.0_06\jre\bin\verify.dll
0x6d3c0000 - 0x6d3df000 C:\Java\jdk1.6.0_06\jre\bin\java.dll
0x6d860000 - 0x6d86f000 C:\Java\jdk1.6.0_06\jre\bin\zip.dll
0x10000000 - 0x10052000 C:\Documents and Settings\gokulkannan.j\Local Settings\Temp\jna36577.tmp
0x77c10000 - 0x77c68000 C:\WINDOWS\system32\msvcrt.dll

VM Arguments:
jvm_args: -Dfile.encoding=UTF-8
java_command: com.jgk.mains.SystemUserName
Launcher Type: SUN_STANDARD

Environment Variables:
JAVA_HOME=C:\Java\jdk1.5.0_14
CLASSPATH=D:\classpath\activation.jar;D:\classpath\axis-ant.jar;D:\classpath\axis.jar;D:\classpath\commons-discovery-0.2.jar;D:\classpath\commons-logging-1.0.4.jar;D:\classpath\jasper-compiler-jdt.jar;D:\classpath\jasper-compiler.jar;D:\classpath\jasper-runtime.jar;D:\classpath\jaxrpc.jar;D:\classpath\jsp-api.jar;D:\classpath\log4j-1.2.8.jar;D:\classpath\mail.jar;D:\classpath\saaj.jar;D:\classpath\servlet-api.jar;D:\classpath\wsdl4j-1.5.1.jar;D:\Workspace\MQSeries\lib\com.ibm.mq.jar; D:\Workspace\MQSeries\lib\connector.jar; D:\Workspace\MQSeries\lib\base; D:\Workspace\MQSeries\lib\jta.jar;
PATH=C:/Java/jdk1.5.0_14/bin/../jre/bin/client;C:/Java/jdk1.5.0_14/bin/../jre/bin;C:\Java\jdk1.5.0_14\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;D:\Ant1.7\bin;D:\Axis2-1.4\bin;D:\Javacc4.1\bin;
USERNAME=gokulkannan.j
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 15 Model 6 Stepping 5, GenuineIntel



--------------- S Y S T E M ---------------

OS: Windows XP Build 2600 Service Pack 2

CPU:total 2 (2 cores per cpu, 1 threads per core) family 15 model 6 stepping 5, cmov, cx8, fxsr, mmx, sse, sse2, sse3

Memory: 4k page, physical 1564140k(565324k free), swap 3506192k(2581212k free)

vm_info: Java HotSpot(TM) Client VM (10.0-b22) for windows-x86 JRE (1.6.0_06-b02), built on Mar 25 2008 01:22:05 by "java_re" with MS VC++ 7.1

time: Mon Oct 20 18:27:21 2008
elapsed time: 0 seconds




Could any one help me please?

Thanks,
Jade
[ October 20, 2008: Message edited by: Jade Jadaaih ]
 
Rob Spoor
Sheriff
Posts: 20821
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your declaration is wrong. From MSDN:

Those are both pointers, the first to a string (C char buffer), the second to a DWORD.

According to the JNA documentation, DWORD should be mapped to int, not long. But since it's a pointer to DWORD, you should probably use IntByReference. byte[] for the LPTSTR should work as far as I know.
 
Jade Jadaaih
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Rob !!

I will try and fix it. One more question, when I try to use the GetUserName I am getting an exception saying the the procedure does not exist. As you may see, in the code I am using the alias name GetUserNameA.

Should I be using GetUserName or GetUserNameA? and why did GetUserName not work?
 
Rob Spoor
Sheriff
Posts: 20821
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most native functions that require a string have actually two versions, one ending with A (ASCII text only, supports char) and one ending with W (16 bit unicode, supports WCHAR). The one without either is usually a macro to either one of these, depending on the platform.

I'd simply go for the A version all the time unless you really need unicode support, then you go for the W version. By explicitly specifying the A or W you'll make sure you always get what you expect.
 
Jade Jadaaih
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob !!

You said it right. The IntByReference made it work, the corrected code for reference is as follows.

package com.jgk.mains;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;

public class SystemUserName {

public interface Advapi32 extends Library {

Advapi32 INSTANCE = (Advapi32)Native.loadLibrary("advapi32", Advapi32.class);


boolean GetUserNameA(byte[] name, IntByReference intRef);

}

/**
* @param args
*/
public static void main(String[] args) {
try{
byte[] bytes = new byte[2007];
IntByReference intRef = new IntByReference(100);
boolean bool = Advapi32.INSTANCE.GetUserNameA(bytes, intRef);
System.out.println("Result : "+bool);
}catch(Exception e){
e.printStackTrace();
}
}

}

Could you please let me know any pointers from where I could learn more of JNA please?

Thanks for your time.
 
Rob Spoor
Sheriff
Posts: 20821
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't got past https://jna.dev.java.net/ yet, but I got introduced by this artical on JavaWorld.

Oh, and can you please Use Code Tags in the future?
[ October 21, 2008: Message edited by: Rob Prime ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!