Jump to content

How to find if window handle is active on current virtual desktop (Win10)


 Share

Recommended Posts

I have a working script that changes the core affinity and process priority of multiples of a specific application I have running.  

I have that part figured out.


I would like to make a little modification to it.


Windows10 introduced virtual desktops.  I am trying to have different core affinity and priority of processes on the visible and non-visible desktops.  

What I need is a bool function that could be described as IsWindowOnCurrentDesktop($hWnd).  I have searched the winAPI.au3 but I do not believe anything like that exists built it.

I have searched multiple places before asking for help. I found a Microsoft supplied example of the function I need using C#, but I am unfamiliar with C# to a degree that I cannot port the system call over.

https://blogs.msdn.microsoft.com/winsdk/2015/09/10/virtual-desktop-switching-in-windows-10/

Help is appreciated but not expected.  Thanks in advanced.   In the meantime I will be learning C# syntax and class structure.

Edited by DLS
Link to comment
Share on other sites

I got it to work.  Heres the code if someone needs it.

Relevant Function

Func _IsWindowOnCurrentVirtualDesktop($hWnd)
    $CLSID = "{aa509086-5ca9-4c25-8f95-589d3c07b48a}"
    $IID = "{a5cd92ff-29be-454c-8d04-d82879fb3f1b}"
    $TAG = "IsWindowOnCurrentVirtualDesktop hresult(hwnd;ptr*);"
    $IVirtualDesktopManager = ObjCreateInterface($CLSID, $IID, $TAG)
    $Result = False
    $IVirtualDesktopManager.IsWindowOnCurrentVirtualDesktop($hWnd, $Result)
    $IVirtualDesktopManager = 0
    Return $Result
 EndFunc

Entire Program

#requireadmin
#include <WinAPI.au3>
#include <WinAPIProc.au3>
#include <ProcessConstants.au3>

$Process = ProcessList("processname.exe")
For $i = 1 To $Process[0][0]
    $pid = $Process[$i][1]
    $handle = _WinAPI_OpenProcess($PROCESS_QUERY_INFORMATION+$PROCESS_SET_INFORMATION, False, $pid)
    $hWnd = _GetWindowHandleFromPid($pid)
    ProcessSetPriority ($pid, 0 )                           ;set cpu priority
    _WinAPI_SetProcessAffinityMask($handle, 0x08)           ;set cpu affinity
    ;msgbox(0,"",$Process[$i][0] & "   " & $Process[$i][1]  & "   " & $handle & "   " & $hWnd ) ;debug
    ;msgbox(0, "", _IsWindowOnCurrentVirtualDesktop($hWnd)) ;debug
 Next

Func _IsWindowOnCurrentVirtualDesktop($hWnd)
    $CLSID = "{aa509086-5ca9-4c25-8f95-589d3c07b48a}"
    $IID = "{a5cd92ff-29be-454c-8d04-d82879fb3f1b}"
    $TAG = "IsWindowOnCurrentVirtualDesktop hresult(hwnd;ptr*);"
    $IVirtualDesktopManager = ObjCreateInterface($CLSID, $IID, $TAG)
    $Result = False
    $IVirtualDesktopManager.IsWindowOnCurrentVirtualDesktop($hWnd, $Result)
    $IVirtualDesktopManager = 0
    Return $Result
 EndFunc

 Func _GetWindowHandleFromPid($pid)
    Local $aData = _WinAPI_EnumProcessWindows($pid)
    Return $aData[1][0]
 EndFunc

;~  0x01    CPU1                binary to hex conversion reference chart with 1s for active cores and 0s for disabled
;~  0x02    CPU2
;~  0x03    CPU1+2
;~  0x04    CPU3
;~  0x05    CPU1+3
;~  0x06    CPU2+3
;~  0x07    CPU1+2+3
;~  0x08    CPU4
;~  0x09    CPU1+4
;~  0x0A    CPU2+4
;~  0x0B    CPU1+2+4
;~  0x0C    CPU3+4
;~  0x0E    CPU1+3+4
;~  0x0F    CPU1+2+3+4

 

Edited by DLS
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

×
×
  • Create New...