Java + JNA,寻找一种运行ShellExecuteW的方法 [英] Java + JNA, looking for a way to run ShellExecuteW

查看:82
本文介绍了Java + JNA,寻找一种运行ShellExecuteW的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JNA从Java运行ShellExecute函数. 我在非Unicode文件夹上运行ShellExecuteA没有任何问题

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

    }
}

但是,由于我希望能够在unicode文件夹上使用它,因此我实际上想运行ShellExecuteW而不是A版本,但是无法确定如何运行.每当我运行以下代码时,它就完成执行而没有做任何事情或显示任何错误.

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

    }
}

我猜问题在于第三个参数lpFile-我尝试使用String和WString都一样. 任何帮助将不胜感激.

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

谢谢.

推荐答案

因为这是答案,所以我在这里重复@technomage对问题的评论:

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

如果要显式使用宽字符串,则通常需要将其用于所有函数参数.对于所有C字符串,w32 API通常都具有LPTCSTR,这意味着ascii为String,unicode为WString.

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.

因此,工作代码为:

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

这篇关于Java + JNA,寻找一种运行ShellExecuteW的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆