Page 1 of 1

Switch user/Login (Vista)

Posted: Thu Jan 31, 2008 11:54 am
by eVAPor8
Hi all,

(Background)
I'm currently running Vista with two accounts, an Admin and Standard - I work as a Standard, but where I'm configuring my new lappy, I'm finding myself having to jump back and forth between accounts. I'm trying to put together a little "User-switcher" button that'll drop me straight into the Admin account (or at least to the "Enter Password" dialog)

(Problem)
Having searched high and low, I cannot find a "login" API anywhere. I cannot believe it doesn't exist somewhere, but google, MSDN, forums search have produces nothing my wild goose-chases so far :(

I've worked out the code to log off, but this only dumps me out as far as the "Choose an account" screen... I'd like to try to get a bit further because Click [>] -> *Switch User* -> *Choose account* -> *Enter password* is both clunky, slow and annoying.

Here's the code I've got so far... Maybe I'm looking in the wrong direction!

Code: Select all

#LIBRARY = 0
If OpenLibrary(#LIBRARY, "WtsApi32.dll")
  CallFunction(#LIBRARY, "WTSDisconnectSession",  0, -1, false)
  CloseLibrary(#LIBRARY)
EndIf
Now I know I could Run As Administrator for a lot of things, but I "BossKey"-type solution would be far more helpful.

Can anyone point me in the right direction? Thanks in advance! :D

Posted: Thu Jan 31, 2008 1:11 pm
by eVAPor8
Still haven't sussed how to set focus to the other user, but I've expanded the code to sit in the SysTray. Left-click to log off, right-click to quit.

Having looked at the Help on CatchImage, I think the
Enumeration
#Image0
EndEnumeration

Block is unnecessary, and also, since I specify the same icon when compiling, I pretty sure there's a much more elegant solution since it seems to me the same bitmap is being stored twice (and I'm a complete PB newbie). Any advice, thoughts or comments would be greatly appreciated.

Code: Select all

Image0 = CatchImage(0, ?Image0)

If OpenWindow(0, 100, 100, 100, 100, "Fast User Switch", #PB_Window_Invisible)
  AddSysTrayIcon(0, WindowID(0), Image0)
  Repeat
    Event = WaitWindowEvent()   
    Select Event
      Case #PB_Event_SysTray
         If EventType() = #PB_EventType_LeftClick
            #LIBRARY = 0
            If OpenLibrary(#LIBRARY, "WtsApi32.dll")
              CallFunction(#LIBRARY, "WTSDisconnectSession",  0, -1, false)
              CloseLibrary(#LIBRARY)
            EndIf
         ElseIf EventType() = #PB_EventType_RightClick
          Break 
         EndIf
     EndSelect 
  Until Event = #PB_Event_CloseWindow
EndIf

End

; // DataSection // ------------------------------------------------- //
DataSection
Image0:
  IncludeBinary "C:\Users\Devel\Desktop\Play.ico"
EndDataSection

Posted: Tue Feb 05, 2008 1:23 am
by SFSxOI
No, your on the right track, and the function your using is correct too. I use it all the time myself. But from what I get from your post, you want to be able to click on your icon in the tray and do it. I just tried your code but used my own icon.

If it left click on the icon it switches users by logging me off then bringing up the log in screen, however, if I right click on it the program just ends. Is that what your getting too...and i gather from code you posted thats the way it operates.

What do you mean by set focus to the other user? You mean click on your icon and select the other user to switch to? In Vista you have to go thru the log in screen to switch users.

Posted: Tue Feb 05, 2008 4:47 pm
by eVAPor8
SFSxOI wrote:No, your on the right track, and the function your using is correct too. I use it all the time myself. But from what I get from your post, you want to be able to click on your icon in the tray and do it. I just tried your code but used my own icon.

If it left click on the icon it switches users by logging me off then bringing up the log in screen, however, if I right click on it the program just ends. Is that what your getting too...and i gather from code you posted thats the way it operates.

What do you mean by set focus to the other user? You mean click on your icon and select the other user to switch to? In Vista you have to go thru the log in screen to switch users.
Yeah, that's what I was a afraid of. As far as the left/right-click operation goes, you've got it on the nail. I could have had tooltips/balloons/menus, etc - but call me lazy - the program's got to do two things: switch user and quit with minimal fuss/effort.

After reading MSDN and finding LogonUser() I tried adding

Code: Select all

Debug(LogonUser_("eVAPor8",#Null,"P455w0rd",  #LOGON32_LOGON_INTERACTIVE , #LOGON32_PROVIDER_DEFAULT, #Null))
But to no avail. I'm obviously missing something (the docs allude to the logonuser function begin for impersonation for processes rather than logging fully in to the desktop... then again maybe the M$ docs are missing something... it would hardly be the first time! :lol:

And before someone chimes in.... No, that's NOT my passy up there!!! :lol: