DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Crazy Autohotkey issue  (Read 13,446 times)

jpprater

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 90
    • Participated in NANY Challenge Once
Crazy Autohotkey issue
« on: December 14, 2009, 03:58 PM »
I've already posted about this over at the Autohotkey forum, but maybe someone here has seen this issue before.
I'm having a post-compile issue with one of my scripts.
I have the following two files:
ld.ahk
Code: (autoit)
#include LockDetect.ahk
#NoTrayIcon
IfNotExist, LD_Settings.ini
	IniWrite, ., LD_Settings.ini, logsettings, logpath
IniRead, LogPath, LD_Settings.ini, logsettings, logpath
FormatTime, StartTime,, MM-dd-yyyy_HH-mm-ss
FormatTime, StartTime1,, MM-dd-yyyy HH-mm-ss
PID := DllCall("GetCurrentProcessId")
LogFileName := A_ComputerName . "_" . StartTime . "_" . PID . ".txt"
LogFile := LogPath . "\\" . LogFileName
IfNotExist, %LogPath%
	FileCreateDir, %LogPath%
FileAppend, Process %PID% started at %StartTime1% `n, %LogFile%
notify_lock_unlock()

on_lock()
{
	Global LogFile
	FormatTime, TimeString,, MM-dd-yyyy HH-mm-ss
	FileAppend, Locked at %TimeString% `n, %LogFile%
}

on_unlock()
{
	Global LogFile
	FormatTime, TimeString,, MM-dd-yyyy HH-mm-ss
	FileAppend, Unlocked at %TimeString% `n, %LogFile%
}
And LockDetect.ahk, copied from notifylockunlock.ahk in http://www.autohotkey.com/forum/topic5359.html.
Code: (autoit)
;--------------------------------------------------------------- 
;Notify Lock\Unlock
;   This script monitors LockWorkstation calls
;
;   If a change is detected it 'notifies' the calling script
;      On Lock
;         This script will call function "on_lock()"
;      On Unlock
;         This script will call fucntion "on_unlock()"
;IMPORTANT: The functions "on_lock()" and "on_unlock()" DO NOT
;exist in this script, they are to be created in the script that
;calls notify_lock_unlock() (presumably your main script)
;---------------------------------------------------------------
;Re-purposed by WTO605
;Last edited 2009-08-18 16:34 UTC
;---------------------------------------------------------------
;Based on Winamp_Lock_Pause by MrInferno
;Posted: Fri Apr 21, 2006 4:49 am
;Source: http://www.autohotkey.com/forum/topic9384.html
;---------------------------------------------------------------
;Winamp_Lock_Pause was/is based on script codes from "shimanov"
;Posted: Thu Sep 15, 2005 12:26 am   
;Source: http://www.autohotkey.com/forum/viewtopic.php?t=5359
;Posted: Tue Dec 06, 2005 9:14 pm
;Source: http://www.autohotkey.com/forum/viewtopic.php?t=6755
;---------------------------------------------------------------

;Initialize global constants
WTS_SESSION_LOCK      :=   0x7
WTS_SESSION_UNLOCK      :=   0x8
NOTIFY_FOR_ALL_SESSIONS   :=   1
NOTIFY_FOR_THIS_SESSION   :=   0
WM_WTSSESSION_CHANGE   :=   0x02B1

notify_lock_unlock()
{
   Global WM_WTSSESSION_CHANGE
   Global NOTIFY_FOR_ALL_SESSION

   hw_ahk := FindWindowEx( 0, 0, "AutoHotkey", a_ScriptFullPath " - AutoHotkey v" a_AhkVersion ) 

   OnMessage( WM_WTSSESSION_CHANGE, "Handle_WTSSESSION_CHANGE" ) 

   success := DllCall( "wtsapi32.dll\WTSRegisterSessionNotification", "uint", hw_ahk, "uint", NOTIFY_FOR_ALL_SESSIONS )

   if( ErrorLevel OR ! success ) 
   {
      success := DllCall( "wtsapi32.dll\WTSUnRegisterSessionNotification", "uint", hw_ahk )
      ;If DLL registration fails, wait 20 seconds and try again
      Sleep, 20000
      notify_lock_unlock()
      ;MsgBox, [WTSRegisterSessionNotification] failed: EL = %ErrorLevel%
   }
   return
}

Handle_WTSSESSION_CHANGE( p_w, p_l, p_m, p_hw )
; p_w  = wParam   ;Session state change event
; p_l  = lParam   ;Session ID
; p_m  = Msg   ;WM_WTSSESSION_CHANGE
; p_hw = hWnd   ;Handle to Window
{
   Global WTS_SESSION_LOCK
   Global WTS_SESSION_UNLOCK

   If ( p_w = WTS_SESSION_LOCK ) 
   {
      on_lock()
   }
   Else If ( p_w = WTS_SESSION_UNLOCK ) 
   {
      on_unlock()
   }
}

FindWindowEx( p_hw_parent, p_hw_child, p_class, p_title ) 
{
   return, DllCall( "FindWindowEx", "uint", p_hw_parent, "uint", p_hw_child, "str", p_class, "str", p_title ) 
}
If I run the above ld.ahk, which has #include lockdetect.ahk at the very top, everything runs perfectly.  I get exactly the behavior I expect.
However, if I compile ld.ahk, which operation should build the code in the included lockdetect.ahk into the resulting .exe file, when I run the .exe, I get the behavior expected up until the script should try to run code included from LockDetect.ahk.  This also happens if I copy the code manually from LockDetect.ahk to the top of ld.ahk.
I am using the very latest version of autohotkey, 1.0.48.05.
« Last Edit: December 14, 2009, 04:08 PM by jpprater »

jpprater

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 90
    • Participated in NANY Challenge Once
Re: Crazy Autohotkey issue
« Reply #1 on: December 14, 2009, 05:41 PM »
I've at least figured out where the problem is really happening.  It's nothing to do with the compiler; it's the DllCall in LockDetect.ahk.  That DllCall is failing repeatedly, and only in the compiled version.
Calling DllCall( "GetLastError" ) returns the error code 87, which translates (according to this reference) into INVALID_PARAMETER.  But I'm sure the parameters given in the DllCall are correct!  If the parameters were wrong, why would they only be wrong in the compiled version, and not also in the original interpreted script?  :huh: :'(
« Last Edit: December 14, 2009, 06:23 PM by jpprater »

jpprater

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 90
    • Participated in NANY Challenge Once
Re: Crazy Autohotkey issue
« Reply #2 on: December 14, 2009, 08:28 PM »
Finally figured it out.  The FindWindowEx call in LockDetect.ahk wasn't finding a valid hWnd for the script after compilation.  Changing the values in that call fixed it.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Crazy Autohotkey issue
« Reply #3 on: December 14, 2009, 09:43 PM »
thanks for the explanation, could help others  :up:

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,840
    • Coding Snacks
Re: Crazy Autohotkey issue
« Reply #4 on: December 14, 2009, 10:01 PM »
kudos for sticking at it AND advising the fix :Thmbsup:
Alcohol is photoshop, bottled
Askhole - someone who ask for advice, then ignores it
"Look wise, say nothing, and grunt. Speech was given to conceal thought" - Sir William Osler

jpprater

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 90
    • Participated in NANY Challenge Once
Re: Crazy Autohotkey issue
« Reply #5 on: December 15, 2009, 01:30 AM »
thanks for the explanation, could help others  :up:
kudos for sticking at it AND advising the fix :Thmbsup:
Thank you, mouser and Target. :) The DllCall relied on having a valid hWnd for the script's window, but it was written based on the script running in the AutoHotkey interpreter, not compiled independently.  The class was still the same, but the window title wouldn't match anymore because the title of a compiled Autohotkey script's window becomes the path to the executable (unless otherwise set).

tatysek

  • Participant
  • Joined in 2016
  • *
  • Posts: 2
Re: Crazy Autohotkey issue
« Reply #6 on: January 18, 2016, 10:45 AM »
Finally figured it out.  The FindWindowEx call in LockDetect.ahk wasn't finding a valid hWnd for the script after compilation.  Changing the values in that call fixed it.
@jpprater
What was the change you applied? Can you please post new code? Thanks

jpprater

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 90
    • Participated in NANY Challenge Once
Re: Crazy Autohotkey issue
« Reply #7 on: January 18, 2016, 02:48 PM »
This code right here in LockDetect.ahk:
Code: ( autohotkey)
hw_ahk := FindWindowEx( 0, 0, "AutoHotkey", a_ScriptFullPath " - AutoHotkey v" a_AhkVersion ) 
This had to be changed to accurately reflect the title of the compiled script's window.
I don't have the code available to me just now, but I'll post it tomorrow.  If you compile the script yourself and try to run it, and then open the script's window, you'll see what the proper title to match is.

jpprater

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 90
    • Participated in NANY Challenge Once
Re: Crazy Autohotkey issue
« Reply #8 on: January 19, 2016, 01:36 PM »
This is the updated LockDetect.ahk.  Changes between this version and the original are on line 46 and the additional constants on lines 29-39.
Code: ( autohotkey)
;--------------------------------------------------------------- 
;Notify Lock\Unlock
;   This script monitors LockWorkstation calls
;
;   If a change is detected it 'notifies' the calling script
;      On Lock
;         This script will call function "on_lock()"
;      On Unlock
;         This script will call fucntion "on_unlock()"
;IMPORTANT: The functions "on_lock()" and "on_unlock()" DO NOT
;exist in this script, they are to be created in the script that
;calls notify_lock_unlock() (presumably your main script)
;---------------------------------------------------------------
;Re-purposed by WTO605
;Last edited 2009-08-18 16:34 UTC
;---------------------------------------------------------------
;Based on Winamp_Lock_Pause by MrInferno
;Posted: Fri Apr 21, 2006 4:49 am
;Source: http://www.autohotkey.com/forum/topic9384.html
;---------------------------------------------------------------
;Winamp_Lock_Pause was/is based on script codes from "shimanov"
;Posted: Thu Sep 15, 2005 12:26 am   
;Source: http://www.autohotkey.com/forum/viewtopic.php?t=5359
;Posted: Tue Dec 06, 2005 9:14 pm
;Source: http://www.autohotkey.com/forum/viewtopic.php?t=6755
;---------------------------------------------------------------

;Initialize global constants
WTS_CONSOLE_CONNECT := 0x1
WTS_CONSOLE_DISCONNECT := 0x2
WTS_REMOTE_CONNECT := 0x3
WTS_REMOTE_DISCONNECT := 0x4
WTS_SESSION_LOGON := 0x5
WTS_SESSION_LOGOFF := 0x6
WTS_SESSION_LOCK      :=   0x7
WTS_SESSION_UNLOCK      :=   0x8
NOTIFY_FOR_ALL_SESSIONS   :=   1
NOTIFY_FOR_THIS_SESSION   :=   0
WM_WTSSESSION_CHANGE   :=   0x02B1

notify_lock_unlock()
{
   Global WM_WTSSESSION_CHANGE
   Global NOTIFY_FOR_ALL_SESSION

   hw_ahk := FindWindowEx( 0, 0, "AutoHotkey", A_ScriptFullPath )

   OnMessage( WM_WTSSESSION_CHANGE, "Handle_WTSSESSION_CHANGE" ) 

   success := DllCall( "wtsapi32.dll\WTSRegisterSessionNotification", "uint", hw_ahk, "uint", NOTIFY_FOR_ALL_SESSIONS )

   if( ErrorLevel OR ! success ) 
   {
      success := DllCall( "wtsapi32.dll\WTSUnRegisterSessionNotification", "uint", hw_ahk )
      ;If DLL registration fails, wait 20 seconds and try again
      Sleep, 20000
      notify_lock_unlock()
      ;MsgBox, [WTSRegisterSessionNotification] failed: EL = %ErrorLevel%
   }
   return
}

Handle_WTSSESSION_CHANGE( p_w, p_l, p_m, p_hw )
; p_w  = wParam   ;Session state change event
; p_l  = lParam   ;Session ID
; p_m  = Msg   ;WM_WTSSESSION_CHANGE
; p_hw = hWnd   ;Handle to Window
{
   Global WTS_SESSION_LOCK
   Global WTS_SESSION_UNLOCK
   Global WTS_SESSION_LOGON
   Global WTS_SESSION_LOGOFF

   If ( p_w = WTS_SESSION_LOCK ) 
   {
      on_lock()
   }
   Else If ( p_w = WTS_SESSION_UNLOCK ) 
   {
      on_unlock()
   }
   Else If (p_w = WTS_SESSION_LOGON)
   {
      on_logon()
   }
   Else If (p_w = WTS_SESSION_LOGOFF)
   {
      on_logoff()
   }
}

FindWindowEx( p_hw_parent, p_hw_child, p_class, p_title ) 
{
   return, DllCall( "FindWindowEx", "uint", p_hw_parent, "uint", p_hw_child, "str", p_class, "str", p_title ) 
}

tatysek

  • Participant
  • Joined in 2016
  • *
  • Posts: 2
Re: Crazy Autohotkey issue
« Reply #9 on: January 19, 2016, 08:35 PM »
That was exactly what I was looking for, big thank you!

Original address: https://www.donationcoder.com/forum/index.php?topic=20984.0