[英]How to marshal the multi byte character set C#
我需要調用一個使用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;
}
}
MSDN上的信息: 字符串的默認封送處理
您首先需要將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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.