I have compiled a bunch of online resources that got me to here. Hopefully what I have is close. Unfortunately I have no Windows Programming experience. I come from a Linux background. I am also new to alien for Lua, but I know Lua well enough.
What I want to do is send a simple "Hello World" with sendMessage()
from the Win32 API
to a running Notepad.exe
window.
I got the process ID from the command prompt with the following command:
tasklist /FI "IMAGENAME eq notepad.exe" /FI "USERNAME eq user"
And I got the message to send code of 0x000C from here.
So far this is what I have:
require "luarocks.require"
require "alien"
myTestMessage = 0x000C -- Notepad "Set text" id
notepadPID = 2316 -- Notepad Process ID
-- Prototype the SendMessage function from User32.dll
local SendMessage= alien.User32.SendMessageA
SendMessage:types{ret ='long', abi = 'stdcall','long','long','string','string'}
-- Prototype the FindWindowExA function from User32.dll
local FindWindowEx = alien.User32.FindWindowExA
FindWindowEx:types{ret = 'long', abi = 'stdcall', 'long', 'long', 'string', 'string'}
-- Prototype the GetWindowThreadProcessID function from User32.dll
local GetWindowThreadProcessId = alien.User32.GetWindowThreadProcessId
GetWindowThreadProcessId:types{ret = 'long', abi = 'stdcall', 'long', 'pointer'}
local buffer = alien.buffer(4) -- This creates a 4-byte buffer
local threadID = GetWindowThreadProcessId(notepadPID, buffer) -- this fills threadID and our 4-byte buffer
local longFromBuffer = buffer:get(1, 'long') -- this tells that I want x bytes forming a 'long' value and starting at the first byte of the
-- 'buffer' to be in 'longFromBuffer' variable and let its type be 'long'
local handle = FindWindowEx(threadID, "0", "Edit", nil); -- Get the handle to send the message to
local x = SendMessage(handle, myTestMessage, "0", "Hello World!") -- Actually send the message
A lot of this code was pieced together from the Lua alien documents, msdn, and some Google searches (namely this result).
Is there anyone out there who could be a hero and explain to me what i'm doing wrong, and how I should go about this. And, most importantly, why!