Jump to content

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

Tutorial : C++ and Autohotkey


  • Please log in to reply
10 replies to this topic
side
  • Members
  • 168 posts
  • Last active: Nov 30 2014 03:41 PM
  • Joined: 01 Nov 2012

hello everyone.
I am going to show you ,how to configure your computer to make it able run AHK scripts via your C++ IDE
(beware of compiler)
First of all let me inform you that the main idea is HotKeyIt's and i/we will use his library(At the end of tutorial i will post

credits)

 

Before we start!

1)i am not here to teach you c++...for any lack of knowledge search google or something

2)i placed the images in URLS due to .png forum bug

 

Lets begin!

 

1)Installation of AHK_H which is needed 

 

Download the AHK_H from 

https://ahknet.autoh.../AutoHotkey.zip

 

it will be a zip file.Extract it to an empty folder.

 

-Damn Side there is no installer!

 

yes!AHK_H have to be installed ,manually.

Lets begin.

Firstly we enter to folder called ''multi-threaded dll MD" (other folders are used in AHK IDE)

there are 3 folders

 

http://img41.imagesh...1/2888/zdm5.png

 

-Which one i choose Side?looks chinese to me 

 

Win32w Means x32 UNICODE system

Win32a Means x32 ANSI 

x64w means x64 

if you dont know what to choose you can use that script though-->

msgbox,AHK version: %A_AhkVersion%
msgbox,Is Unicode?=%A_IsUnicode% (1=unicode 16bit  | empty string=ansi 8bit)
msgbox,OS Version:%A_OSVersion%`n
msgbox,Is 64 bit OS?=%A_Is64bitOS% (1=true ,its x64 | 0=false , its x32(x86))

exitapp

 

all of them are for ahk_L

if u want for basic AHK use Win32a 
 

so ,open a folder and check those 5 files.3 dlls ,1 exe and 1 bin

Dont touch them!

Firstly go to %A_ProgramFiles%\Autohotkey and take a backup copy file of the .exe that is there

Secondly go to %A_ProgramFiles%\Autohotkey\Compiler and take a backup copy file of the .bin that is there

 

Now...replace those 2 files with those in .zip file...

The 3 Dlls are going to be pasted in %A_WinDir%\System32\ to make them available everytime we call a function

(otherwise we will have to enter the full directory of the dll)

If u got an error message about msvcr100.dll just press ok and move on

 

Finally :

-For windows XP got to start/run and type regsvr32 "Path of AutoHotkey.dll"

example: regsvr32 "C:\WINDOWS\System32\AutoHotkey.dll"

-For windows vista/7 just type regsvr32 "C:\WINDOWS\System32\AutoHotkey.dll" in the search bar of start menu

 

 

That was the installation!

 

2)Starting a C++ Code

 

In this tutorial i will use Code::Blocks...it doesnt really matter but i recommend that one
Lets start a new console application and be ready to type a code

#include <windows.h>
int main(void)
{
   
}

Firstly we aim to a target.our target will be,to create an AHK msgbox inside c++ IDE

 

So now,we move to AHK_H documention to see what we need

https://ahknet.autoh...s_List-txt.html

 

We surely need this--> 

Execute a script from a string that contains ahk script. ahkExec

 

hm.....

we surely have to handle(Focus on) /Start a new thread Autohotkey Dll somehow

Start new thread from ahk file.

ahkdll

 

Its enough.!

 

....lets start coding !

Firstly lets load our .dll

#include <windows.h> //we must include it to use functions such as LoadLibrary
int main(void)
{
HINSTANCE handle = LoadLibrary("C:\\Windows\\System32\\AutoHotkey.dll");
}

Now lets see how HotKeyIt has declare the functions

http://img41.imagesh...1/2579/2hka.png

 

So its a function with 3 variables ...it will look like ahkdll(typeof var1 var1,typeofvar2 var2,typeofvar3 var3)
 

As we see in example that exists down to the page,the ahkdll has got 3 variables strings!
so in C++ we will use pointers ( because we want to have return.not only execution)
so we have to make some declarations

#include <windows.h> //we must include it to use functions such as LoadLibrary
int main(void)
{
typedef UINT (*pahkdll)(LPTSTR script,LPTSTR p1,LPTSTR p2);//pahkdll is the pointer of ahkdll and so on
HINSTANCE handle = LoadLibrary("C:\\Windows\\System32\\AutoHotkey.dll");
}

lets fill the pointer with something!!

#include <windows.h> //we must include it to use functions such as LoadLibrary
int main(void)
{
typedef UINT (*pahkdll)(LPTSTR script,LPTSTR p1,LPTSTR p2);//pahkdll is the pointer of ahkdll and so on
HINSTANCE handle = LoadLibrary("C:\\Windows\\System32\\AutoHotkey.dll");
pahkdll ahkdll = (pahkdll)GetProcAddress(handle, "ahkdll");//use MSDN to see how GetProcAddress works
}

We have to do the same for the other function too,ahkexec

http://img713.images...3/1372/3q2x.png

 

Oh nice..only 1 variable!.it will look lie ahkExec(typeofVar var);

 

 

So the code will be

#include <windows.h> //we must include it to use functions such as LoadLibrary
int main(void)
{
typedef UINT (*pahkdll)(LPTSTR script,LPTSTR p1,LPTSTR p2);//pahkdll is the pointer of ahkdll and so on
typedef BOOL (*pahkExec)(LPTSTR script);

HINSTANCE handle = LoadLibrary("C:\\Windows\\System32\\AutoHotkey.dll");

pahkExec ahkExec = (pahkExec)GetProcAddress(handle, "ahkExec");
pahkdll ahkdll = (pahkdll)GetProcAddress(handle, "ahkdll");//use MSDN to see how GetProcAddress works
}

So i think its ok!lets execute the code ''Msgbox , Hello!"
 

#include <windows.h> //we must include it to use functions such as LoadLibrary
int main(void)
{
typedef UINT (*pahkdll)(LPTSTR script,LPTSTR p1,LPTSTR p2);//pahkdll is the pointer of ahkdll and so on
typedef BOOL (*pahkExec)(LPTSTR script);
HINSTANCE handle = LoadLibrary("C:\\Windows\\System32\\AutoHotkey.dll");
pahkExec ahkExec = (pahkExec)GetProcAddress(handle, "ahkExec");
pahkdll ahkdll = (pahkdll)GetProcAddress(handle, "ahkdll");//use MSDN to see how GetProcAddress works
ahkExec("Msgbox Hello ");
    return 0 ;
}
 

It runs but got no output...why that ?
there are 2 reasons...firstly we dont know if the script is loaded and running!

lets see the ahk_H documention

 

Check if a script is running. ahkReady

 

we gonna need this!

 

so...we will again declare a new function/pointer.lets see how ahkready works

http://i.imgur.com/6i7w1Vd.png

 

No variables.just return...so if ahkready==1 then the thread is running

so...

#include <windows.h> //we must include it to use functions such as LoadLibrary
int main(void)
{
typedef UINT (*pahkdll)(LPTSTR script,LPTSTR p1,LPTSTR p2);//pahkdll is the pointer of ahkdll and so on
typedef BOOL (*pahkExec)(LPTSTR script);
typedef BOOL (*pahkReady)(void);

HINSTANCE handle = LoadLibrary("C:\\Windows\\System32\\AutoHotkey.dll");

pahkExec ahkExec = (pahkExec)GetProcAddress(handle, "ahkExec");
pahkReady ahkReady = (pahkReady)GetProcAddress(handle, "ahkReady");
pahkdll ahkdll = (pahkdll)GetProcAddress(handle, "ahkdll");//use MSDN to see how GetProcAddress works
while (!ahkReady()) //w8 10 milliseconds until thread is started
      Sleep(10);
ahkExec("Msgbox Hello ");    
 return 0 ;
 }

Runs but still no msgbox...why?

As we see in the example of ahkdll we have to ''empty'' the function firstly 

HotKeyIt says--> DllCall(AhkDllPath "\ahkdll","Str","MyScript.ahk","Str","","Str","","Cdecl UPTR")

in c++ that can be just ahkdll("","","");

 

so our code will be something like that -->

#include <windows.h>
int main(void)
{
    //Typedef the functions
   typedef BOOL (*pahkReady)(void);
   typedef BOOL (*pahkExec)(LPTSTR script);
   typedef UINT (*pahkdll)(LPTSTR script,LPTSTR p1,LPTSTR p2);


    // Load
   HINSTANCE handle = LoadLibrary("C:\\Windows\\System32\\AutoHotkey.dll");


    // pointers
   pahkdll ahkdll = (pahkdll)GetProcAddress(handle, "ahkdll");
   pahkReady ahkReady = (pahkReady)GetProcAddress(handle, "ahkReady");
   pahkExec ahkExec = (pahkExec)GetProcAddress(handle, "ahkExec");


    //free memory
   ahkdll("","","");


   // debug
   while (!ahkReady())
      Sleep(10);


ahkExec("Msgbox Hello ");
    return 0 ;
}

Running and we got a msgbox as we wanted so!!!
http://i.imgur.com/S9fQ6Fs.png

Here we are
We did it ...

 

thanks in advance for feedbacks


 

 

credits:
thanks to

HotKeyIt - the main man! his library/his documention/his everything!
alset - Testing many .exe on his computer

Forum IRC - for its patience



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

Thanks for putting this together wink.png

Firstly we enter to folder called ''multi-threaded dll MD" (other folders are used in AHK IDE)

 

Multi-treaded dll is only required if you want to read / write / delete variables and objects (especially memory) from other thread, e.g. AutoHotkey.exe or another AutoHotkey.dll thread. This version uses same heap as the executable or other AutoHotkey.dll threads, so they can manipulate each other memory.

 

 

 

There is not 16 or 8 bit AutoHotkey, Win32 means 32-bit x64 64-bit, Unicode and ANSI is the text encoding.

Also A_PtrSize will tell if AutoHotkey is 32 or 64 bit as you can run 32-bit on 64-bit OS.

msgbox,Is Unicode?=%A_IsUnicode% (1=unicode  | empty string=ansi)
msgbox,OS Version:%A_OSVersion%`n
msgbox,Is 64 bit OS?=%A_Is64bitOS% (1=true ,its x64 | 0=false , its x32(x86))
msgbox,% "Is 64 bit AutoHotkey?= " (A_PtrSize=8 ? 1 : 0) " (1=true ,its x64 | 0=false , its x32(x86))"

we surely have to handle(Focus on) /Start a new thread Autohotkey Dll somehow

Start new thread from ahk file.

 

We can start a new empty AutoHotkey thread using ahkdll or ahktextdll with empty parameters.

  • ahkdll usually is used to start a thread from a script saved in a file.
  • ahktextdll is used to start a new thread from a script saved in memory/variable.

 

 

Also you will not need ahkReady in this example, ahkdll and ahktextdll wait for script to be ready before continuing.



side
  • Members
  • 168 posts
  • Last active: Nov 30 2014 03:41 PM
  • Joined: 01 Nov 2012

thanks for ur reply hotkeyit :) 
those things looks a bit strange to me :)



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

those things looks a bit strange to me :)

Which ones?



side
  • Members
  • 168 posts
  • Last active: Nov 30 2014 03:41 PM
  • Joined: 01 Nov 2012

We can start a new empty AutoHotkey thread using ahkdll or ahktextdll with empty parameters.

  • ahkdll usually is used to start a thread from a script saved in a file.
  • ahktextdll is used to start a new thread from a script saved in memory/variable.

 

 

Also you will not need ahkReady in this example, ahkdll and ahktextdll wait for script to be ready before continuing.


AND


Multi-treaded dll is only required if you want to read / write / delete variables and objects (especially memory) from other thread, e.g. AutoHotkey.exe or another AutoHotkey.dll thread. This version uses same heap as the executable or other AutoHotkey.dll threads, so they can manipulate each other memory.





Cant get what u mean my friend



Samsam
  • Members
  • 1 posts
  • Last active: Dec 12 2013 02:58 AM
  • Joined: 11 Dec 2013

So if I'm understanding this correctly, if you wanted to integrate AHK calls (I.E. use some AHK functions in my C++ code like msgbox)

You would do it this way, instead of all the superfluous stuff you used. Note that the L's are required if you are on a x64 or unicode system.

#include <windows.h>

using namespace std;

int main()
{
	typedef UINT (*pahktextdll)(LPTSTR script,LPTSTR opt, LPTSTR param); //typedef for a pointer to ahktextdll, used for calling an AHK function 

	//Load AHK DLL
	HINSTANCE handle = LoadLibrary(L"C:\\Windows\\System32\\AutoHotkey.dll");
	if(handle==NULL)
	{
		DWORD err=GetLastError();
		cerr << "Error: " <<err;	//Error opening the dll.
	}
	else
	{
		//Get pointers to specific part we'll use
		pahktextdll ahktextdll = (pahktextdll)GetProcAddress(handle, "ahktextdll");
		LPTSTR empty = L"";
		ahktextdll(empty,empty,empty);
		ahktextdll(L"Msgbox Hello",empty,empty);
	}
	return 0;
}


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

This line is not required, it starts an empty thread that is exited straight away by the next call to ahktextdll.

ahktextdll(empty,empty,empty);


MJ Khor
  • Members
  • 17 posts
  • Last active: Aug 25 2015 02:42 AM
  • Joined: 05 Feb 2013

Currently I need to use AHK to develop one simple tool which is after you run the script: 
-you can select a pdf file
-press the F1 hotkey
-the selected pdf file will be open maximize
-then prompt out a box that always on top
-the box will have 3 columns(ID, Number & Name) & 2 buttons(Submit & Cancel)
-User able to key-in number & text in all columns
-After click on Submit button, what user input will be rename as the pdf file name with underscore seperator (e.g.: id_number_name.pdf). Then open the next pdf file and continue.

I've studied the tutorials but I'm still failed to link/combine all the function... I'm really weak in programming but my boss was forcing me to create this for job enhancement. I've no choice and I seek for your help. Appreciate your kindness with true heart.



Edd
  • Members
  • 212 posts
  • Last active: Jan 06 2016 07:36 AM
  • Joined: 24 Dec 2011

Currently I need to use AHK to develop one simple tool which is after you run the script: 
-you can select a pdf file
-press the F1 hotkey
-the selected pdf file will be open maximize
-then prompt out a box that always on top
-the box will have 3 columns(ID, Number & Name) & 2 buttons(Submit & Cancel)
-User able to key-in number & text in all columns
-After click on Submit button, what user input will be rename as the pdf file name with underscore seperator (e.g.: id_number_name.pdf). Then open the next pdf file and continue.

I've studied the tutorials but I'm still failed to link/combine all the function... I'm really weak in programming but my boss was forcing me to create this for job enhancement. I've no choice and I seek for your help. Appreciate your kindness with true heart.

 

You need to do that on AHK?

 

"you can select a pdf file" you mean by opening a FileSelectFile dialog?



MJ Khor
  • Members
  • 17 posts
  • Last active: Aug 25 2015 02:42 AM
  • Joined: 05 Feb 2013

You need to do that on AHK?

 

"you can select a pdf file" you mean by opening a FileSelectFile dialog?

Yes, I need to do that on Auto Hot Key.

 

Assume that the script is running and need to hit F1 key to activate. Select pdf file from a folder, and hit F1 key, then it will open up the selected pdf file(maybe by adobe reader/arobat). 



Edd
  • Members
  • 212 posts
  • Last active: Jan 06 2016 07:36 AM
  • Joined: 24 Dec 2011

But I don't understand if you want to open a FileSelectFile dialog and select the file from that window or open a windows folder and select a pdf file with the cursor, then hit F1 and open the pdf