【问题标题】:C++ dll pass array to C#C++ dll将数组传递给C#
【发布时间】:2013-09-20 20:37:10
【问题描述】:

我必须从我的 C++ dll 数组传递给 C# 程序。 这是 C++ 代码:

 __declspec(dllexport) std::vector<std::string> __stdcall 
    getFilesByDirs
    (
        string directory, 
        std::string fileFilter, 
        bool recursively = true
    )
{
    int __ID = 0;
    std::vector<std::string> filesArray;

    if (recursively)
        getFilesByDirs(directory, fileFilter, false);

    directory += "\\";

    WIN32_FIND_DATAA FindFileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;

    std::string filter = directory + (recursively ? "*" : fileFilter);

    hFind = FindFirstFileA(filter.c_str(), &FindFileData);

    if (hFind == INVALID_HANDLE_VALUE)
    {
        return filesArray;
    }
    else
    {
        if (!recursively)
        {
            if(isGoodForUs(directory + std::string(FindFileData.cFileName))) 
            {
                filesArray[__ID] = directory + std::string(FindFileData.cFileName);
                __ID ++;
            }
        }

        while (FindNextFileA(hFind, &FindFileData) != 0)
        {
            if (!recursively)
            {
                if(!isGoodForUs(directory + std::string(FindFileData.cFileName))) continue;
                filesArray[__ID] = directory + std::string(FindFileData.cFileName);
            }
            else
            {
                if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)>0 && FindFileData.cFileName[0]!='.')
                {
                    if(!isGoodForUs(directory + std::string(FindFileData.cFileName))) continue;
                    getFilesByDirs(directory + std::string(FindFileData.cFileName), fileFilter);
                }
            }
            __ID ++;
        }

        DWORD dwError = GetLastError();
        FindClose(hFind);
    }

    return filesArray;
}

如您所见,有一个按类型扫描文件的功能。 保存在字符串数组中并返回。

现在,这里是 C# 代码:

[DllImport(@"files.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr getFilesByDirs(string path, string exns, bool rec=true);

然后我调用这个方法:

IntPtr a = getFilesByDirs("C:\\", "*.docx");

问题是没有任何东西从 Dll 传递给我的 C# 程序。 任何人都可以帮助解决我的问题?

【问题讨论】:

  • 您不能将std::vector&lt;std::string&gt; 返回到c# 领域,将其解释为IntPtr 肯定是没有意义的。您只能返回基本类型(不使用 COM 或 CLI)。因此,您需要返回一个指向(非堆栈)内存的指针(可能是char*,即字符串)。
  • @EdS。您能解释一下如何通过 C++ 将常规数组返回到 C# 吗?
  • 确保你确实需要调用这个野兽。如果“isGoodForUs”足够短,可以在 C# 中实现,或者可以更轻松地自行调用,则 C++ 函数的其余部分可以表示为内置 C# 函数的两行代码。

标签: c# c++


【解决方案1】:

您需要返回的不是向量,而是原始类型,例如 char**。这里的问题是,为了使用它,您需要知道集合中有多少。作为埃德。 S 在 cmets 中说,您可能会发现使用 CLI 更容易(如果您可以使用 /clr 编译您的 C++)。但如果你能做到这一点,那么你可以在 C++ 中使用 .Net 类型(例如 List)和 C++ 端的marshall your char*'s to Strings。

否则,您可以在 .Net 端返回原始 char* 的句柄。要在 C# 中使用 char*(可能已经使用过 C++,您会对此功能感到满意),您可以使用 unsafe 关键字,这样您就可以将这些 char* 转换为 System::String 在 unsafe 中并添加将它们添加到您的 .Net 收藏中。

【讨论】:

    猜你喜欢
    • 2013-02-26
    • 2021-01-29
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多