使用 JNA 获取/设置应用程序标识符
继我之前关于Windows 7任务栏的问题之后,我想诊断为什么Windows不承认我的应用程序独立于.我目前有以下JNA代码来获取AppUserModelID
:javaw.exe
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的大小和(b)我不知道GetCurrentProcessExplicitAppUserModelID
是否确实返回字节数组或字符串。PWSTR
我知道JSmooth将在模拟这种效果的包装器中运行GUI过程。Launch4j声称做同样的事情,但似乎不起作用。我希望拥有这个集合,而不管Java包装器如何。AppUserModelID
这到底是怎么回事?