如何在 lua cpp 模块中调用托管 c++ dll 函数

How to call managed c++ dll functions in lua cpp module

本文关键字:c++ dll 函数 调用 lua cpp 模块      更新时间:2023-10-16

描述

我所有的函数都是在 C# dll 项目中定义的。然后我把c#函数包装在一个cpp库项目中,现在我想用c++写一个lua模块并调用包装函数。

问题:

如何在 lua cpp 模块中调用包装器函数?请给我一些建议,谢谢!

代码:

  1. libutilscore Project(C# DLL(

    namespace libutilscore
    {
    public static class SharpFTP
    {
    public static string ShowHello()
    {
    return "Hello From C Sharp.";
    }
    }
    }
    
  2. 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();
      }
      
      1. 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++。 或与每个回调。 这很快就会变得非常有趣。