Java + JNA, looking for a way to run ShellExecuteW

907 views Asked by At

I am trying to run a ShellExecute function from java with JNA. I dont have any problems running ShellExecuteA on non-unicode folders

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 ShellExecuteA(WinDef.HWND hwnd,
                                      String lpOperation,
                                      String lpFile,
                                      String lpParameters,
                                      String lpDirectory,
                                      int nShowCmd);
    }

    public static void main(String[] args) {
        WinDef.HWND h = null;
        WString st = new WString("D:");
        Shell32.INSTANCE.ShellExecuteA(h, "open", st.toString(), null, null, 1);

    }
}

But since I want to be able to use it on unicode folders I actually want to run ShellExecuteW instead of A version, but cant figure how. Every time I run the following code it just finishes executing without doing anything or showing any errors.

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,
                                      String lpOperation,
                                      WString lpFile,
                                      String lpParameters,
                                      String lpDirectory,
                                      int nShowCmd);
    }

    public static void main(String[] args) {
        WinDef.HWND h = null;
        WString st = new WString("D:\\日本語");
        Shell32.INSTANCE.ShellExecuteW(h, "open", st, null, null, 1);

    }
}

I guess the problem lies withing the third parameter lpFile - I tried using String, WString all the same. Any help will be appreciated.

Thanks.

1

There are 1 answers

0
Daniel On

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);
    }
}