如何在 lua cpp 模块中调用托管 c++ dll 函数
How to call managed c++ dll functions in lua cpp module
描述:
我所有的函数都是在 C# dll 项目中定义的。然后我把c#函数包装在一个cpp库项目中,现在我想用c++写一个lua模块并调用包装函数。
问题:
如何在 lua cpp 模块中调用包装器函数?请给我一些建议,谢谢!
代码:
-
libutilscore Project(C# DLL(
namespace libutilscore { public static class SharpFTP { public static string ShowHello() { return "Hello From C Sharp."; } } }
-
ManagedDll Project(C++ DLL(
-
ManagedDll.h
#pragma once #ifdef MANAGEDDLL_EXPORTS #define MANAGEDDLL_API __declspec(dllexport) #else #define MANAGEDDLL_API __declspec(dllimport) #endif MANAGEDDLL_API const char *CPP_ShowHello();
-
ManagedDll.cpp
#include "ManagedDll.h" #include <string> using namespace System; using namespace std; using namespace libutilscore; namespace ManagedDll { public ref class CS_FTP { public: static string CS_ShowHello() { String ^ message = libutilscore::SharpFTP::ShowHello(); string result = ""; MarshallString(message, result); return result; } private: static void MarshallString(String ^csstr, string &stdstr) { using namespace Runtime::InteropServices; const char *chars = (const char*)(Marshal::StringToHGlobalAnsi(csstr)).ToPointer(); stdstr = chars; Marshal::FreeHGlobal(IntPtr((void *)chars)); } static void MarshallWstring(String ^csstr, string &wstr) { using namespace Runtime::InteropServices; const char *wchars = (const wchar_t*)(Marshal::StringToHGlobalAnsi(csstr)).ToPointer(); wstr = wchars; Marshal::FreeHGlobal(IntPtr((void *)wchars)); } }; } MANAGEDDLL_API string CPP_ShowHello() { return ManagedDll::CS_FTP::CS_ShowHello(); }
- libutils Project(Lua CPP Module(
-
libutils.h
#pragma once #ifdef LIBUTILS_EXPORTS #define LIBUTILS_API __declspec(dllexport) #else #define LIBUTILD_API __declspec(dllimport) #endif // LIBUTILS_EXPORTS
利 布利斯.cpp
#include "libutils.h" #include "lua.hpp" LIBUTILS_API int showHello(lua_State *luaEnv) { const char *msg = ""; // TODO Call DLL function // msg = CPP_ShowHello().c_str(); lua_pushstring(luaEnv, msg); return 1; } static const luaL_Reg libutils_funcs[] = { {"showHello", showHello}, {"NULL", NULL} }; LIBUTILS_API int luaopen_libutils(lua_State *luaEnv) { luaL_newlib(luaEnv, libutils_funcs); return 1; }
-
一种选择可能是将"Lua CPP 模块"设置为混合模式 DLL。 我之前已经回答过这个主题,所以请阅读我关于如何做到这一点的说明: VC++ 从解决方案中的非/clr 项目的函数调用/clr 项目的函数 简短的回答是,您可以编译 DLL 的特定部分以使用 CLR,而无需以这种方式进行整个操作, 然后在他们之间呼叫。
基本上,一旦你弄清楚了如何从PURE C++代码调用混合模式DLL,那么这个问题就非常相似了。 我不知道 LUA C++模块的限制,尽管我认为如果它们可以加载/调用其他 DLL,那么你很好。
为了获得额外的乐趣,然后弄清楚如何来回传递信息。 并从 C#/.NET 调用纯C++。 或与每个回调。 这很快就会变得非常有趣。
相关文章:
- 挂起和取消挂起一个文件DLL
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 导入库可以跨dll版本工作吗
- 从C++dll访问C#中的一行主要参数
- 链接到自行创建的dll失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- C++:将外部库链接到dll库
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 不同的Visual Studio版本中缺少.dll
- 从DLL中删除类的实例
- 如何包装第三方DLL在R中使用
- 使用c#访问c++dll中带有char*参数的函数时发生AccessViolationException
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- Python ctypes:不会按预期加载 dll
- 在 C++/CLI 中将 .NET 事件从一个 DLL 引发到另一个 DLL