我有一个在C++中的函数_GetFileList,我正在尝试让它返回一个字符串列表/数组给C#代码。我不必非要使用wstring *或其他任何类型,这只是我找到的例子所采用的方法,但我正在尝试让它返回一个列表/数组而不仅仅是单个字符串。如何进行编组处理,以在C#端具有多个字符串?
C++代码:
extern "C" __declspec(dllexport) wstring * __cdecl _GetFileList(int &size){
std::list<wstring> myList;
//Code that populates the list correctly
size = myList.size();
wstring * arr = new wstring[size];
for( int i=0;i<size;i++){
arr[i] = myList.front();
myList.pop_front();
}
return arr;
}
I call that from C# like this:
[DllImport(@"LinuxDirectory.Interface.dll")]
private static extern IntPtr _GetFileList(ref int size);
public bool GetFileList() {
int size = 0;
IntPtr ptr = _GetFileList( ref size );
//WHAT DO I DO HERE TO GET THE LIST OF STRINGS INTO SOMETHING I CAN READ?
}
我尝试使用Marshal.PtrToStringUni, Marshal.PtrToStringAnsi以及结构体来实现,但似乎无法正确使用语法。
std::wstring
,而不仅仅是在内部使用。 - Ben Voigt