栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用JNA获取/设置应用程序标识符

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用JNA获取/设置应用程序标识符

我之前没有看到您的问题,否则即使没有赏金,我也会尝试一下。

这是我想出的。
请注意,如代码本身所述,我没有使用

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/385592.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号