Wtsgetactiveconsolesessionid

[DllImport("Kernel32.dll")][return: MarshalAs(UnmanagedType.U4)]
public static extern uint WTSGetActiveConsoleSessionId(
);

Microsoft documentation: Link

Last updated