topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 5:57 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Crazy Autohotkey issue  (Read 9471 times)

jpprater

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 90
    • View Profile
    • Donate to Member
Crazy Autohotkey issue
« on: December 14, 2009, 09:58 AM »
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 [Select]
  1. #include LockDetect.ahk
  2. #NoTrayIcon
  3. IfNotExist, LD_Settings.ini
  4.         IniWrite, ., LD_Settings.ini, logsettings, logpath
  5. IniRead, LogPath, LD_Settings.ini, logsettings, logpath
  6. FormatTime, StartTime,, MM-dd-yyyy_HH-mm-ss
  7. FormatTime, StartTime1,, MM-dd-yyyy HH-mm-ss
  8. PID := DllCall("GetCurrentProcessId")
  9. LogFileName := A_ComputerName . "_" . StartTime . "_" . PID . ".txt"
  10. LogFile := LogPath . "\\" . LogFileName
  11. IfNotExist, %LogPath%
  12.         FileCreateDir, %LogPath%
  13. FileAppend, Process %PID% started at %StartTime1% `n, %LogFile%
  14. notify_lock_unlock()
  15.  
  16. on_lock()
  17. {
  18.         Global LogFile
  19.         FormatTime, TimeString,, MM-dd-yyyy HH-mm-ss
  20.         FileAppend, Locked at %TimeString% `n, %LogFile%
  21. }
  22.  
  23. on_unlock()
  24. {
  25.         Global LogFile
  26.         FormatTime, TimeString,, MM-dd-yyyy HH-mm-ss
  27.         FileAppend, Unlocked at %TimeString% `n, %LogFile%
  28. }
And LockDetect.ahk, copied from notifylockunlock.ahk in http://www.autohotke...forum/topic5359.html.
Code: AutoIt [Select]
  1. ;---------------------------------------------------------------
  2. ;Notify Lock\Unlock
  3. ;   This script monitors LockWorkstation calls
  4. ;
  5. ;   If a change is detected it 'notifies' the calling script
  6. ;      On Lock
  7. ;         This script will call function "on_lock()"
  8. ;      On Unlock
  9. ;         This script will call fucntion "on_unlock()"
  10. ;IMPORTANT: The functions "on_lock()" and "on_unlock()" DO NOT
  11. ;exist in this script, they are to be created in the script that
  12. ;calls notify_lock_unlock() (presumably your main script)
  13. ;---------------------------------------------------------------
  14. ;Re-purposed by WTO605
  15. ;Last edited 2009-08-18 16:34 UTC
  16. ;---------------------------------------------------------------
  17. ;Based on Winamp_Lock_Pause by MrInferno
  18. ;Posted: Fri Apr 21, 2006 4:49 am
  19. ;Source: http://www.autohotkey.com/forum/topic9384.html
  20. ;---------------------------------------------------------------
  21. ;Winamp_Lock_Pause was/is based on script codes from "shimanov"
  22. ;Posted: Thu Sep 15, 2005 12:26 am  
  23. ;Source: http://www.autohotkey.com/forum/viewtopic.php?t=5359
  24. ;Posted: Tue Dec 06, 2005 9:14 pm
  25. ;Source: http://www.autohotkey.com/forum/viewtopic.php?t=6755
  26. ;---------------------------------------------------------------
  27.  
  28. ;Initialize global constants
  29. WTS_SESSION_LOCK      :=   0x7
  30. WTS_SESSION_UNLOCK      :=   0x8
  31. NOTIFY_FOR_ALL_SESSIONS   :=   1
  32. NOTIFY_FOR_THIS_SESSION   :=   0
  33. WM_WTSSESSION_CHANGE   :=   0x02B1
  34.  
  35. notify_lock_unlock()
  36. {
  37.    Global WM_WTSSESSION_CHANGE
  38.    Global NOTIFY_FOR_ALL_SESSION
  39.  
  40.    hw_ahk := FindWindowEx( 0, 0, "AutoHotkey", a_ScriptFullPath " - AutoHotkey v" a_AhkVersion )
  41.  
  42.    OnMessage( WM_WTSSESSION_CHANGE, "Handle_WTSSESSION_CHANGE" )
  43.  
  44.    success := DllCall( "wtsapi32.dll\WTSRegisterSessionNotification", "uint", hw_ahk, "uint", NOTIFY_FOR_ALL_SESSIONS )
  45.  
  46.    if( ErrorLevel OR ! success )
  47.    {
  48.       success := DllCall( "wtsapi32.dll\WTSUnRegisterSessionNotification", "uint", hw_ahk )
  49.       ;If DLL registration fails, wait 20 seconds and try again
  50.       Sleep, 20000
  51.       notify_lock_unlock()
  52.       ;MsgBox, [WTSRegisterSessionNotification] failed: EL = %ErrorLevel%
  53.    }
  54.    return
  55. }
  56.  
  57. Handle_WTSSESSION_CHANGE( p_w, p_l, p_m, p_hw )
  58. ; p_w  = wParam   ;Session state change event
  59. ; p_l  = lParam   ;Session ID
  60. ; p_m  = Msg   ;WM_WTSSESSION_CHANGE
  61. ; p_hw = hWnd   ;Handle to Window
  62. {
  63.    Global WTS_SESSION_LOCK
  64.    Global WTS_SESSION_UNLOCK
  65.  
  66.    If ( p_w = WTS_SESSION_LOCK )
  67.    {
  68.       on_lock()
  69.    }
  70.    Else If ( p_w = WTS_SESSION_UNLOCK )
  71.    {
  72.       on_unlock()
  73.    }
  74. }
  75.  
  76. FindWindowEx( p_hw_parent, p_hw_child, p_class, p_title )
  77. {
  78.    return, DllCall( "FindWindowEx", "uint", p_hw_parent, "uint", p_hw_child, "str", p_class, "str", p_title )
  79. }
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, 10:08 AM by jpprater »

jpprater

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 90
    • View Profile
    • Donate to Member
Re: Crazy Autohotkey issue
« Reply #1 on: December 14, 2009, 11:41 AM »
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, 12:23 PM by jpprater »

jpprater

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 90
    • View Profile
    • Donate to Member
Re: Crazy Autohotkey issue
« Reply #2 on: December 14, 2009, 02: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,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Crazy Autohotkey issue
« Reply #3 on: December 14, 2009, 03:43 PM »
thanks for the explanation, could help others  :up:

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,832
    • View Profile
    • Donate to Member
Re: Crazy Autohotkey issue
« Reply #4 on: December 14, 2009, 04:01 PM »
kudos for sticking at it AND advising the fix :Thmbsup:

jpprater

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 90
    • View Profile
    • Donate to Member
Re: Crazy Autohotkey issue
« Reply #5 on: December 14, 2009, 07:30 PM »
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
  • *
  • default avatar
  • Posts: 2
    • View Profile
    • Donate to Member
Re: Crazy Autohotkey issue
« Reply #6 on: January 18, 2016, 04: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
    • View Profile
    • Donate to Member
Re: Crazy Autohotkey issue
« Reply #7 on: January 18, 2016, 08:48 AM »
This code right here in LockDetect.ahk:
Code: Autohotkey [Select]
  1. 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
    • View Profile
    • Donate to Member
Re: Crazy Autohotkey issue
« Reply #8 on: January 19, 2016, 07:36 AM »
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 [Select]
  1. ;---------------------------------------------------------------
  2. ;Notify Lock\Unlock
  3. ;   This script monitors LockWorkstation calls
  4. ;
  5. ;   If a change is detected it 'notifies' the calling script
  6. ;      On Lock
  7. ;         This script will call function "on_lock()"
  8. ;      On Unlock
  9. ;         This script will call fucntion "on_unlock()"
  10. ;IMPORTANT: The functions "on_lock()" and "on_unlock()" DO NOT
  11. ;exist in this script, they are to be created in the script that
  12. ;calls notify_lock_unlock() (presumably your main script)
  13. ;---------------------------------------------------------------
  14. ;Re-purposed by WTO605
  15. ;Last edited 2009-08-18 16:34 UTC
  16. ;---------------------------------------------------------------
  17. ;Based on Winamp_Lock_Pause by MrInferno
  18. ;Posted: Fri Apr 21, 2006 4:49 am
  19. ;Source: http://www.autohotkey.com/forum/topic9384.html
  20. ;---------------------------------------------------------------
  21. ;Winamp_Lock_Pause was/is based on script codes from "shimanov"
  22. ;Posted: Thu Sep 15, 2005 12:26 am  
  23. ;Source: http://www.autohotkey.com/forum/viewtopic.php?t=5359
  24. ;Posted: Tue Dec 06, 2005 9:14 pm
  25. ;Source: http://www.autohotkey.com/forum/viewtopic.php?t=6755
  26. ;---------------------------------------------------------------
  27.  
  28. ;Initialize global constants
  29. WTS_CONSOLE_CONNECT := 0x1
  30. WTS_CONSOLE_DISCONNECT := 0x2
  31. WTS_REMOTE_CONNECT := 0x3
  32. WTS_REMOTE_DISCONNECT := 0x4
  33. WTS_SESSION_LOGON := 0x5
  34. WTS_SESSION_LOGOFF := 0x6
  35. WTS_SESSION_LOCK      :=   0x7
  36. WTS_SESSION_UNLOCK      :=   0x8
  37. NOTIFY_FOR_ALL_SESSIONS   :=   1
  38. NOTIFY_FOR_THIS_SESSION   :=   0
  39. WM_WTSSESSION_CHANGE   :=   0x02B1
  40.  
  41. notify_lock_unlock()
  42. {
  43.    Global WM_WTSSESSION_CHANGE
  44.    Global NOTIFY_FOR_ALL_SESSION
  45.  
  46.    hw_ahk := FindWindowEx( 0, 0, "AutoHotkey", A_ScriptFullPath )
  47.  
  48.    OnMessage( WM_WTSSESSION_CHANGE, "Handle_WTSSESSION_CHANGE" )
  49.  
  50.    success := DllCall( "wtsapi32.dll\WTSRegisterSessionNotification", "uint", hw_ahk, "uint", NOTIFY_FOR_ALL_SESSIONS )
  51.  
  52.    if( ErrorLevel OR ! success )
  53.    {
  54.       success := DllCall( "wtsapi32.dll\WTSUnRegisterSessionNotification", "uint", hw_ahk )
  55.       ;If DLL registration fails, wait 20 seconds and try again
  56.       Sleep, 20000
  57.       notify_lock_unlock()
  58.       ;MsgBox, [WTSRegisterSessionNotification] failed: EL = %ErrorLevel%
  59.    }
  60.    return
  61. }
  62.  
  63. Handle_WTSSESSION_CHANGE( p_w, p_l, p_m, p_hw )
  64. ; p_w  = wParam   ;Session state change event
  65. ; p_l  = lParam   ;Session ID
  66. ; p_m  = Msg   ;WM_WTSSESSION_CHANGE
  67. ; p_hw = hWnd   ;Handle to Window
  68. {
  69.    Global WTS_SESSION_LOCK
  70.    Global WTS_SESSION_UNLOCK
  71.    Global WTS_SESSION_LOGON
  72.    Global WTS_SESSION_LOGOFF
  73.  
  74.    If ( p_w = WTS_SESSION_LOCK )
  75.    {
  76.       on_lock()
  77.    }
  78.    Else If ( p_w = WTS_SESSION_UNLOCK )
  79.    {
  80.       on_unlock()
  81.    }
  82.    Else If (p_w = WTS_SESSION_LOGON)
  83.    {
  84.       on_logon()
  85.    }
  86.    Else If (p_w = WTS_SESSION_LOGOFF)
  87.    {
  88.       on_logoff()
  89.    }
  90. }
  91.  
  92. FindWindowEx( p_hw_parent, p_hw_child, p_class, p_title )
  93. {
  94.    return, DllCall( "FindWindowEx", "uint", p_hw_parent, "uint", p_hw_child, "str", p_class, "str", p_title )
  95. }

tatysek

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 2
    • View Profile
    • Donate to Member
Re: Crazy Autohotkey issue
« Reply #9 on: January 19, 2016, 02:35 PM »
That was exactly what I was looking for, big thank you!