문제

후속 조치 Windows 7 작업 표시 줄에 관한 이전 질문, 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???????????

(3)에서 출력이 UTF-16 일 수 있다는 사실을 알고 있으면 UTF-16에서 바이트 배열을 변환하려고 시도했습니다. 모든 정직에서 나는 여기서 내 접근 방식이 옳은지 (a) 나는 A의 크기를 모른다. PWSTR 그리고 (b) 나는 모른다 GetCurrentProcessExplicitAppUserModelID 실제로 바이트 배열 또는 문자열을 반환하고 있습니다.

JSMooth 가이 효과를 시뮬레이션하는 래퍼에서 GUI 프로세스를 실행한다는 것을 알고 있습니다. Launch4J는 같은 일을한다고 주장하지만 작동하지 않는 것 같습니다. 나는 가질 것을 찾고있다 AppUserModelID 세트 Java 래퍼에 관계없이.

여기서 무엇이 잘못되고 있습니까?

도움이 되었습니까?

해결책

나는 당신의 질문을 보지 못했습니다. 그렇지 않으면 현상금 없이도 시도했을 것입니다.

여기 내가 생각해 낸 것입니다. 코드 자체에 언급 된 바와 같이, 나는 CoTaskMemFree 기능 (에서 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

다른 팁

AppUserModelid를 설정 해야하는 경우 위의 JNA 코드로 충분합니다. 그러나 Java 응용 프로그램의 새로운 Windows 7 기능을 활용하려면 확인하십시오. j7goodies Windows 7 작업 표시 줄 확장을 제공하는 Java 라이브러리.


편집하다: 더 많은 정보 j7goodies 프로그래머 가이드

4.2. AppuserModelid 설정

Windows 7 기능 중 하나를 사용하려면 응용 프로그램이 프로세스 식별자 - 응용 프로그램 사용자 모델 ID를 명시 적으로 설정해야합니다.AppUserModelID). 128 자를 초과 할 수 있으며 공백을 포함 할 수 없습니다. 각 섹션은 예를 들어 낙타 케이스입니다.

 CompanyName.ProductName.SubProduct.VersionInformation

이 식별자는 GUI (창)이 표시되기 전에 설정해야합니다. 당신은 다음을 호출하여 설정했습니다.

// Remember to set AppUserModelID before creating any UI
AppUserModelId.setCurrentProcessId("StrixCode.J7Goodies.Appname");

4.3. 창 속성을 설정합니다

Window 속성이 정의되지 않으면 Java 응용 프로그램을 Windows 7 작업 표시 줄에 고정 할 수 없습니다. 속성은 4 개의 필드로 구성됩니다.

  • AppuserModelid - 전달 된 것과 동일합니다 AppUserModelId.setCurrentProcessId(String)
  • RelAunchDisplayName - 응용 프로그램 이름
  • RelAunchCommand - 응용 프로그램을 시작하는 데 사용 된 전체 명령입니다. Java 프로그램의 경우 다음과 같습니다. <path to javaw.exe> -jar <path to application jar>
  • Relaunchicon - 응용 프로그램의 아이콘으로가는 경로

중요한: RelaunchCommand 그리고 RelaunchDisplayName 항상 함께 설정해야합니다. 이러한 속성을 설정하려면 간단한 창 분리 클래스를 사용하십시오.

WindowProperties props = new WindowProperties(myFrame);
props.setRelaunchCommand("<full path to javaw.exe –arguments>");
props.setRelaunchDisplayName("My Java Application");
props.setRelaunchIcon("<full path to an .ico or .exe file>");
props.setAppUserModelID("StrixCode.J7Goodies.Appname");
props.save();

다음은 전화하는 방법에 대한 더 간단한 예입니다. SetCurrentProcessExplicitAppUserModelID ~을 통해 JNA:

import com.sun.jna.*;
import com.sun.jna.win32.*;

interface Shell32 extends StdCallLibrary {

    Shell32 INSTANCE = (Shell32) Native.loadLibrary("shell32", Shell32.class, W32APIOptions.DEFAULT_OPTIONS);

    NativeLong SetCurrentProcessExplicitAppUserModelID(WString appID);

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top