无尽的夜空,唯有月光撒满窗前

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  14 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

//
// 将一些有用的Win32特性导出.
// 以便在Lua中使用.
//


extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#pragma comment(lib, "lua.lib")
};


#include <Windows.h>
#include <iostream>
using namespace std;


static const char* const ERROR_ARGUMENT_COUNT = "参数数目错误!";
static const char* const ERROR_ARGUMENT_TYPE = "参数类型错误!";


//
// 发生错误,报告错误.
//
void ErrorMsg(lua_State* luaEnv, const char* const pszErrorInfo)
{
lua_pushstring(luaEnv, pszErrorInfo);
lua_error(luaEnv);
}


//
// 检测函数调用参数个数是否正常.
//
void CheckParamCount(lua_State* luaEnv, int paramCount)
{
// lua_gettop获取栈中元素个数.
if (lua_gettop(luaEnv) != paramCount)
{
ErrorMsg(luaEnv, ERROR_ARGUMENT_COUNT);
}
}


//
// 显示Windows对话框.
// @param [in] pszMessage string 1
// @param [in] pszCaption string 2
//
extern "C" int ShowMsgBox(lua_State* luaEnv)
{
const char* pszMessage = 0;
const char* pszCaption = 0;

// 检测参数个数是否正确.
CheckParamCount(luaEnv, 2);

// 提取参数.
pszMessage = luaL_checkstring(luaEnv, 1);
pszCaption = luaL_checkstring(luaEnv, 2);

if (pszCaption && pszMessage)
{
::MessageBox(
NULL,
pszMessage,
pszCaption,
MB_OK | MB_ICONINFORMATION
);
}
else
{
ErrorMsg(luaEnv, ERROR_ARGUMENT_TYPE);
}

// 返回值个数为0个.
return 0;
}


//
// 导出函数列表.
//
static luaL_Reg luaLibs[] =
{
{"ShowMsgBox", ShowMsgBox},
{NULL, NULL}
};


//
// Dll入口函数,Lua调用此Dll的入口函数.
//
extern "C" __declspec(dllexport)
int luaopen_luadll(lua_State* luaEnv)
{
const char* const LIBRARY_NAME = "luadll";
luaL_register(luaEnv, LIBRARY_NAME, luaLibs);

return 1;
}


//:-)

posted on 2012-01-09 20:21 skyline 阅读(487) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理