我之前没有看到您的问题,否则即使没有赏金,我也会尝试一下。
这是我想出的。
请注意,如代码本身所述,我没有使用CoTaskMemFree
函数from来实现适当的内存清理Ole32.dll
。因此,我建议您仅将实现用于SetCurrentProcessExplicitAppUserModelID()
package com.stackoverflow.AppIdTest;import com.sun.jna.Native;import com.sun.jna.NativeLong;import com.sun.jna.Pointer;import com.sun.jna.WString;import com.sun.jna.ptr.PointerByReference;public class AppIdTest{ public static void main(String[] args) throws Exception { setCurrentProcessExplicitAppUserModelID(AppIdTest.class.getName()); System.out.println(getCurrentProcessExplicitAppUserModelID()); } // DO NOT DO THIS, IT'S JUST FOR TESTING PURPOSE AS I'M NOT FREEING THE MEMORY // AS REQUESTED BY THE documentATION: // // http://msdn.microsoft.com/en-us/library/dd378419%28VS.85%29.aspx // // "The caller is responsible for freeing this string with CoTaskMemFree when // it is no longer needed" public static String getCurrentProcessExplicitAppUserModelID() { final PointerByReference r = new PointerByReference(); if (GetCurrentProcessExplicitAppUserModelID(r).longValue() == 0) { final Pointer p = r.getValue(); return p.getString(0, true); // here we leak native memory by lazyness } return "N/A"; } public static void setCurrentProcessExplicitAppUserModelID(final String appID) { if (SetCurrentProcessExplicitAppUserModelID(new WString(appID)).longValue() != 0) throw new RuntimeException("unable to set current process explicit AppUserModelID to: " + appID); } private static native NativeLong GetCurrentProcessExplicitAppUserModelID(PointerByReference appID); private static native NativeLong SetCurrentProcessExplicitAppUserModelID(WString appID); static { Native.register("shell32"); }}对你起作用吗?
至少在这里它可以正确打印回:
com.stackoverflow.AppIdTest.AppIdTest



