How do I tell if the PC is already in Remote Desktop? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
nutnutwin
Posts: 76
Joined: 28 Aug 2019, 07:25

How do I tell if the PC is already in Remote Desktop?

Post by nutnutwin » 08 Feb 2023, 00:34

Hi, thank you for reading my post

# Background
I have two PCs, PC_A, PC_B, in the same LAN

I am using PC_A and have a hotkey to run remotedesktop to connect to another PC(PC_B)

say, I have
^F8:: MyFunc_Connect()

which calls "C:\Windows\SysWOW64\mstsc.exe" with parameter to connect to other PC

# What I want to do
When I RemoteDesktop from PC_A to PC_B I wish to block this shortcutkey,
so as to avoid double RemoteDesktop like PC_A➤PC_B➤PC_A, which cause trouble sometimes

# What I tried
I understand that "mstsc.exe" is RemoteDesktop Client, but I have no idea the "server app exe" on PC_B.
If I do know, then I can use WinExist() to judge if PC_B is being connected from some other PCs.

Thank you.

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: How do I tell if the PC is already in Remote Desktop?  Topic is solved

Post by jNizM » 08 Feb 2023, 04:49

https://github.com/jNizM/ahk-scripts-v2/blob/main/src/Others/IsRemoteSession.ahk

Returns a value of TRUE to indicate that the current session is a remote session, and FALSE to indicate that the current session is a local session.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

nutnutwin
Posts: 76
Joined: 28 Aug 2019, 07:25

Re: How do I tell if the PC is already in Remote Desktop?

Post by nutnutwin » 11 Feb 2023, 17:56

Thank you for your answer, will try it after updating to V2

rc76
Posts: 145
Joined: 07 Nov 2020, 01:45

Re: How do I tell if the PC is already in Remote Desktop?

Post by rc76 » 21 Jan 2024, 07:16

Is there any chance we can convert this code into v1 compatible?

Code: Select all

IsRemoteSession()
{
    #DllLoad "wtsapi32.dll"

    static WTS_CURRENT_SERVER_HANDLE := 0
    static WTSIsRemoteSession        := 29
    static ProcessId                 := DllCall("kernel32\GetCurrentProcessId")

    ; retrieves the Remote Desktop Services session associated with a specified process
    if !(DllCall("kernel32\ProcessIdToSessionId", "UInt", ProcessId, "UInt*", &SessionId := 0))
        throw OSError()

    ; retrieves session information for the specified session on the specified Remote Desktop Session Host (RD Session Host)
    if !(DllCall("wtsapi32\WTSQuerySessionInformation", "Ptr", WTS_CURRENT_SERVER_HANDLE, "UInt", SessionId, "Int", WTSIsRemoteSession, "Ptr*", &Buf := 0, "UInt*", &Size := 0))
    {
        DllCall("wtsapi32\WTSFreeMemory", "Ptr", Buf)
        throw OSError()
    }
    IsRemoteSession := NumGet(Buf, "Int")
    DllCall("wtsapi32\WTSFreeMemory", "Ptr", Buf)

    return IsRemoteSession
}

Post Reply

Return to “Ask for Help (v1)”