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

时间:2018-05-11 13:58:52

标签: c# c++ lua-5.2

说明

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

问题:

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

代码:

  1. libutilscore项目(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项目(Lua CPP模块)
    • libutils.h

      #pragma once
      
      #ifdef LIBUTILS_EXPORTS
          #define LIBUTILS_API __declspec(dllexport)
      #else
          #define LIBUTILD_API __declspec(dllimport)
      #endif // LIBUTILS_EXPORTS
      
    • libutils.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;
      }
      

0 个答案:

没有答案