Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Could I use Autohotkey for windows logon?


  • Please log in to reply
23 replies to this topic
david151
  • Members
  • 29 posts
  • Last active: May 22 2011 09:37 PM
  • Joined: 29 Apr 2011
I have a media centre pc (windows XP) which I control with my Android phone via Bluetooth. The phone acts as a keyboard and mouse, giving me full control over my system. The only thing I am unable to do is login to windows at the password screen. In pursuit of doing this I setup my remote control software as a windows service, meaning the program is fully initialised at the login screen. I am able to pair successfully with my phone and use it to browse the hard drive and even run programs. The only thing I can’t do is use it to type in my password and as a result am stuck at the login screen. I am told that windows has a security feature that prevents this. What I wondered was whether I could use my phone to send a hotkey to autohotkey which would type in my password enabling me to get past the login screen to access my system? (remember I can already access programs and browse my hard drive, just not type my password)

I know this is not a very secure way of logging in, however it is just to stop my children messing about on the media centre when I am not there. I would really like to do away with my keyboard, as I really don’t need it on the media centre PC. I can already do everything I want with my phone as a remote and this is the only thing stopping me from having to have a keyboard sitting around.

Would my idea work? If so does anyone have any idea of the code I would need since I am a real beginner with Autohotkey.

Thanks for your help

Dave

dmg
  • Members
  • 2395 posts
  • Last active: Nov 04 2015 06:46 AM
  • Joined: 19 Nov 2010
I am by no means an expert, but I am 95% sure it would not work. Windows is set up to prevent just the type of thing you want. Whether you are trying to log in through your phone or by AHk basically the same thing is happening, a program installed in Windows is trying to type in the login screen and Windows won't allow it.

Anyone else know a way around this???
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion

-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact

  • Guests
  • Last active:
  • Joined: --
Couldn't you...
[*:da9xdnyz]Remove the password from the account on Windows (so it auto logs in)
[*:da9xdnyz]Leave the password set, but set Windows to Auto-Login (there are some registry keys for that)

...which I control with my Android phone via Bluetooth.

...however it is just to stop my children messing about on the media centre when I am not there.

...just wondering, but if the MC PC has no KB/Mouse (or you want it to end up that way), then how are the children gonna mess with it? If you protect your Android phone (take it with you), then no one can mess with the PC, even if it Auto-Logs in (unless they bring their own KB/Mouse).

Also, if the Android phone can "send hotkeys" why can't it "type" the password?

(remember I can already access programs and browse my hard drive, just not type my password)

...note that any programs you run like this are running as LocalService or SYSTEM...or something, but it's not running as your Username, so the programs are not saving settings in the same place, as if you logged in normally.

I'm having a hard time understanding how you are controlling it with the phone?...if you open Notepad, can you type there? Can you start at the beginning on how you set this up? What program on the phone are you running to "control" the computer? (& what program on the computer is hosting the phone?). Is Windows set to login with the "Welcome Screen" or do you need to press Ctrl+Alt+Del to login?

david151
  • Members
  • 29 posts
  • Last active: May 22 2011 09:37 PM
  • Joined: 29 Apr 2011
I don’t want to auto login as then I have no security, all the kids have to do is plug in a keyboard or mouse. The reason I can’t simply use my phone to type my password at the login screen is due to some sort of windows security mechanism built into windows which prevents this (unless I am told the developers have paid Microsoft to sign the program or it is using a custom driver).

I use premotedroid which is a server app that is installed on the windows machine

<!-- m -->http://code.google.com/p/premotedroid/<!-- m -->

I can set this up as a windows service so it is enabled before the computer fully boots to the desktop and I have communication between the server and my phone at the login screen but can’t type to login. All I can do is use the premotedroid file browser on my phone to view my computers directory structure and start programs etc. I can’t see the programs running that I open as they are running behind the login screen that I am stuck at. I can’t do anything meaningful as all I see is the login screen with all running processes hidden behind this. My thinking is if I can send a hotkey to Autohotkey, it could run a script to log me on with my password. Does anyone know how I would set this up and if it is feasible?

I don’t have to press Ctrl, Alt and Del to login, I just have the welcome screen where I type my password.

dmg
  • Members
  • 2395 posts
  • Last active: Nov 04 2015 06:46 AM
  • Joined: 19 Nov 2010
I would like to propose a possible alternative. Instead of using AHk to log you into Windows, you could use AHk itself to lock down your computer until you send a code through the remote. It would take a bit of coding but it CAN be done.
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion

-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
This seems to work for me on Windows XP:
D("-- " A_Now)

if GetDesktopName() != "WinSta0\Winlogon"
{
    D("relaunching")
    ; Relaunch self on Winlogon desktop:
    command = "%A_AhkPath%" "%A_ScriptFullPath%"
    if !RunOnDesktop("WinSta0\Winlogon", command)
        D("failed - error " A_LastError)
    ExitApp
}

WinWait Log On to Windows,, 5
if ErrorLevel
	D("timed out")
else {
	D("found window!!!")
	; Send password{Enter}
}

D(t) { ; For debugging.
	FileAppend %t%`n, C:\testing.txt
}

RunOnDesktop(desktop, command) {
    si_size := A_PtrSize=8 ? 104 : 68
    VarSetCapacity(pi, 24, 0), VarSetCapacity(si, si_size, 0)
    NumPut(&desktop, NumPut(0, NumPut(si_size, si))) ; cb, lpReserved, lpDesktop (right-to-left)
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    if !DllCall("CreateProcess", ptr, 0, "str", command, ptr, 0, ptr, 0
            , "int", 0, "uint", 0, ptr, 0, ptr, 0, ptr, &si, ptr, &pi)
        return false
    DllCall("CloseHandle", ptr, NumGet(pi))
    DllCall("CloseHandle", ptr, NumGet(pi, A_PtrSize=8 ? 8 : 4))
    return true
}

GetDesktopName() {
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    if (winsta := GetUserObjectName(DllCall("GetProcessWindowStation", ptr)))
    && (desktop := GetUserObjectName(DllCall("GetThreadDesktop", "uint", DllCall("GetCurrentThreadId", "uint"), ptr)))
        return winsta "\" desktop
}

GetUserObjectName(h) {
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    VarSetCapacity(buf, 1024)
    if DllCall("GetUserObjectInformation", ptr, h, "int", UOI_NAME:=2
                                    , "str", buf, "uint", 1024, ptr, 0)
        return buf
}
I ran the script as a scheduled task at system startup as SYSTEM. Running it as a service would probably work just as well. Either way, it probably won't work on Vista or later.

david151
  • Members
  • 29 posts
  • Last active: May 22 2011 09:37 PM
  • Joined: 29 Apr 2011
Hi Lexikos

Thanks for your reply. What you have posted sounds perfect as I only want to use it on XP. I was just looking at your code, but for someone with very basic knowledge of AHK it looks really complex. Please could I just ask a couple of really simple questions?

1) Where would I put my windows password on the script?
2) If I want to run this script on the press of a hotkey is that possible? Where would I put this e.g. F1?

If there anything else I need to be aware of when running the script?

Thanks Dave

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
The following block of code is executed after the script has determined it is running on the Winlogon desktop:
WinWait Log On to Windows,, 5
if ErrorLevel
   D("timed out")
else {
   D("found window!!!")
   ; Send password{Enter}
}
You can replace it with whatever you want. A hotkey, for instance.

david151
  • Members
  • 29 posts
  • Last active: May 22 2011 09:37 PM
  • Joined: 29 Apr 2011
Hi Lexikos

I am really confused about how you are making this script work. I have tried setting it up as a scheduled task, however it does not log into windows at the login screen – nothing happens when I press the hotkey(F1). I have also tried setting it up as a service, but with the same result. I have tried setting things up with the ahk file and also converted it to exe with Ahk2Exe.exe; however nothing I try seems to work. Please see the exact script I am using with my trial password (1234)
– am I doing anything wrong? Any ideas why the script is not working? What are you doing exactly to make it work?

Thanks Dave

F1::
D("-- " A_Now)

if GetDesktopName() != "WinSta0\Winlogon"
{
    D("relaunching")
    ; Relaunch self on Winlogon desktop:
    command = "%A_AhkPath%" "%A_ScriptFullPath%"
    if !RunOnDesktop("WinSta0\Winlogon", command)
        D("failed - error " A_LastError)
    ExitApp
}

WinWait Log On to Windows,, 5
if ErrorLevel
   D("timed out")
else {
   D("found window!!!")
   ; 1234{Enter}
}

D(t) { ; For debugging.
   FileAppend %t%`n, C:\testing.txt
}

RunOnDesktop(desktop, command) {
    si_size := A_PtrSize=8 ? 104 : 68
    VarSetCapacity(pi, 24, 0), VarSetCapacity(si, si_size, 0)
    NumPut(&desktop, NumPut(0, NumPut(si_size, si))) ; cb, lpReserved, lpDesktop (right-to-left)
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    if !DllCall("CreateProcess", ptr, 0, "str", command, ptr, 0, ptr, 0
            , "int", 0, "uint", 0, ptr, 0, ptr, 0, ptr, &si, ptr, &pi)
        return false
    DllCall("CloseHandle", ptr, NumGet(pi))
    DllCall("CloseHandle", ptr, NumGet(pi, A_PtrSize=8 ? 8 : 4))
    return true
}

GetDesktopName() {
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    if (winsta := GetUserObjectName(DllCall("GetProcessWindowStation", ptr)))
    && (desktop := GetUserObjectName(DllCall("GetThreadDesktop", "uint", DllCall("GetCurrentThreadId", "uint"), ptr)))
        return winsta "\" desktop
}

GetUserObjectName(h) {
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    VarSetCapacity(buf, 1024)
    if DllCall("GetUserObjectInformation", ptr, h, "int", UOI_NAME:=2
                                    , "str", buf, "uint", 1024, ptr, 0)
        return buf
}


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
My script works like so:
[*:1j0rgepq]It's launched in the context of a service.
[*:1j0rgepq]It determines it is not running on the Winlogon desktop, so it launches another instance of itself (which runs on the Winlogon desktop) and exits.
[*:1j0rgepq]The new instance is running on the Winlogon desktop, so it continues (to WinWait, etc.).Your script fails like so:
[*:1j0rgepq]It's lanched in the context of a service.
[*:1j0rgepq]The auto-execute section ends at the F1 hotkey, so the script does nothing.
[*:1j0rgepq]There cannot be any keyboard input in this context, so the hotkey will never execute.Try replacing only the section shown in my previous post, as I suggested. Do not put anything above the if GetDesktopName() != "WinSta0\Winlogon" block.

Alternatively, have this run at system startup and put your hotkey in a separate script:
command = "%A_AhkPath%" "[color=darkred]Path to some other script.ahk[/color]"
RunOnDesktop("WinSta0\Winlogon", command)

; (Insert definition of RunOnDesktop here)


david151
  • Members
  • 29 posts
  • Last active: May 22 2011 09:37 PM
  • Joined: 29 Apr 2011
Hi Lexikos

I really appreciate your help, however I am having very limited success. I did exactly what you said and took out everything above "if GetDesktopName" The only thing I changed was putting in 1234 for the password. The Scrip does nothing and fails to log me on at startup. I even tried it with entering the direct paths where the script says “%A_AhkPath%" "%A_ScriptFullPath%"”, however this did not rectify anything. Here is the exact script I am running:

if GetDesktopName() != "WinSta0\Winlogon"
{
    D("relaunching")
    ; Relaunch self on Winlogon desktop:
    command = "%A_AhkPath%" "%A_ScriptFullPath%"
    if !RunOnDesktop("WinSta0\Winlogon", command)
        D("failed - error " A_LastError)
    ExitApp
}

WinWait Log On to Windows,, 5
if ErrorLevel
   D("timed out")
else {
   D("found window!!!")
   ; Send 1234{Enter}
}

D(t) { ; For debugging.
   FileAppend %t%`n, C:\testing.txt
}

RunOnDesktop(desktop, command) {
    si_size := A_PtrSize=8 ? 104 : 68
    VarSetCapacity(pi, 24, 0), VarSetCapacity(si, si_size, 0)
    NumPut(&desktop, NumPut(0, NumPut(si_size, si))) ; cb, lpReserved, lpDesktop (right-to-left)
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    if !DllCall("CreateProcess", ptr, 0, "str", command, ptr, 0, ptr, 0
            , "int", 0, "uint", 0, ptr, 0, ptr, 0, ptr, &si, ptr, &pi)
        return false
    DllCall("CloseHandle", ptr, NumGet(pi))
    DllCall("CloseHandle", ptr, NumGet(pi, A_PtrSize=8 ? 8 : 4))
    return true
}

GetDesktopName() {
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    if (winsta := GetUserObjectName(DllCall("GetProcessWindowStation", ptr)))
    && (desktop := GetUserObjectName(DllCall("GetThreadDesktop", "uint", DllCall("GetCurrentThreadId", "uint"), ptr)))
        return winsta "\" desktop
}

GetUserObjectName(h) {
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    VarSetCapacity(buf, 1024)
    if DllCall("GetUserObjectInformation", ptr, h, "int", UOI_NAME:=2
                                    , "str", buf, "uint", 1024, ptr, 0)
        return buf
}

Secondly I tried your instructions in the last message regarding putting a hotkey in a separate script. Again this does not automatically work. The only way I have managed to get things half working is if I use my phone at the login screen to browse for the script and run it. It then comes up with a message saying:

"An older instance of this script is already running. Replace it with this instance?"

If I click yes to replace the script it miraculously works and I can login with the hotkey specified in the separate script. The message still comes up even if I disable the script for auto start through deleting the service or schedule task. I have read I could use the command "#SingleInstance force" - but that is not addressing why I have this problem? What is going wrong? Here are the scripts I use for this method:


command = "%A_AhkPath%" "C:\logon.ahk"
RunOnDesktop("WinSta0\Winlogon", command)

if GetDesktopName() != "WinSta0\Winlogon"
{
    D("relaunching")
    ; Relaunch self on Winlogon desktop:
    command = "%A_AhkPath%" "%A_ScriptFullPath%"
    if !RunOnDesktop("WinSta0\Winlogon", command)
        D("failed - error " A_LastError)
    ExitApp
}

WinWait Log On to Windows,, 5
if ErrorLevel
   D("timed out")
else {
   D("found window!!!")
   ; Send 1234{Enter}
}

D(t) { ; For debugging.
   FileAppend %t%`n, C:\testing.txt
}

RunOnDesktop(desktop, command) {
    si_size := A_PtrSize=8 ? 104 : 68
    VarSetCapacity(pi, 24, 0), VarSetCapacity(si, si_size, 0)
    NumPut(&desktop, NumPut(0, NumPut(si_size, si))) ; cb, lpReserved, lpDesktop (right-to-left)
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    if !DllCall("CreateProcess", ptr, 0, "str", command, ptr, 0, ptr, 0
            , "int", 0, "uint", 0, ptr, 0, ptr, 0, ptr, &si, ptr, &pi)
        return false
    DllCall("CloseHandle", ptr, NumGet(pi))
    DllCall("CloseHandle", ptr, NumGet(pi, A_PtrSize=8 ? 8 : 4))
    return true
}

GetDesktopName() {
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    if (winsta := GetUserObjectName(DllCall("GetProcessWindowStation", ptr)))
    && (desktop := GetUserObjectName(DllCall("GetThreadDesktop", "uint", DllCall("GetCurrentThreadId", "uint"), ptr)))
        return winsta "\" desktop
}

GetUserObjectName(h) {
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    VarSetCapacity(buf, 1024)
    if DllCall("GetUserObjectInformation", ptr, h, "int", UOI_NAME:=2
                                    , "str", buf, "uint", 1024, ptr, 0)
        return buf
}


F3::
Send 1234{Enter}

I would be really grateful if you could let me know what is going wrong when I try the method to automatically logon and also when I try the hotkey method. I know I may sound a little dumb, however I have spent a long time looking at this and don’t know what step to take next.

Thanks Dave

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

; Send 1234{Enter}

Semicolon ( ; ) is AutoHotkey's comment flag. That line doesn't do anything.

"An older instance of this script is already running. Replace it with this instance?"

I'm fairly certain that means the script was already running on that same desktop. I don't see why it wouldn't work before reloading. :?

The message still comes up even if I disable the script for auto start through deleting the service or schedule task.

You are restarting the PC, right? If so, that should be impossible.

Okay, this is what I meant:
; task.ahk

command = "%A_AhkPath%" "C:\logon.ahk"
RunOnDesktop("WinSta0\Winlogon", command)

RunOnDesktop(desktop, command) {
    si_size := A_PtrSize=8 ? 104 : 68
    VarSetCapacity(pi, 24, 0), VarSetCapacity(si, si_size, 0)
    NumPut(&desktop, NumPut(0, NumPut(si_size, si))) ; cb, lpReserved, lpDesktop (right-to-left)
    ptr := A_PtrSize ? "ptr" : "uint" ; for compatibility
    if !DllCall("CreateProcess", ptr, 0, "str", command, ptr, 0, ptr, 0
            , "int", 0, "uint", 0, ptr, 0, ptr, 0, ptr, &si, ptr, &pi)
        return false
    DllCall("CloseHandle", ptr, NumGet(pi))
    DllCall("CloseHandle", ptr, NumGet(pi, A_PtrSize=8 ? 8 : 4))
    return true
}
; logon.ahk

MsgBox login
F1::MsgBox F1
task.ahk is scheduled to run at system startup using the command line "C:\Program Files\AutoHotkey\AutoHotkey.exe" "C:\task.ahk", as the user NT AUTHORITY\SYSTEM. It will not work if you run it as a regular user. That's all I did.

david151
  • Members
  • 29 posts
  • Last active: May 22 2011 09:37 PM
  • Joined: 29 Apr 2011
Hi Lexikos

Things are working better now, although there are 3 things I want to clarify:


1) Thanks for posting the code for task.ahk. The strange thing about this is when pressing the F3 key to logon sometimes it works and sometimes it doesn’t?

2) How come you posted the logon.ahk - as there is no password? My logon.ahk is simply

F3::
Send 1234{Enter}

This seems to work, what was your logon.ahk for?

3) As specified above, automatic execution does not always work. What does seem to work 100% of the time is using my file browser on the phone to manually click on task.ahk to start. If I do this is it possible to have logon.ahk automatically issue the command without having to type F3?


Send 1234{Enter}

Thanks Dave

dmg
  • Members
  • 2395 posts
  • Last active: Nov 04 2015 06:46 AM
  • Joined: 19 Nov 2010
You can put the command in the AutoExecute section of the script, or set a timer to send the command a certain number of seconds after the script loads.
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion

-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

what was your logon.ahk for?

To demonstrate that the/a script is being run and is able to interact with the logon screen.

If I do this is it possible to have logon.ahk automatically issue the command without having to type F3?

Sure. As far as I know, there's nothing more to it than the line of code you posted. But why would you want to? If you want the system to log on automatically, there are far better and more official ways of doing it. For instance, go to Start, Run, type control userpasswords2, hit Enter, and uncheck the box that says Users must enter a user name and password to use this computer.