Lua无法加载VC++ dll。

我使用Visual Studio C/C++工具链将我的_litertools_项目编译为.dll,并正确导出lua_openlitertools函数:

PS> dumpbin /exports .\bin\lib\litertools.dll
Microsoft (R) COFF/PE Dumper Version 14.00.22013.1

Dump of file .\bin\lib\litertools.dll

  Section contains the following exports for litertools.dll

    00000000 characteristics
    5439D35E time date stamp Sun Oct 12 12:03:26 2014
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names
    ordinal hint RVA      name

          1    0 000227AD luaopen_litertools = @ILT+1960(_luaopen_litertools)
  Summary

        1000 .data
        2000 .idata
        6000 .rdata
        2000 .reloc
        1000 .rsrc
       44000 .text
       21000 .textbss

当我设置配置package.cpath并通过以下方式需要_litertools_ dll的时候:

CMD> lua -e "package.cpath=\".\\bin\\lib\\?.dll\"; require(\"litertools\")"

我收到以下错误

lua: error loading module 'litertools' from file '.\bin\lib\litertools.dll':
        The specified module could not be found.

stack traceback:
        [C]: in ?
        [C]: in function 'require'
        (command line):1: in main chunk
        [C]: in ?

仔细查看后,我通过require (loadlib.c)调试了调用,并到达了这里:

static void *ll_load (lua_State *L, const char *path, int seeglb) {
  HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);
  (void)(seeglb);  /* not used: symbols are 'global' by default */
  if (lib == NULL) pusherror(L);
  return lib;
}

LoadLibraryExA的调用返回NULL。我不确定为什么会发生这种情况,但是我已经使用mingw-gcc编译了相同的库,并能够需要dll。

点赞
用户2303714
用户2303714

我发现使用以下方法找不到依赖项:

http://www.dependencywalker.com/enter image description here

将单元测试移动到单独的项目中并更正解决方案设置。

2014-10-12 02:57:03