1

我需要调用一个使用 C# 中的多字节字符集的函数 (c/c++)。但我不知道如何将它编组为多字节。有谁知道如何将结果转换为字符串?

C#:

[DllImport("essentials.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern System.IntPtr GetFiles(string filedir, string path);

        [STAThread]
        static void Main()
        {
            string filedir = @"C:\Users\Ruben\Documents\School\*";
            string path = @"C:\Users\Ruben\Documents\School\";
            System.IntPtr pointer = GetFiles(filedir, path);
            string files = Marshal.PtrToStringAnsi(pointer); // nothing
        }

C++

extern "C"
{
    __declspec(dllexport) char* GetFiles(char* filedir, char* path)
    {
        string filedir2 = filedir;
        string path2 = path;
        string files = GetFiles2(filedir2, path2);
        char* Rfiles = new char[files.length() + 1];
        strcpy_s(Rfiles, files.length() + 1, files.c_str());
        return Rfiles;
    }
}
4

2 回答 2

0

MSDN 上的信息:字符串的默认封送处理

于 2012-09-24T19:33:42.200 回答
0

您首先需要编组 C# 字符串,试试这个:

[DllImport("essentials.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    public static extern System.IntPtr GetFiles
        ([MarshalAs(UnmanagedType.AnsiBStr)]string filedir,
        [MarshalAs(UnmanagedType.AnsiBStr)]string path);
于 2012-09-28T09:51:14.737 回答