Java call for Windows API GetShortPathName
Asked Answered
O

1

4

I'd like to use native windows api function in my java class.

The function I am interested in is GetShortPathName. http://msdn.microsoft.com/en-us/library/aa364989%28VS.85%29.aspx

I tried to use this - http://dolf.trieschnigg.nl/eightpointthree/eightpointthree.html but in some conditions java crashes completely when I use it, so it is not the option for me.

The question is Do I have to write code in e.g C, make DLL and then use that DLL in JNI/JNA? Or maybe I somehow can access the system API in different way?

I will appreciate your comments. If maybe you could post some code as the example I would be grateful.

...

I found the answer using JNA



import com.sun.jna.Native;
import com.sun.jna.platform.win32.Kernel32;

public class Utils {

    public static String GetShortPathName(String path) {
        byte[] shortt = new byte[256];

        //Call CKernel32 interface to execute GetShortPathNameA method
        int a = CKernel32.INSTANCE.GetShortPathNameA(path, shortt, 256);
        String shortPath = Native.toString(shortt);
        return shortPath;

    }

    public interface CKernel32 extends Kernel32 {

        CKernel32 INSTANCE = (CKernel32) Native.loadLibrary("kernel32", CKernel32.class);

        int GetShortPathNameA(String LongName, byte[] ShortName, int BufferCount);
    }

}
Orlena answered 14/6, 2012 at 17:50 Comment(8)
what do you mean when you say 'java crashes'? Can you include in your post how it crashes?Lutes
There are no exceptions. I used it in applet. It was working in Firefox on my machine, but was dying in IE. When I tried on another machine it even died in Firefox. The java console was just disappearing with no notice. After some time I was able to crash java with just console app. # # A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6c978775, pid=4148, tid=5156Orlena
Stack: [0x0b520000,0x0b570000], sp=0x0b56eee4, free space=315k Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) V [jvm.dll+0xa8775] C [temp1600609646579962556.dll+0x11d6] Java_com_eaio_nativecall_IntCall_executeCall0+0x162 j com.eaio.nativecall.IntCall.executeCall0([Ljava/lang/Object;)I+0 j com.eaio.nativecall.IntCall.executeCall([Ljava/lang/Object;)I+21 j Utils.getShortName(Ljava/lang/String;)Ljava/lang/String;+56Orlena
JVM crashed in your native library.Lutes
What do you mean by native library? It was crashing only when I used com.eaio. .... The JNA version didn't crash ... at least until now.Orlena
which version of java are you running? can you try with the following command line: java -Xint <your program>Lutes
I was using 1.6.31. I wish I could investigate this issue further but since it didn't work and I found JNA working I removed the old version of the project.Orlena
@norbi771: As far as I understood, you answered your question by an edit of your question. You should rather post your edit as an answer and then accept it.Helwig
P
5

Thanks for hint. Following is my improved function. It uses Unicode version of the GetShortPathName

import com.sun.jna.Native;
import com.sun.jna.platform.win32.Kernel32;

public static String GetShortPathName(String path) {
    char[] result = new char[256];

    Kernel32.INSTANCE.GetShortPathName(path, result, result.length);
    return Native.toString(result);
}
Property answered 16/2, 2014 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.