Getting a value from C++ DLL call

Ask for help, how to use AHK_H, etc.
AlexTekeuf
Posts: 2
Joined: 07 Mar 2017, 05:14

Getting a value from C++ DLL call

07 Mar 2017, 05:33

Hi everybody! :wave:

I'm a beginner in AHK and I'm trying to call an AHK script from C++ code using the AHK DLL.

I have this AHK script file "MyScript.ahk" :

Code: Select all

#NoEnv
SetBatchLines -1
ListLines Off
#NoTrayIcon	
 
MyVar:="test3"
And this C++ code :

Code: Select all

typedef int(*AhkDll)(wchar_t *, wchar_t *, wchar_t *);
typedef int(*AhkReady)();
typedef int(*AhkAssign)(wchar_t *, wchar_t *);
typedef wchar_t*(*AhkGetVar)(wchar_t *, UINT);

//...

wchar_t* l_sRes = NULL;

// Load AHK DLL file 
HINSTANCE l_hAutoHotKeyLibrary = LoadLibrary(_T("AutoHotkey.dll"));
if (l_hAutoHotKeyLibrary != NULL)
{
	// Get function pointer 
	AhkDll l_hAutoHotKeyDLL = (AhkDll)GetProcAddress(l_hAutoHotKeyLibrary, "ahkdll");
	AhkReady l_hAutoHotKeyReady = (AhkReady)GetProcAddress(l_hAutoHotKeyLibrary, "ahkReady");
	AhkAssign l_hAutoHotKeyAssign = (AhkAssign)GetProcAddress(l_hAutoHotKeyLibrary, "ahkassign");
	AhkGetVar l_hAutoHotKeyGetVar = (AhkGetVar)GetProcAddress(l_hAutoHotKeyLibrary, "ahkgetvar");

	if ((l_hAutoHotKeyDLL != NULL) && (l_hAutoHotKeyReady != NULL) && (l_hAutoHotKeyAssign != NULL) && (l_hAutoHotKeyGetVar != NULL))
	{
		l_hAutoHotKeyAssign(_T("MyVar"), _T("test1"));
		l_sRes = l_hAutoHotKeyGetVar(_T("MyVar"), 0); // l_sRes = NULL

		l_hAutoHotKeyDLL(_T("MyScript.ahk"), _T(""), _T(""));
		l_sRes = l_hAutoHotKeyGetVar(_T("MyVar"), 0);  // l_sRes = ""

		l_hAutoHotKeyAssign(_T("MyVar"), _T("test2"));
		l_sRes = l_hAutoHotKeyGetVar(_T("MyVar"), 0); // l_sRes = "test2"

		while (l_hAutoHotKeyReady())
			Sleep(50);
		l_sRes = l_hAutoHotKeyGetVar(_T("MyVar"), 0); // l_sRes = NULL
	}
}
You can see the results I get from the GetVar calls in the code comments.
These results are very strange, Does someone can explain me how it exactly works?
The thing I want to do is to get the value from the script (here, "test3") to use it into my C++ code.

Thanks,
Alex.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Getting a value from C++ DLL call

07 Mar 2017, 13:24

1. Your ahkdll script needs to be #Persistent
2. The value returned from the functions is not always a pointer to string! (see docs)
3. When function like ahkgetvar returns a pointer to string, you will need to copy its value if you want to keep it. Autohotkey uses the same variable/memory to return strings from these functions.
AlexTekeuf
Posts: 2
Joined: 07 Mar 2017, 05:14

Re: Getting a value from C++ DLL call

08 Mar 2017, 06:40

Thanks HotKeyIt for your answer!

1. Making my script #Persistent did the trick! Now I get the "test3" result :)
2. 3. I thought it was never a pointer as I set the second parameter value to 0 (<=> no pointer)

Anyway, now that it is #Persistent, I have another problem.
When I call this macro function another time in my application, I get an exception error 0x091C3326 in AutoHotKey.dll
It seems like the preivous called script is not "ended" or something...

Thanks again,
Alex.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Getting a value from C++ DLL call

08 Mar 2017, 17:43

The pointer I meant is the returned value, ahkgetvar always returns a pointer to string (also the pointer to var is returned as string).

Can you show exactly what you do when you get exception?

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 23 guests