Search code examples
javawinapijna

SetWindowDisplayAffinity fails with error "Access denied"


I am using jna.extra.User32Extra library in java to find a window and change the windowdisplayaffinity value. But it is returning "ACCESS DENIED" error code.

HWND top= target.findWindow("WindowTitle");
System.err.println(User32Extra.INSTANCE.SetWindowDisplayAffinity(top, 0));
System.err.println(Native.getLastError());

The following code gives "false" and error code "5"(ACCESS DENIED).

Do I need any extra permissions? What is the reason for that error?


Solution

  • SetWindowDisplayAffinity can only be used on a window owned by the calling process. Hence the error. The documentation says:

    This feature enables applications to protect their own onscreen window content from being captured or copied through a specific set of public operating system features and APIs.

    The feature would be rendered useless if another application could so readily override the target application's choice.