无忧启动论坛

 找回密码
 注册
搜索
系统gho:最纯净好用系统下载站投放广告、加入VIP会员,请联系 微信:wuyouceo
查看: 4200|回复: 2
打印 上一主题 下一主题

[讨论] 强大的LuaJIT FFI

[复制链接]
跳转到指定楼层
1#
发表于 2014-3-12 15:10:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
LuaJIT 比 官方 Lua 解释器运行更快,功能更强,只是有些库可能不兼容。
闲来无事,试用 LuaJIT FFI 测试 Windows 系统原生代码如下:
  1. local ffi = require("ffi")
  2. ffi.cdef[[
  3. int MessageBoxA(void *h, const char *txt, const char *title, int type);
  4. int GetSystemDirectoryA(char *buf, int len);
  5. void * malloc(int n);
  6. void free(void *p);
  7. int ShellExecuteA(void *w, const char *Operation, const char *File, const char *Param, const char *Dir, int nShowCmd);
  8. int GetWindowThreadProcessId(void *hWnd, int *lpdwProcessId);
  9. typedef struct {int hProcess, hThread, dwProcessId, dwThreadId;} PROCESS_INFORMATION;
  10. typedef struct {int cb; void *lpReserved, *lpDesktop, *lpTitle; int dwX, dwY, dwXSize, dwYSize, dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags; uint16_t wShowWindow; uint16_t cbReserved2; uint8_t *lpReserved2; int hStdInput, hStdOutput, hStdError; } STARTUPINFO, *LPSTARTUPINFO;
  11. bool CreateProcessA(const char *lpApplicationName, const char *lpCommandLine, void *lpProcessAttributes, void *lpThreadAttributes, bool bInheritHandles, int dwCreationFlags, void *lpEnvironment, const char *lpCurrentDirectory, STARTUPINFO *lpStartupInfo, PROCESS_INFORMATION *lpProcessInformation);
  12. int OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  13. int WaitForSingleObject(int hdl, int ms);
  14. ]]

  15. local shlapi = ffi.load("shell32")
  16. local p = ffi.gc(ffi.C.malloc(255), ffi.C.free)
  17. local l = ffi.C.GetSystemDirectoryA(p, 255)
  18. ffi.C.MessageBoxA(nil,p,string.format("%d",l),16)
  19. print (string.format("%d", shlapi.ShellExecuteA(nil, nil, "notepad", nil, nil, 1)))
  20. local si = ffi.new("STARTUPINFO")
  21. local pi = ffi.new("PROCESS_INFORMATION")
  22. si.cb = ffi.sizeof(si)
  23. print (si.cb)
  24. if not ffi.C.CreateProcessA(nil, "notepad d:\\menu.lst", nil, nil, false, 0, nil, nil, si, pi) then
  25.     print ("error")
  26.     exit (0)
  27. end
  28. -- INFINITE        0xFFFFFFFF
  29. -- WAIT_OBJECT_0 0
  30. -- WAIT_ABANDONED_0 128
  31. -- WAIT_TIMEOUT 258
  32. -- SYNCHRONIZE        0x100000L
  33. local hsync = ffi.C.OpenProcess(0x100000, false, pi.dwProcessId)
  34. print ("wait for PID=" .. pi.dwProcessId .. " end: " .. ffi.C.WaitForSingleObject(hsync, -1))
复制代码
2#
发表于 2014-3-12 16:28:59 | 只看该作者
虽然看不懂还是要支持楼主的。

点评

谢谢。我突然想到,Lua的中文翻译是不是“撸啊”?  详情 回复 发表于 2014-3-12 16:35
回复

使用道具 举报

3#
 楼主| 发表于 2014-3-12 16:35:27 | 只看该作者
renchmin 发表于 2014-3-12 16:28
虽然看不懂还是要支持楼主的。

谢谢。我突然想到,Lua的中文翻译是不是“撸啊”?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1 )

闽公网安备 35020302032614号

GMT+8, 2025-8-5 04:13

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表