winapi - Lua Alien - 使用 WinAPI 将消息发送到 Notepad.exe

标签 winapi lua ipc

我整理了一堆在线资源,让我来到这里。希望我所拥有的很接近。不幸的是我没有 Windows 编程经验。我有 Linux 背景。我也是alien的新手对于Lua,但我足够了解Lua。

我想要做的是使用 sendMessage()Win32 API 发送一个简单的“Hello World”到正在运行的 Notepad.exe 窗口。

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

tasklist /FI "IMAGENAME eq notepad.exe" /FI "USERNAME eq user"

notepad PID

我收到了来自 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 ,以及一些 Google 搜索 ( namely this result )。

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

最佳答案

我最终找到了一种更简单的方法来使用FindWindow来做到这一点和 FindWindowEX来自 Windows API。这样您就可以找到记事本父进程和子进程的正确句柄。

-- 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

关于winapi - Lua Alien - 使用 WinAPI 将消息发送到 Notepad.exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16920492/

相关文章:

c++ - 调用 win32 API 并回调类函数

lua 不处理 string.find 或 string.match 中的 anchor

module - 在 LUA 中需要模块

linux - 如何在 Linux 中使用 POSIX API 通过消息队列发送整数?

c - gtk+ win32 线程

c++ - 用于 Trackbar thumb 的 win32 C++ 自定义颜色

lua - 使用Lua调用C(Linux中)

c++ - 我如何与 C++ 中的其他应用程序交互?

c - Linux 消息队列的接收端应该放什么键?

C 如何使用异步readFile