我整理了一堆在线资源,让我来到了这里。希望我得到的已经很接近了。不幸的是,我没有Windows编程经验。我来自Linux背景。我对Lua的alien也不熟悉,但我对Lua已经很了解了。
我想要做的是从Win32 API向正在运行的Notepad.exe窗口发送一个简单的带有sendMessage()的"Hello World“。
我使用以下命令从命令提示符获取了进程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)中拼凑而成的。
有没有人可以成为英雄,并向我解释我做错了什么,以及我应该如何去做。最重要的是,为什么!
发布于 2013-06-04 22:51:17
您误用了GetWindowThreadProcessId()和FindWindowEx()。它们的第一个参数是指向所需窗口的HWND句柄,但是您向GetWindowThreadProcessId()传递了一个进程ID,向FindWindowEx()传递了一个线程ID,这两个参数都是错误的。
没有从进程ID获取HWND的简单方法,必须使用EnumWindows()遍历当前运行的窗口,对每个窗口调用GetWindowThreadProcessId(),直到找到与已有的进程ID匹配的进程ID。
发布于 2014-03-05 22:56:16
我最终找到了一种从Windows API同时使用FindWindow和FindWindowEX来完成此任务的更简单的方法。这样,您就可以找到Notepad的父进程和子进程的正确句柄。
-- Require luarocks and alien which are necessray for calling Windows functions
require "luarocks.require"
require "alien"
local SendMessage= alien.User32.SendMessageA
SendMessage:types{ret ='long', abi = 'stdcall','long','long','string','string'}
local FindWindowEx = alien.User32.FindWindowExA
FindWindowEx:types{ret = 'long', abi = 'stdcall', 'long', 'long', 'string', 'string'}
local FindWindow = alien.User32.FindWindowA
FindWindow:types{ret = 'long', abi = 'stdcall', 'string', 'string'}
local notepadHandle = FindWindow("Notepad", NULL )
local childHandle = FindWindowEx(notepadHandle, "0", "Edit", nil)
local x = SendMessage(childHandle, "0x000C", "0", "Hello World!") -- Actually send the messagehttps://stackoverflow.com/questions/16920492
复制相似问题