Java + JNA, looking for a way to run ShellExecuteW

狂风中的少年 提交于 2019-12-06 00:03:12

Because this is the answer I repeat @technomage's comment to the question here:

If you're going to explicitly use wide strings, you typically need to use them for all function parameters. w32 API will typically have LPTCSTR for all C strings, which means String for ascii and WString for unicode.

The working code is therefore:

import com.sun.jna.*;
import com.sun.jna.platform.win32.ShellAPI;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.win32.*;

public class Main {
    public interface Shell32 extends ShellAPI, StdCallLibrary {
        Shell32 INSTANCE = (Shell32)Native.loadLibrary("shell32", Shell32.class);

        WinDef.HINSTANCE ShellExecuteW(WinDef.HWND hwnd,
                                      WString lpOperation,
                                      WString lpFile,
                                      WString lpParameters,
                                      WString lpDirectory,
                                      int nShowCmd);
    }

    public static void main(String[] args) {
        WinDef.HWND h = null;
        WString file = new WString("D:\\日本語");
        Shell32.INSTANCE.ShellExecuteW(h, new WString("open"), file, null, null, 1);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!