Jump to content

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

Strange behaviour of ahkgetvar


  • Please log in to reply
3 replies to this topic
ursamajoris
  • New members
  • 3 posts
  • Last active: Sep 14 2016 09:47 AM
  • Joined: 03 Aug 2015

I need to detect end of script execution and read two varables but ahkgetvar returns very strange values.

 

Here is my test script:

ScriptResultName:="TestValue"
ScriptResultValue:=100
 
Loop, 5
{
  SoundBeep, 1500, 200
  ScriptResultValue:=ScriptResultValue + 1
  Sleep 500
}
 
ScriptReady:=1
Sleep 100
 
 
Here is code of my application:

DWORD WINAPI AHKEngineThread()
{
  const HINSTANCE hAutoHotkeyDLL = LoadLibrary(m_AHKEngineDLLFileName);
  if (!hAutoHotkeyDLL) 
  {
    DebugLog(_T("AHKEngineThread::Failed to load Autohotkey engine!"));
    AfxEndThread(0);
    return 0;
  }
 
  const FuncPtr_ahkdll     AutoHotkeyRunScriptFromFile   = (FuncPtr_ahkdll)GetProcAddress(hAutoHotkeyDLL, "ahkdll");
  const FuncPtr_ahktextdll AutoHotkeyRunScriptFromString = (FuncPtr_ahktextdll)GetProcAddress(hAutoHotkeyDLL, "ahktextdll");
  const FuncPtr_ahkReady   AutoHotkeyIsScriptRunning     = (FuncPtr_ahkReady)GetProcAddress(hAutoHotkeyDLL, "ahkReady");
  const FuncPtr_ahkassign  AutoHotkeySetVariable         = (FuncPtr_ahkassign)GetProcAddress(hAutoHotkeyDLL, "ahkassign");
  const FuncPtr_ahkgetvar  AutoHotkeyGetVariable         = (FuncPtr_ahkgetvar)GetProcAddress(hAutoHotkeyDLL, "ahkgetvar");
  
  AutoHotkeyRunScriptFromFile(AHKEngineScriptFilePath, NULL, NULL);
 
  bool ReadValue = true;
  while (AutoHotkeyIsScriptRunning())
  {
    Sleep(25);
 
    LPCTSTR ScriptReady = AutoHotkeyGetVariable(_T("ScriptReady"), FALSE);
    if ((ReadValue) && (ScriptReady != NULL))
    {
      if (_tcscmp(ScriptReady, _T("1")) == 0)
      {
        LPCTSTR ScriptResultName  = AutoHotkeyGetVariable(_T("ScriptResultName"), FALSE);
        LPCTSTR ScriptResultValue = AutoHotkeyGetVariable(_T("ScriptResultValue"), FALSE);
 
        if ((ScriptResultName != NULL) && (ScriptResultValue != NULL))
        {
          DebugLog(_T("Script result: Name: %s Value: %s"), ScriptResultName, ScriptResultValue);
        }
 
        ReadValue = false;
      }
    }
  }
  
  FreeLibrary(hAutoHotkeyDLL);
  AfxEndThread(0);
  return 0;
}
 
In debug output result is:
Script result: Name: 105 Value: 105

 

if ScriptResultValue is read before ScriptResultName result is:

Script result: Name: TestValue Value: TestValue

 

So I am really confused. What is thу right way to read integer and string values from script engine?



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

Please use [ code ] tags.

The problem is in DebugLog I think, try DebugLog(_T("Script result: Name: %1s Value: %2s"), ScriptResultName, ScriptResultValue); or check values in debugger manually or test in AutoHotkey_H.

Also make sure you are using latest version.

Btw. ahkgetvar always returns a pointer to string, which is an empty string ("") when variable does not exist or variable is empty!

AhkThread:=AhkThread("
(
ScriptResultName:=""TestValue""
ScriptResultValue:=100
 
Loop, 5
{
  SoundBeep, 1500, 200
  ScriptResultValue:=ScriptResultValue + 1
  Sleep 500
}
 
ScriptReady:=1
Sleep 100
)")

readValue:=true
While AhkThread.ahkReady(){
	Sleep 25
	ScriptReady:=AhkThread.ahkgetvar("ScriptReady")
	if (readValue && ScriptReady){
		if (ScriptReady){
			ScriptResultName := AhkThread.ahkgetvar("ScriptResultName"),ScriptResultValue := AhkThread.ahkgetvar("ScriptResultValue")
			if (ScriptResultName && ScriptResultValue)
				ToolTip % "Script result: Name: " ScriptResultName " Value: " ScriptResultValue
			readValue:=false
		}
	}
}
MsgBox End


ursamajoris
  • New members
  • 3 posts
  • Last active: Sep 14 2016 09:47 AM
  • Joined: 03 Aug 2015

 

The problem is in DebugLog I think, try DebugLog(_T("Script result: Name: %1s Value: %2s"), ScriptResultName, ScriptResultValue); or check values in debugger manually or test in AutoHotkey_H.

Also make sure you are using latest version.

Btw. ahkgetvar always returns a pointer to string, which is an empty string ("") when variable does not exist or variable is empty!


 

Thanks for quick reply!

I have found the reason of strange behaviour: each call of ahkgetvar returns pointer to the same buffer, but value of buffer is changed after each call.

So result of ahkgetvar should be saved by strcpy to another buffer.



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

I should have realized that, thanks for pointing out :)