3

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.

estw272
  • 211
  • 5
  • 20
  • You don't check for errors, so why would you expect any? Read the documentation of the function you are calling to learn how to test for errors. – David Heffernan Jun 10 '15 at 10:35
  • 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. – technomage Jun 10 '15 at 15:20
  • @technomage Thanks, changing all strings to WString worked. – estw272 Jun 11 '15 at 02:30

1 Answers1

0

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);
    }
}
Daniel
  • 27,718
  • 20
  • 89
  • 133