【发布时间】:2010-12-26 20:04:16
【问题描述】:
跟进my previous question concerning the Windows 7 taskbar,我想诊断一下为什么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)我不知道PWSTR 的大小和(b)我不知道GetCurrentProcessExplicitAppUserModelID 是否确实返回了一个字节数组或字符串。
我知道 JSmooth 将在模拟这种效果的包装器中运行 GUI 进程。 Launch4j 声称可以这样做,但似乎不起作用。我希望设置AppUserModelID 无论 Java 包装器如何。
这里出了什么问题?
【问题讨论】:
-
如果将格式设为 UTF-8,输出会是什么?
-
??U???L???v3?E?SV?u?&...(实际上更长,因为字节数组的长度为 255)。如果我返回字节值,它会显示负值:-117 -1 85 -117 -20 -125 -20 76 -95 -84 -97 12 118 51 -59 -119 69 -4 83 86 -117 117 8 -125 38 ...。它给我的印象是该函数返回的不是字符串。