从C&x2B接收字符**+;DLL转换为C#字符串[]

从C&x2B接收字符**+;DLL转换为C#字符串[],c#,c++,arrays,string,dll,C#,C++,Arrays,String,Dll,尽管有这么多问题,我还是找不到一个合适的答案 我的目标是使用返回字符**的DLL来填充字符串[] DLL声明: extern "C" SHTSDK_EXPORT int GetPeerList(SHTSDK::Camera *camera, int* id, int id_size, char** name, int name_size, int* statut, int statut_size); [DllImport(libName)] static public extern int G

尽管有这么多问题,我还是找不到一个合适的答案

我的目标是使用返回字符**的DLL来填充
字符串[]

DLL声明

extern "C" SHTSDK_EXPORT int GetPeerList(SHTSDK::Camera *camera, int* id, int id_size, char** name, int name_size, int* statut, int statut_size);
[DllImport(libName)]
static public extern int GetPeerList(IntPtr camera, IntPtr id, int id_size, IntPtr name, int name_size, IntPtr statut, int statut_size);
StringBuilder[] name = new StringBuilder[nbPeer];
for (int i = 0; i < nbPeer; i++)
{
     name[i] = new StringBuilder(256);
}
//Alloc peer name array
GCHandle nameHandle = GCHandle.Alloc(name, GCHandleType.Pinned);
IntPtr pointeurName = nameHandle.AddrOfPinnedObject();

int notNewConnection = APIServices.GetPeerList(cameraStreaming, pointeurId, 

nbPeer, pointeurName, nbPeer, pointeurStatut, nbPeer);

// Now I'm supposed to read string with name[i] but it crashes
我的导入

extern "C" SHTSDK_EXPORT int GetPeerList(SHTSDK::Camera *camera, int* id, int id_size, char** name, int name_size, int* statut, int statut_size);
[DllImport(libName)]
static public extern int GetPeerList(IntPtr camera, IntPtr id, int id_size, IntPtr name, int name_size, IntPtr statut, int statut_size);
StringBuilder[] name = new StringBuilder[nbPeer];
for (int i = 0; i < nbPeer; i++)
{
     name[i] = new StringBuilder(256);
}
//Alloc peer name array
GCHandle nameHandle = GCHandle.Alloc(name, GCHandleType.Pinned);
IntPtr pointeurName = nameHandle.AddrOfPinnedObject();

int notNewConnection = APIServices.GetPeerList(cameraStreaming, pointeurId, 

nbPeer, pointeurName, nbPeer, pointeurStatut, nbPeer);

// Now I'm supposed to read string with name[i] but it crashes
我在C代码中的用法

extern "C" SHTSDK_EXPORT int GetPeerList(SHTSDK::Camera *camera, int* id, int id_size, char** name, int name_size, int* statut, int statut_size);
[DllImport(libName)]
static public extern int GetPeerList(IntPtr camera, IntPtr id, int id_size, IntPtr name, int name_size, IntPtr statut, int statut_size);
StringBuilder[] name = new StringBuilder[nbPeer];
for (int i = 0; i < nbPeer; i++)
{
     name[i] = new StringBuilder(256);
}
//Alloc peer name array
GCHandle nameHandle = GCHandle.Alloc(name, GCHandleType.Pinned);
IntPtr pointeurName = nameHandle.AddrOfPinnedObject();

int notNewConnection = APIServices.GetPeerList(cameraStreaming, pointeurId, 

nbPeer, pointeurName, nbPeer, pointeurStatut, nbPeer);

// Now I'm supposed to read string with name[i] but it crashes
StringBuilder[]name=新的StringBuilder[nbPeer];
对于(int i=0;i
我错过了什么?我真的搜索了其他主题,我认为可以工作,但仍然崩溃


谢谢。

我建议您开发一个小型的C++/CLI桥接层。此C++/CLI桥的目的是获取DLL以
char**
raw指针的形式返回的字符串数组,并将其转换为.NET字符串数组,该数组可以作为简单的
string[]
在C代码中使用

C++/CLI版本的C#
string[]
(字符串数组)是
array^
,例如:

array<String^>^ managedStringArray = gcnew array<String^>(count);
您可能会在C++/CLI阵列上找到一本有趣的读物



p.S.从本机DLL返回的字符串的格式为
char
-strings。另一方面,.NET字符串是Unicode UTF-16字符串。因此,您需要澄清在本地字符串中使用什么编码来表示文本,并将其转换为.NET字符串的UTF16。

我建议使用混合的汇编(VisualC++与CLI支持),并将其用作本地(C++)函数的包装器。这比你现在做的要容易得多。也许这会有帮助?