Draygoes Posted July 15, 2021 Share Posted July 15, 2021 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 More sharing options...
Nine Posted July 15, 2021 Share Posted July 15, 2021 (edited) 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 July 15, 2021 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Draygoes Posted July 15, 2021 Author Share Posted July 15, 2021 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 More sharing options...
Solution Nine Posted July 15, 2021 Solution Share Posted July 15, 2021 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, “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Draygoes Posted July 15, 2021 Author Share Posted July 15, 2021 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 More sharing options...
Nine Posted July 15, 2021 Share Posted July 15, 2021 1 minute ago, Draygoes said: Do you want credit for the help? Nah, a simple thanks is well enough But if you feel the need to donate, there is a button up right for it... Draygoes and JockoDundee 1 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
TheXman Posted July 15, 2021 Share Posted July 15, 2021 (edited) 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 Edited July 15, 2021 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
Draygoes Posted July 15, 2021 Author Share Posted July 15, 2021 (edited) 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 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 July 15, 2021 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now