继我之前关于 Windows 7 任务栏的问题之后,我想诊断一下为什么 Windows 不承认我的应用程序独立于javaw.exe
. 我目前有以下 JNA 代码来获取AppUserModelID
:
public class AppIdTest {
public static void main(String[] args) {
NativeLibrary lib;
try {
lib = NativeLibrary.getInstance("shell32");
} catch (Error e) {
System.err.println("Could not load Shell32 library.");
return;
}
Object[] functionArgs = new Object[1];
String functionName = null;
Function function;
try {
functionArgs[0] = new String("Vendor.MyJavaApplication")
.getBytes("UTF-16");
functionName = "GetCurrentProcessExplicitAppUserModelID";
function = lib.getFunction(functionName);
// Output the current AppId
System.out.println("1: " + function.getString(0));
functionName = "SetCurrentProcessExplicitAppUserModelID";
function = lib.getFunction(functionName);
// Set the new AppId
int ret = function.invokeInt(functionArgs);
if (ret != 0) {
Logger.out.error(function.getName() + " returned error code "
+ ret + ".");
}
functionName = "GetCurrentProcessExplicitAppUserModelID";
function = lib.getFunction(functionName);
// Output the current AppId
System.out.println("2: " + function.getString(0));
// Output the current AppID, converted from UTF-16
System.out.println("3: "
+ new String(function.getByteArray(0, 255), "UTF-16"));
} catch (UnsupportedEncodingException e) {
System.err.println("System does not support UTF-16 encoding.");
} catch (UnsatisfiedLinkError e) {
System.err.println(functionName + " was not found in "
+ lib.getFile().getName() + ".");
}
}
}
应用程序的输出看似乱码:
1: ‹ÿU‹ìƒìL¡¬Ÿv3ʼnEüSV‹uƒ&
2: ‹ÿU‹ìƒìL¡¬Ÿv3ʼnEüSV‹uƒ&
3: ????????????????P???????????
意识到输出可能是 UTF-16 的事实,在 (3) 中,我尝试从 UTF-16 转换字节数组。老实说,我不知道我的方法是否正确,因为 (a) 我不知道 a 的大小PWSTR
和 (b) 我不知道是否GetCurrentProcessExplicitAppUserModelID
确实返回了字节数组或字符串。
我知道 JSmooth 将在模拟这种效果的包装器中运行 GUI 进程。Launch4j 声称可以这样做,但似乎不起作用。无论 Java 包装器如何,我都希望拥有该AppUserModelID
套件。
这里出了什么问题?