LUA调用Windows下的DLL有两个方法。Alien与LUA LIB库编译。

Alien方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
require 'alien'
local user = alien.load 'user32.dll'
 
-- these are the API calls needed. Note two NB things:
-- (1) functions dealing with text ending with 'A' are for ASCII
-- (2) need to specify abi to get proper __stdcall
user.FindWindowA:types {"string","string",abi="stdcall"}
user.GetWindowTextA:types {"int","string","int",abi="stdcall"}
user.GetClassNameA:types{"int","string","int",abi="stdcall"}
 
find = user.FindWindowA
gettext = user.GetWindowTextA
getclass = user.GetClassNameA
 
local mydll = alien.load "MyLuaDLL.dll"
mydll.callme:types{"int","string","int",abi="stdcall"}
callme = mydll.callme
 
t1 = alien.buffer(50)
callme(2, t1, 50);
print("callme=".. t1:tostring());

LUA LIB库编译方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
 
//不需要参数
extern "C" int lua_Nokia_Open(lua_State* L)
{
double dRet = Nokia_Open();
lua_pushnumber(L, dRet);
return 1;
}
 
struct luaL_reg lrLibs[] =
{
{ "lua_Nokia_Open", lua_Nokia_Open },
{ NULL, NULL }  /* sentinel */
};
 
 
 
extern "C" __declspec(dllexport) int  luaopen_luaNokiaSMS(lua_State* L)
{
 
luaL_register( L, "luaNokiaSMS", lrLibs );
return 0;
}

注意哦,你的DLL名必须是luaNokiaSMS.dll

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2012 客家缘分小筑 Suffusion theme by Sayontan Sinha