如何使用lua和ffi将操作系统api复制到剪贴板



如何在lua中复制值或字符串?我只找到了常规微软操作系统api的替代品。对于我的使用,我没有访问osapi的权限,而是可以访问ffi和steam全景。

如果可能的话,我还知道如何从当前剪贴板中获取字符串。

我可以访问

  1. LuaJIT 2.0.5(https://github.com/LuaJIT/LuaJIT)

  2. FFI(https://luajit.org/ext_ffi.html)

  3. 钻头(https://bitop.luajit.org/api.html)

    通过永不言败(https://docs.neverlose.cc)

可能不是最漂亮的解决方案,但假设您可以运行powershell:

local pipe = io.popen("powershell get-clipboard", "r")
local clipboard = pipe:read("*a")
print("Clipboard: " .. clipboard)
pipe:close()

在源引擎(在本例中为-csgo(破解Lua-api的情况下,您可以创建vgui2.dll的接口,并对其函数进行ffi.cast。这里有三个代码片段,用于永不丢失-初始化,获取和设置剪贴板。

如果您愿意,您仍然可以将此代码移植到其他csgo欺骗程序,方法是将Utils.CreateInterface替换为与欺骗程序的api文档中相同的函数。例如,在游戏意义上,它将是client.create_interface

-- initialisation (ffi, creating functions and interface)
local ffi = require("ffi")
ffi.cdef[[
typedef int(__thiscall* get_clipboard_text_count)(void*);
typedef void(__thiscall* get_clipboard_text)(void*, int, const char*, int);
typedef void(__thiscall* set_clipboard_text)(void*, const char*, int);
]]
local VGUI_Systemdll =  Utils.CreateInterface("vgui2.dll", "VGUI_System010")
local VGUI_System = ffi.cast(ffi.typeof('void***'), VGUI_Systemdll)
local get_clipboard_text_count = ffi.cast( "get_clipboard_text_count", VGUI_System[ 0 ][ 7 ] )
local get_clipboard_text = ffi.cast( "get_clipboard_text", VGUI_System[ 0 ][ 11 ] )
local set_clipboard_text = ffi.cast( "set_clipboard_text", VGUI_System[ 0 ][ 9 ] )
-- getting clipboard content
local clipboard_text_length = get_clipboard_text_count( VGUI_System )
local clipboardstring = ""
if clipboard_text_length > 0 then -- game will probably crash without that check
local buffer = ffi.new("char[?]", clipboard_text_length)
local size = clipboard_text_length * ffi.sizeof("char[?]", clipboard_text_length)
get_clipboard_text( VGUI_System, 0, buffer, size )
clipboardstring = ffi.string( buffer, clipboard_text_length-1 )
end
-- clipboardstring variable is what's in the clipboard
-- writing to clipboard
local some_cool_string = "i love ryuko very much"
set_clipboard_text(VGUI_System, some_cool_string, some_cool_string:len())

最新更新