Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 521971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:18:21+00:00 2026-05-13T08:18:21+00:00

Following up on my previous question concerning the Windows 7 taskbar , I would

  • 0

Following up on my previous question concerning the Windows 7 taskbar, I would like to diagnose why Windows isn’t acknowledging that my application is independent of javaw.exe. I presently have the following JNA code to obtain the 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() + ".");
        }
    }
}

The output of the application is seemingly gibberish:

1: ‹ÿU‹ìƒìL¡¬Ÿv3ʼnEüSV‹uƒ&
2: ‹ÿU‹ìƒìL¡¬Ÿv3ʼnEüSV‹uƒ&
3: ????????????????P???????????

Being aware of the fact that the output may be UTF-16, in (3) I attempted to convert a byte array from UTF-16. In all honesty I don’t know if my approach here is right as (a) I don’t know the size of a PWSTR and (b) I don’t know if GetCurrentProcessExplicitAppUserModelID is indeed returning a byte array or string.

I’m aware that JSmooth will run the GUI process in a wrapper which simulates this effect. Launch4j claims to do the same, but doesn’t appear to work. I am looking to have the AppUserModelID set regardless of the Java wrapper.

What is going wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:18:22+00:00Added an answer on May 13, 2026 at 8:18 am

    I didn’t see your question before otherwise I would have given a try even without a bounty.

    Here is what I came up with. Please note, as stated in the code itself, I didn’t implement proper memory clean up with the CoTaskMemFree function (from Ole32.dll). So I suggest you take only the implementation for 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");
      }
    }
    

    Does it work for you?

    At least here it correctly prints back:

    com.stackoverflow.AppIdTest.AppIdTest

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.