Jump to content


Photo

Switch user [in progress]


  • Please log in to reply
2 replies to this topic

#1 daonlyfreez

daonlyfreez
  • Members
  • 995 posts

Posted 26 August 2012 - 10:27 PM

Switch to user selection screen (current user stays logged-on)

#NoEnv 

/*
Switch user - Switch to user selection screen (current user stays logged-on)
Probably needs Windows Vista and higher

http://blog.dotsmart.net/2008/01/17/shortcut-to-switch-user-in-windows-vista/
http://msdn.microsoft.com/en-us/library/aa383830.aspx

BOOL WTSDisconnectSession(
  _In_  HANDLE hServer,
  _In_  DWORD SessionId,
  _In_  BOOL bWait
);

*/

Ptr := A_PtrSize ? "Ptr" : "UInt"

WTS_CURRENT_SERVER_HANDLE := 0
WTS_CURRENT_SESSION := -1

res := DllCall("wtsapi32.dll\WTSDisconnectSession"
				, Ptr, WTS_CURRENT_SERVER_HANDLE
				, "UInt", WTS_CURRENT_SESSION
				, "Int", 1)
If !(res) ; res should be 1 on success
   MsgBox % "ErrorLevel: " . ErrorLevel . "`nResult: " . res

Tested and working on:
- AutoHotkey Basic v1.0.48.5 x86, Windows 7 x86 Home Premium
- AutoHotkey_L v1.1.5.6 x86, Windows 7 x86 Home Premium
- AutoHotkey_L v1.1.08.01 x64, Windows 7 x64

User switching without interaction

German site with code. Looking at this now, but seems too complicated for me.

Not working (yet)
#NoEnv

/*

http://msdn.microsoft.com/en-us/library/bb394782%28v=vs.85%29
http://plainwonders.blogspot.nl/2009/09/why-doesnt-wtsconnectsession-work-when.html

BOOL WINAPI WTSConnectSession(
  _In_  ULONG LogonId,
  _In_  ULONG TargetLogonId,
  _In_  PTSTR pPassword,
  _In_  BOOL bWait
);


http://kay-bruns.de/wp/2009/05/19/benutzer-wechsle-dich/

BOOL DoFUS(LPCTSTR ToUser) //Do Fast User Switching
{
  //ToUser can be 'UserName' or 'DomainName\\UserName'
  DWORD SessID=-1;
  DWORD n=0;
  WTS_SESSION_INFO* si=0;
  WTSEnumerateSessions(0,0,1,&si,&n);
  //get Session ID for ToUser
  if (si && n)
  {
    for (DWORD i=0;i<n;i++)
    {
      TCHAR* un=0;
      TCHAR* dn=0;
      DWORD unl=0;
      DWORD dnl=0;
      WTSQuerySessionInformation(0,si[i].SessionId,WTSUserName,&un,&unl);
      WTSQuerySessionInformation(0,si[i].SessionId,WTSDomainName,&dn,&dnl);
      CBigResStr undn(L"%s\\%s",dn,un); //this just combines domain\\user
      WTSFreeMemory(dn);
      dn=0;
      if ((_tcsicmp(un,ToUser)==0)||(_tcsicmp(undn,ToUser)==0))
      {
        WTSFreeMemory(un);
        SessID=si[i].SessionId;
        break;
      }
      WTSFreeMemory(un);
      un=0;
    }
    WTSFreeMemory(si);
  }
  if (SessID==-1)
    return FALSE;
  return SwitchToSession(SessID);
}


BOOL SwitchToSession(DWORD SessionId)
{
  HMODULE hWinSta = LoadLibraryW(L"winsta.dll");
  if (!hWinSta)
    return FALSE;
  typedef BOOL (WINAPI *WSCW)(HANDLE,DWORD,DWORD,PWCHAR,BOOL);
  WSCW WinStationConnectW=(WSCW)GetProcAddress(hWinSta,"WinStationConnectW");
  if (!WinStationConnectW)
    return FALSE;
  return WinStationConnectW(0,SessionId,-1,L"",TRUE);
}


*/


/*
Ptr := A_PtrSize ? "Ptr" : "UInt"

WTS_CURRENT_SERVER_HANDLE := 0
WTS_CURRENT_SESSION := -1

LOGONID_CURRENT := -1

res := DllCall("wtsapi32.dll\WTSConnectSession"
				, "UInt", WTS_CURRENT_SERVER_HANDLE
				, "UInt", LOGONID_CURRENT
				, "Str", ""
				, "Int", 1)

MsgBox % "ErrorLevel: " . ErrorLevel . "`nResult: " . res

*/

SwitchToSession(iSessionID, sPassWord=""){
	res := DllCall("winsta.dll\WinStationConnectW"
					, "UInt", 0
					, "UInt", iSessionID
					, "UInt", -1
					, "Str", sPassWord
					, "Int", 1)
	If !(res) ; res should be ??? on success
		MsgBox % "ErrorLevel: " . ErrorLevel . "`nResult: " . res

}


SwitchToUser(sUserName, sPassWord=""){
        ; ???
}


HTH

#2 Lexikos

Lexikos
  • Administrators
  • 8844 posts

Posted 27 August 2012 - 08:04 AM

If you use "Ptr" on AutoHotkey Basic, it interprets the P as the pointer suffix, discards anything to the right of it, and defaults to INT because there's nothing left. Basically, "Ptr" acts like "IntP", hence the access violation exception (0xC0000005). You probably meant to use Ptr rather than "Ptr".

The top code tested okay on v1.1.08.01 x64, Windows 7 x64.

#3 daonlyfreez

daonlyfreez
  • Members
  • 995 posts

Posted 27 August 2012 - 11:33 AM

Thanks Lexikos. :)

Changed.