首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lua Alien - SendMessage to Notepad.exe with WinAPI

Lua Alien - SendMessage to Notepad.exe with WinAPI
EN

Stack Overflow用户
提问于 2013-06-04 22:30:02
回答 2查看 1.4K关注 0票数 2

我整理了一堆在线资源,让我来到了这里。希望我得到的已经很接近了。不幸的是,我没有Windows编程经验。我来自Linux背景。我对Lua的alien也不熟悉,但我对Lua已经很了解了。

我想要做的是从Win32 API向正在运行的Notepad.exe窗口发送一个简单的带有sendMessage()的"Hello World“。

我使用以下命令从命令提示符获取了进程ID:

代码语言:javascript
复制
tasklist /FI "IMAGENAME eq notepad.exe" /FI "USERNAME eq user"

我收到了here发送0x000C代码的消息。

到目前为止,我得到的是:

代码语言:javascript
复制
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 documentsmsdn和一些谷歌搜索(namely this result)中拼凑而成的。

有没有人可以成为英雄,并向我解释我做错了什么,以及我应该如何去做。最重要的是,为什么!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-04 22:51:17

您误用了GetWindowThreadProcessId()和FindWindowEx()。它们的第一个参数是指向所需窗口的HWND句柄,但是您向GetWindowThreadProcessId()传递了一个进程ID,向FindWindowEx()传递了一个线程ID,这两个参数都是错误的。

没有从进程ID获取HWND的简单方法,必须使用EnumWindows()遍历当前运行的窗口,对每个窗口调用GetWindowThreadProcessId(),直到找到与已有的进程ID匹配的进程ID。

票数 1
EN

Stack Overflow用户

发布于 2014-03-05 22:56:16

我最终找到了一种从Windows API同时使用FindWindowFindWindowEX来完成此任务的更简单的方法。这样,您就可以找到Notepad的父进程和子进程的正确句柄。

代码语言:javascript
复制
-- 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 message
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16920492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档