I just got the message pumper working.
Here is the code, if you want to look at it (main.c):
// SciTE4AutoHotkey v2 LUA message pumper
// Build with: gcc -shared -o module.dll -I..\lualib\src main.c scite.la
// You've got to have a LUA distribution at ..\lualib (creates the src folder)
// for this to work.
// Also copy the scite.* files from http://luaforge.net/frs/download.php/3293/scite-debug.zip (scite.la, scite.def and scite.lib)
#include <windows.h>
#include "lauxlib.h"
#define DllExport __declspec(dllexport)
#define RET_OK 1
#define RET_FAIL 0
#define MAX_TITLE 255
#define SNDMSG_DELAY 8000
#ifndef PROCESS_VM_OPERATION
#define PROCESS_VM_OPERATION 0x0008
#endif
#ifndef PROCESS_VM_READ
#define PROCESS_VM_READ 0x0010
#endif
#ifndef PROCESS_VM_WRITE
#define PROCESS_VM_WRITE 0x0020
#endif
#ifndef MEM_COMMIT
#define MEM_COMMIT 0x1000
#endif
#ifndef PAGE_READWRITE
#define PAGE_READWRITE 4
#endif
#ifndef MEM_RELEASE
#define MEM_RELEASE 0x8000
#endif
HWND cWindow = 0, tWindow = 0;
char* cWinTitle;
// Private callback function to enumerate the windows.
BOOL CALLBACK _lib_winsearchproc(HWND hWnd, LPARAM lParam){
char wTitle[MAX_TITLE+1];
// Get window title
GetWindowText(hWnd, wTitle, MAX_TITLE);
if(!strncmp(wTitle, cWinTitle, lParam)){
// Window found.
cWindow = hWnd;
return 0; // Cancel the enumeration
}
return 1; // Continue enumerating the windows
}
// localizewin(wintitle) -- Localizes the window with the specified window title to
// further send messages to it. True = sucess, false = failure.
int lib_localizewin(lua_State* L){
// set the global variables
cWinTitle = (char*) luaL_checkstring(L, 1);
tWindow = cWindow, cWindow = 0;
// look for the window
EnumWindows((WNDENUMPROC)_lib_winsearchproc, strlen(cWinTitle));
if(!cWindow){ // no window found?
// just restore the old window and return.
cWindow = tWindow;
lua_pushboolean(L, RET_FAIL);
return 1;
}
lua_pushboolean(L, RET_OK);
return 1;
}
// pumpmsg(msg, wparam, lparam) -- Sends a message to the current window.
// Timeout of 8 seconds. It returns the value that the window returns.
int lib_pumpmsg(lua_State* L){
int iMsg = luaL_checkint(L, 1);
int wParam = luaL_checkint(L, 2);
int lParam = luaL_checkint(L, 3);
if(!IsWindow(cWindow))
return luaL_error(L, "Invalid window handle.");
int result;
if(!SendMessageTimeout(cWindow, (UINT)iMsg, (WPARAM)wParam, (LPARAM)lParam, SMTO_ABORTIFHUNG, SNDMSG_DELAY, (PDWORD_PTR) &result)){
return luaL_error(L, "Failed at SendMessage'ing the window!");
}
lua_pushnumber(L, result);
return 1;
}
// pumpmsg(msg, wparam, lparam) -- Sends a message with lparam as string to the current window.
// Timeout of 8 seconds. It returns the value that the window returns.
int lib_pumpmsgstr(lua_State* L){
int iMsg = luaL_checkint(L, 1);
int wParam = luaL_checkint(L, 2);
const char* lParam = luaL_checkstring(L, 3);
int lParamSize = strlen(lParam);
if(!IsWindow(cWindow))
return luaL_error(L, "Invalid window handle.");
// Inject the string at the process.
DWORD pID;
GetWindowThreadProcessId(cWindow, &pID);
HANDLE hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, 0, pID);
if(!hProcess)
return luaL_error(L, "Couldn't open the memory of the window!");
void* rlParam = VirtualAllocEx(hProcess, 0, lParamSize, MEM_COMMIT, PAGE_READWRITE);
if(!rlParam)
return luaL_error(L, "Couldn't allocate the memory at the window!");
if(!WriteProcessMemory(hProcess, rlParam, lParam, lParamSize, NULL))
return luaL_error(L, "Couldn't inject the string parameter at the window!");
// Send the message to the window.
int result;
if(!SendMessageTimeout(cWindow, (UINT)iMsg, (WPARAM)wParam, (LPARAM)rlParam, SMTO_ABORTIFHUNG, SNDMSG_DELAY, (PDWORD_PTR) &result)){
return luaL_error(L, "Failed at SendMessage'ing the window!");
}
// Free the memory used by the string
if(!VirtualFreeEx(hProcess, rlParam, 0, MEM_RELEASE))
return luaL_error(L, "Failed to free the memory at the window!");
if(!CloseHandle(hProcess))
return luaL_error(L, "Couldn't close the process handle!");
lua_pushnumber(L, result);
return 1;
}
int DllExport libinit(lua_State* L){
// do the following for each function you want to add to the LUA engine
lua_register(L, "localizewin", lib_localizewin);
lua_register(L, "pumpmsg", lib_pumpmsg);
lua_register(L, "pumpmsgstr", lib_pumpmsgstr);
return 0;
}
Now I'm patiently waiting for Lexikos to release his functions...

EDIT: Post #200! WOOT!!