lua运行外部程序_Lua通过COM调用外部程序excel及调用windows api

[在我接触JNA之前一直以为调用本地链接库函数的时候一定要借助JNI或者SWT来实现,一日中无意看到JNA,发现它也可以实现这类调用,于是下回来按网站上的例子试试了 还不错,

为了方便起见,最好安装lua for windows,里面已经包含了很多有用的第三方模块。

require(’luacom’) — luacom

ie = luacom.CreateObject(”InternetExplorer.Application”)

ie:Navigate2(”http://sunxiunan.com”)

ie.Visible = true

使用lua调用excel,然后往cell里面填一些数据。

require(’luacom’) — luacom

– Excelの起動

excel = luacom.CreateObject(”Excel.Application”)

excel.Visible = true — 可視状態に

– ワークブックを追加

local book  = excel.Workbooks:Add()

local sheet = book.Worksheets(1)

– 適当な値を100個書き込む

for row=1,100 do

sheet.Cells(row, 1).Value2 = math.floor(math.random() * 20)

end

稍微复杂一些的代码

require “luacom”

excel = luacom.CreateObject(”Excel.Application”)

local book  = excel.Workbooks:Add()

local sheet = book.Worksheets(1)

excel.Visible = true

– 適当な値を書き込む

for row=1,30 do

for col=1,30 do

sheet.Cells(row, col).Value2 = math.floor(math.random() * 100)

end

end

– 値を調べて50以上のものを黄色でマークする

local range = sheet:Range(”A1″)

for row=1,30 do

for col=1,30 do

local v = sheet.Cells(row, col).Value2

if v > 50 then

local cell = range:Offset(row-1, col-1)

cell:Select()

excel.Selection.Interior.Color = 65535

end

end

end

excel.DisplayAlerts = false — 終了確認を出さないようにする

excel:Quit()

excel = nil

如果想给excel加个图表该怎么做?[ 云风的 BLOG 思绪来的快去的也快,偶尔会在这里停留

require “luacom”

excel = luacom.CreateObject(”Excel.Application”)

local book  = excel.Workbooks:Add()

local sheet = book.Worksheets(1)

excel.Visible = true

for row=1,30 do

sheet.Cells(row, 1).Value2 = math.floor(math.random() * 100)

end

local chart = excel.Charts:Add()

chart.ChartType = 4 — xlLine

local range = sheet:Range(”A1:A30″)

chart:SetSourceData(range)

如果想调用windows api,可以用下面的代码

require “alien”

MessageBox = alien.User32.MessageBoxA

MessageBox:types{ret = ‘long’, abi = ’stdcall’, ‘long’, ’string’,

’string’, ‘long’ }

MessageBox(0, “title for test”, “LUA call windows api”, 0)

如何实现回调函数呢?下面的例子展示了回调。

require ‘alien’

–声明了两个函数EnumWindows和GetClassName

EnumWindows = alien.user32.EnumWindows

EnumWindows:types {”callback”, “pointer”, abi=”stdcall”}

GetClassName = alien.user32.GetClassNameA

GetClassName:types {”long”, “pointer”, “int”, abi=”stdcall” }

local buf = alien.buffer(512)

– 会被EnumWindows反复调用,传入windows的handle

local function enum_func(hwnd, p)

GetClassName(hwnd, buf, 511)

print (hwnd..”:”..tostring(buf))

return 1

end

local callback_func = alien.callback(

enum_func,

{”int”, “pointer”, abi=”stdcall”})

EnumWindows(callback_func, nil)

其中函数原型是

BOOL EnumWindows(WNDENUMPROC lpEnumFunc

, LPARAM lParam

);

int GetClassName(HWND hWnd

, LPTSTR lpClassName

, int nMaxCount

);

其中EnumWindows第一个参数的原型为,这个函数是客户调用时候传入,EnumWindows用它返回

BOOL CALLBACK EnumWindowsProc(HWND hwnd

, LPARAM lParam

);

其他复杂的使用方法可以参考alien的文档。

[Lua是一个轻量级脚本语言,在C++中可以方便的调用、运行Lua脚本。下面的示例参考http://gamedevgeek.com/2006/05/04/lua-tutorials/,在运行示例之前,需要先配置Visual C

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例程序,其中Lua调用了C函数,C函数又回调了Lua函数。 C代码: ```c #include <stdio.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" static int c_hello(lua_State *L) { printf("Hello from C!\n"); return 0; } static int c_call_lua(lua_State *L) { const char *str = luaL_checkstring(L, 1); printf("C calling Lua: %s\n", str); lua_pushinteger(L, 12345); lua_pushstring(L, "Hello from Lua!"); return 2; } static int c_call_lua_callback(lua_State *L) { const char *str = luaL_checkstring(L, 1); lua_Integer num = luaL_checkinteger(L, 2); luaL_checktype(L, 3, LUA_TFUNCTION); lua_pushvalue(L, 3); // 把回调函数压入栈中 lua_pushstring(L, str); lua_pushinteger(L, num); int result = lua_pcall(L, 2, 1, 0); // 调用回调函数 if (result != 0) { printf("Error calling Lua callback: %s\n", lua_tostring(L, -1)); lua_pop(L, 1); return 0; } const char *ret_str = lua_tostring(L, -1); printf("C received from Lua callback: %s\n", ret_str); return 0; } int main() { lua_State *L = luaL_newstate(); luaL_openlibs(L); lua_pushcfunction(L, c_hello); lua_setglobal(L, "c_hello"); lua_pushcfunction(L, c_call_lua); lua_setglobal(L, "c_call_lua"); lua_pushcfunction(L, c_call_lua_callback); lua_setglobal(L, "c_call_lua_callback"); luaL_dostring(L, "function lua_hello() print('Hello from Lua!') end"); luaL_dostring(L, "c_hello()"); luaL_dostring(L, "print(c_call_lua('Hello from C!'))"); luaL_dostring(L, "c_call_lua_callback('Hello from C callback!', 123, function(str, num) print('Lua callback received:', str, num) return 'Hello from Lua callback!' end)"); lua_close(L); return 0; } ``` Lua代码: ```lua local function lua_callback(str, num) print("Lua received from C callback:", str, num) return "Hello from Lua callback!" end c_hello() local num, str = c_call_lua("Hello from Lua!") print("Lua received from C:", num, str) c_call_lua_callback("Hello from Lua callback!", 123, lua_callback) ``` 在运行程序时,会输出以下内容: ``` Hello from C! 12345 Hello from Lua! Lua callback received: Hello from C callback! 123 C received from Lua callback: Hello from Lua callback! Lua received from C callback: Hello from Lua callback! 123 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值