Java 调用 Windows API GetShortPathName

nor*_*771 4 java winapi short-filenames

我想在我的 java 类中使用本机 windows api 函数。

我感兴趣的函数是 GetShortPathName。 http://msdn.microsoft.com/en-us/library/aa364989%28VS.85%29.aspx

我尝试使用这个 - http://dolf.trieschnigg.nl/eightpointthird/eightpointthird.html 但在某些情况下,当我使用它时,java会完全崩溃,所以它不是我的选择。

问题是我是否必须用 C 语言编写代码,制作 DLL,然后在 JNI/JNA 中使用该 DLL?或者也许我可以以不同的方式访问系统 API?

我会感谢您的评论。如果您可以发布一些代码作为示例,我将不胜感激。

...

我使用 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);
    }

}
Run Code Online (Sandbox Code Playgroud)

Dmy*_*nko 5

谢谢你的提示。以下是我改进的功能。它使用 Unicode 版本的 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);
}
Run Code Online (Sandbox Code Playgroud)