Jump to content

Trying to use SetWindowDisplayAffinity though DLLCall. It just doesn't do anything for some reason.


Go to solution Solved by Nine,

Recommended Posts

I am making a function that makes windows screenshot resistant using SetWindowDisplayAffinity. This is my first time using a DLL call, but it looks right to me?
So I set the script up to start and target notepad to test this and it did nothing. No error, no problems, but I can still screenshot notepad.

Any thoughts?
 

$WDA_NONE = 0x00000000;Imposes no restrictions on where the window can be displayed.
$WDA_MONITOR = 0x00000001;The window content is displayed only on a monitor. Everywhere else, the window appears with no content.
$WDA_EXCLUDEFROMCAPTURE = 0x00000011;Windows 10 Version 2004 only! The window is displayed only on a monitor. Everywhere else, the window does not appear at all. 
                                    ;One use for this affinity is for windows that show video recording controls, so that the controls are not included in the capture.


#cs

     Syntax C++
BOOL SetWindowDisplayAffinity(
  HWND  hWnd,
  DWORD dwAffinity
);

#ce

;https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowdisplayaffinity

; Run Notepad
    Run("notepad.exe")

    ; Wait 10 seconds for the Notepad window to appear.
    Local $hWnd = WinWait("[CLASS:Notepad]", "", 10)
MsgBox(0,"",$hWnd);debug
    ; Convert the handle to a string.
    Local $sHWnd = String($hWnd)
MsgBox(0, "", $sHWnd);debug
DllCall("user32.dll", "BOOL", "SetWindowDisplayAffinity", HWnd, $sHWnd, "DWORD", $WDA_EXCLUDEFROMCAPTURE)

 

Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

Try using directly hWnd as the parameter to the function instead of converting it to string...

And HWnd should be in quotes.  Put some error handing after dllcall !

Edited by Nine
Link to comment
Share on other sites

9 minutes ago, Nine said:

Try using directly hWnd as the parameter to the function instead of converting it to string...

And HWnd should be in quotes.  Put some error handing after dllcall !

I updated the code using your suggestions. It's still not working for some reason.
@error returns 1

 

$WDA_NONE = 0x00000000;Imposes no restrictions on where the window can be displayed.
$WDA_MONITOR = 0x00000001;The window content is displayed only on a monitor. Everywhere else, the window appears with no content.
$WDA_EXCLUDEFROMCAPTURE = 0x00000011;Windows 10 Version 2004 only! The window is displayed only on a monitor. Everywhere else, the window does not appear at all.
                                    ;One use for this affinity is for windows that show video recording controls, so that the controls are not included in the capture.


#cs

     Syntax C++
BOOL SetWindowDisplayAffinity(
  HWND  hWnd,
  DWORD dwAffinity
);

#ce

;https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowdisplayaffinity

; Run Notepad
    Run("notepad.exe")

    ; Wait 10 seconds for the Notepad window to appear.
    Local $hWnd = WinWait("[CLASS:Notepad]", "", 10)

DllCall("user32.dll", "BOOL", "SetWindowDisplayAffinity", "HWnd", $hWnd, "DWORD", $WDA_MONITOR)
MsgBox(0, "", @error)

 

Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

  • Solution

As per MSDN :

Quote

hWnd

Type: HWND

A handle to the top-level window. The window must belong to the current process.

The handle does not belong to the current process.  The Run creates a new process...

As per Help File :

Quote
@error: 1 = unable to use the DLL file,

 

Link to comment
Share on other sites

Oh! Ok, my mistake! I thought that was able to target any window hehe. 
Ok, then that solves my problem. Can only protect its own windows. I'm sure someone will find a use for that as a function.


Thanks for your time! Do you want credit for the help?

Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

Link to comment
Share on other sites

1 hour ago, Draygoes said:

I updated the code using your suggestions. It's still not working for some reason.
@error returns 1

You should find & fix the problem that is causing the @error from the DllCall().  @error = 1, from the DllCall(), says that you have a problem with the DllCall() function itself, not that you got a bad return code from the API call.  If you would have found & fixed your problem with the DllCall() first, you could/would have seen that you are getting an "Access denied" error from the API call (see example below).  The "Access denied" error from the API call is most likely due to what Nine said.  In short, the @error = 1 from the DllCall has nothing to do with the real issue.  You just didn't get that far.

Example:

#include <Constants.au3>
#include <WinAPIError.au3>


;SetWindowDisplayAffinity Constants
Const $WDA_NONE               = 0x00000000, _ 
      $WDA_MONITOR            = 0x00000001, _ 
      $WDA_EXCLUDEFROMCAPTURE = 0x00000011    


example()

Func example()
    Local $hWnd
    Local $aDllResult

    ;Launch notepad
    Run("notepad.exe")
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Unable to launch Notepad - @error = " & @error)

    ;Get handle to Notepad window
    $hWnd = WinWaitActive("[RegExpTitle:(?i)^Untitled - Notepad]", "", 5)
    If $hWnd = 0 Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Timed out waiting for Notepad window")

    ;Call API
    $aDllResult = DllCall("user32.dll", "bool", "SetWindowDisplayAffinity", _
                          "hwnd",  $hWnd, _
                          "dword", $WDA_NONE)
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "DllCall error - @error = " & @error)
    If $aDllResult[0] = 0 Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "API failed." & @CRLF & @CRLF & _WinAPI_GetLastErrorMessage())
EndFunc

image.png.fa9a337b083dccd4cc944068d8839d53.png

Edited by TheXman
Link to comment
Share on other sites

35 minutes ago, TheXman said:

You should find & fix the problem that is causing the @error from the DllCall().  @error = 1, from the DllCall(), says that you have a problem with the DllCall() function itself, not that you got a bad return code from the API call.  If you would have found & fixed your problem with the DllCall() first, you could/would have seen that you are getting an "Access denied" error from the API call (see example below).  The "Access denied" error from the API call is most likely due to what Nine said.  In short, the @error = 1 from the DllCall has nothing to do with the real issue.  You just didn't get that far.

Example:

#include <Constants.au3>
#include <WinAPIError.au3>


;SetWindowDisplayAffinity Constants
Const $WDA_NONE               = 0x00000000, _ 
      $WDA_MONITOR            = 0x00000001, _ 
      $WDA_EXCLUDEFROMCAPTURE = 0x00000011    


example()

Func example()
    Local $hWnd
    Local $aDllResult

    ;Launch notepad
    Run("notepad.exe")
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Unable to launch Notepad - @error = " & @error)

    ;Get handle to Notepad window
    $hWnd = WinWaitActive("[RegExpTitle:(?i)^Untitled - Notepad]", "", 5)
    If $hWnd = 0 Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Timed out waiting for Notepad window")

    ;Call API
    $aDllResult = DllCall("user32.dll", "bool", "SetWindowDisplayAffinity", _
                          "hwnd",  $hWnd, _
                          "DWORD", $WDA_NONE)
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "DllCall error - @error = " & @error)
    If $aDllResult[0] = 0 Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "API failed." & @CRLF & @CRLF & _WinAPI_GetLastErrorMessage())
EndFunc

image.png.fa9a337b083dccd4cc944068d8839d53.png

I will remember that for next time, thanks. As of this moment with the working code that I just posted in examples, I get @error 0. :)

Edited by Draygoes
Update after adding examples.
Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...