Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何知道从C++;dll到C# 实际上,我在C++中使用C++ dll,如下:< /p> 我的C++代码 < /p> extern "C" __declspec(dllexport) char** __stdcall hh() { static char* myArray[3] = {"A1", "BB2", "CC3",}; return myArray; } [DllImport(@"ourdll.dll",CharSet = CharSet.Ansi,CallingConvention = CallingConvention.StdCall)] public static extern IntPtr hh(); static void Main(string[] args) { IntPtr a = hh(); int j = 0; string[] s=new string[100]; //I want this to be dynamic do { s[j] = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(a,4*j)); j++; } while(s[j-1] != null); }_C#_C++_Interop_Marshalling - Fatal编程技术网

如何知道从C++;dll到C# 实际上,我在C++中使用C++ dll,如下:< /p> 我的C++代码 < /p> extern "C" __declspec(dllexport) char** __stdcall hh() { static char* myArray[3] = {"A1", "BB2", "CC3",}; return myArray; } [DllImport(@"ourdll.dll",CharSet = CharSet.Ansi,CallingConvention = CallingConvention.StdCall)] public static extern IntPtr hh(); static void Main(string[] args) { IntPtr a = hh(); int j = 0; string[] s=new string[100]; //I want this to be dynamic do { s[j] = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(a,4*j)); j++; } while(s[j-1] != null); }

如何知道从C++;dll到C# 实际上,我在C++中使用C++ dll,如下:< /p> 我的C++代码 < /p> extern "C" __declspec(dllexport) char** __stdcall hh() { static char* myArray[3] = {"A1", "BB2", "CC3",}; return myArray; } [DllImport(@"ourdll.dll",CharSet = CharSet.Ansi,CallingConvention = CallingConvention.StdCall)] public static extern IntPtr hh(); static void Main(string[] args) { IntPtr a = hh(); int j = 0; string[] s=new string[100]; //I want this to be dynamic do { s[j] = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(a,4*j)); j++; } while(s[j-1] != null); },c#,c++,interop,marshalling,C#,C++,Interop,Marshalling,我的C#代码 extern "C" __declspec(dllexport) char** __stdcall hh() { static char* myArray[3] = {"A1", "BB2", "CC3",}; return myArray; } [DllImport(@"ourdll.dll",CharSet = CharSet.Ansi,CallingConvention = CallingConvention.StdCall)] p

我的C#代码

extern "C" __declspec(dllexport)
    char**  __stdcall hh()
{
 static char* myArray[3] = {"A1", "BB2", "CC3",};
    return myArray;
}
[DllImport(@"ourdll.dll",CharSet = CharSet.Ansi,CallingConvention = CallingConvention.StdCall)]     
      public static extern IntPtr hh();
       static void Main(string[] args)
        {
            IntPtr a = hh();
            int j = 0;
            string[] s=new string[100]; //I want this to be dynamic
           do
           {
               s[j] = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(a,4*j));
               j++;
           }
           while(s[j-1] != null);
        }
无法获取hh()返回的数组大小;如何获得数组的大小,以便我的代码行

string[]s=新字符串[100]; 更改为
string[]s=新字符串[数组的实际长度]

您也必须返回数组的大小(可能是引用变量),或者可以向列表中添加空终止符。如果使用空终止符,您可以在数组中循环,直到找到空为止,以确定大小。

您也必须返回数组的大小(可能是引用的变量),或者您可以将空终止符添加到列表中。如果使用空终止符,可以在数组中循环,直到找到空为止,以确定大小。

如果要在PInvoke中找到数组的长度,可以使用
封送.SizeOf(object)
方法


它适用于我的结构

如果你想在PInvoke中找到数组的长度,你可以使用
Marshal.SizeOf(object)
方法


它适用于我在访问数组元素时使用的结构

,但是有没有办法通过编组来知道返回的数组的大小。该函数返回一个char**。它没有自动计算出大小的概念。它只是一个指向字符的指针。它正好指向一个指针数组,但没有任何信息告诉编译器情况就是这样。我正在使用它访问数组的元素,但是否有任何方法通过编组来知道返回的数组的大小。函数返回一个char**。它没有自动计算出大小的概念。它只是一个指向字符的指针。它恰好指向一个指针数组,但没有任何东西告诉编译器情况是这样的