我編譯了一堆在線資源,讓我到這裏。希望我擁有的是接近的。不幸的是,我沒有Windows編程經驗。我來自Linux背景。我對Lua也是alien的新手,但我對Lua的瞭解足夠了。Lua Alien - SendMessage與WinAPI的Notepad.exe
我想要做的是發送一個簡單的「Hello World」sendMessage()
從Win32 API
到正在運行的Notepad.exe
窗口。
我從命令提示的進程ID與以下命令:
tasklist /FI "IMAGENAME eq notepad.exe" /FI "USERNAME eq user"
而且我得到從here發送0x000C的代碼的消息。
到目前爲止,這是我:
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
很多這樣的代碼是從Lua alien documents,msdn拼湊在一起,而一些谷歌搜索(namely this result)。
有沒有人可以成爲英雄,向我解釋我做錯了什麼,以及我應該如何去做這件事。而且,最重要的是,爲什麼!