Marcus' Macro Blog

Mostly tips, tutorials, articles and news about Macro Scheduler & Windows Automation
November 15th, 2010 by Marcus Tettmar

The other day I posted a response on the forums which uses LibFunc to run the Win32 API function GetKeyState. This prompted someone to email me the following:

I didn’t know the Win API call to check for a key being down. The API call works but have you considered adding a native MS command called KeyDown> or maybe CheckKeyDown>

Well, it’s already possible to create native looking functions for DLL functions and therefore Win32 API functions.

We can do this by creating an Imports file in the Imports sub folder of Macro Scheduler. Let’s make an import file for some functions in User32.dll:

Step 1. If you don’t already have an Imports subfolder inside the Macro Scheduler program folder, create one: c:\program files\Macro Scheduler12\Imports

Step 2. Create a symbolic link to the appropriate DLL in this folder. Start a CMD prompt and CD to the Imports folder and for User32.dll type the following:

mklink user32.dll c:\windows\system32\user32.dll

If on a 64 bit system replace system32 with syswow64

This will create a link to the DLL in the Imports folder. Next we need to make a corresponding INI file.

Step 3. Create a file called User32.ini in the Imports folder. Inside this file create entries for each function you wish to import. E.g.:

[GetKeyState]
FunctionName=GetKeyState
Parms=1
Parm1=INT

[SendMessage]
Parms=4
Parm1=INT
Parm2=INT
Parm3=INT
Parm4=INT

[FindWindow]
Parms=2
Parm1=PCHAR
Parm2=PCHAR

Now, instead of writing:

Let>VK_UP=38
LibFunc>user32,GetKeyState,result,VK_UP

You can write:

Let>VK_UP=38
GetKeyState>VK_UP,result

So here’s a project for someone or a group of people: Create a User32.ini file for a fuller list of compatible functions which we can share on the website. Any function that accepts either integers (INT) or strings (PCHAR) and returns an integer (or nothing) can be included. And then we want another one for Kernel32.dll … etc.

No related posts.

4 Responses to “Creating Native Function Aliases for Win32 Functions”

  1. Dick Lockey says:

    Will this method work with compiled scripts?

  2. Marcus Tettmar says:

    Yes, but the Imports folder will need to be distributed with and exist next to the EXE file. [It could be created dynamically by the script - e.g. import the INI file into the script and have the script create the folder, export the INI file and make the sym link.] But of course if you know the script is going to be compiled you may prefer to just continue using LibFunc.

  3. Dick Lockey says:

    Thanks.

    By the way, great blog subject.

  4. jpuziano says:

    Heh… when I posed that additional question, I didn’t expect it to already be possible.

    Thanks for this Marcus and take care

Leave a Reply