实际上我在 C# 中使用 C++ dll,如下所示
我的 C++ 代码
extern "C" __declspec(dllexport)
char** __stdcall hh()
{
static char* myArray[3] = {"A1", "BB2", "CC3",};
return myArray;
}
我的 C# 代码
[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() 返回的数组大小;如何获取数组大小以便我的代码行
字符串[] s=新字符串[100]; 更改为 string[] s=new string[实际数组长度];
最佳答案
您要么也必须返回数组的大小(可能是一个引用变量),要么您可以向列表添加一个 NULL 终止符。如果您使用 NULL 终止符,您可以遍历数组直到找到 NULL 以确定大小。
关于c# - 如何知道从 C++ dll 传递到 C# 的数组的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10100162/