0

I compiled the source code to dll, and want to use it in my Nvim plugin, but it was failed.

this is my dll source code

//func.hpp
#pragma once


#ifdef __WIN32
    #define EXPORT_DLL __declspec(dllexport)
#else
    #define EXPORT_DLL 
#endif

#include <iostream>
#include <lua.hpp>
#include <LuaBridge/LuaBridge.h>
#include <filesystem>
#include <string>

extern "C"
{
    EXPORT_DLL int extend(lua_State* L);
}
//func.cpp
#include "func.hpp"
#include <vector>

bool isdir(std::string input)
{
    std::filesystem::path path(input);
    return std::filesystem::exists(path);
}

bool mkdir(std::string input)
{
    std::filesystem::path path(input);
    return std::filesystem::create_directory(input);
}

bool mkdirs(std::string input)
{
    std::filesystem::path path(input);
    return std::filesystem::create_directories(input);
}

std::string getcwd()
{
    std::string path = std::filesystem::current_path().string();
    return path;
}

lua_State* L_ptr = nullptr;

luabridge::LuaRef listdir(std::string input)
{
    std::filesystem::path path(input);
    std::vector<std::string> __filesName;
    luabridge::LuaRef filesname_table = luabridge::newTable(L_ptr);
    for (const auto iter : std::filesystem::directory_iterator(path)) 
    {
        __filesName.push_back(iter.path().filename().string()); 

    } 
    for (int i = 1; i - 1 < __filesName.size(); ++i) 
    {
        filesname_table[i] = __filesName[i - 1];
    }
    return filesname_table;
}

EXPORT_DLL int extend(lua_State* L)
{
    L_ptr = L;
    using namespace luabridge;
    getGlobalNamespace(L)
        .addFunction("isdir", isdir)
        .addFunction("mkdir", mkdir)
        .addFunction("mkdirs", mkdirs)
        .addFunction("getcwd", getcwd)
        .addFunction("listdir", listdir);
    return 1;
}

this is my lua script

local script_path = debug.getinfo(1, "S").source:sub(2)
local script_dir = vim.fn.fnamemodify(script_path, ":h")
local extend = package.loadlib(script_dir.."/extend", "extend")
extend()

local function test()
    print(isdir("C:"))
end

return
{
    test = test
}

when i call my plugin in Nvim command lines whitlua require'test'.test(), the command lines opened the powershell. but when i call the dll directly in another lua script like this

local extend = package.load("extend", "extend")
print(isdir("C:"))

it can print true

when i change packge.loadlib to require it failed too. How can i use the dll in my plugin

Rizen
  • 21
  • 2

0 Answers0