E-r*_*ich 6 c c# arrays pointers marshalling
在我的编组helloworld问题的基础上,我遇到问题,编组用C语言分配的数组到C#.我花了几个小时研究我可能出错的地方,但我尝试过的所有内容都会出现AccessViolationException等错误.
处理在C中创建数组的函数如下.
__declspec(dllexport) int __cdecl import_csv(char *path, struct human ***persons, int *numPersons)
{
int res;
FILE *csv;
char line[1024];
struct human **humans;
csv = fopen(path, "r");
if (csv == NULL) {
return errno;
}
*numPersons = 0; // init to sane value
/*
* All I'm trying to do for now is get more than one working.
* Starting with 2 seems reasonable. My test CSV file only has 2 lines.
*/
humans = calloc(2, sizeof(struct human *));
if (humans == NULL)
return ENOMEM;
while (fgets(line, 1024, csv)) {
char *tmp = strdup(line);
struct human *person;
humans[*numPersons] = calloc(1, sizeof(*person));
person = humans[*numPersons]; // easier to work with
if (person == NULL) {
return ENOMEM;
}
person->contact = calloc(1, sizeof(*(person->contact)));
if (person->contact == NULL) {
return ENOMEM;
}
res = parse_human(line, person);
if (res != 0) {
return res;
}
(*numPersons)++;
}
(*persons) = humans;
fclose(csv);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C#代码:
IntPtr humansPtr = IntPtr.Zero;
int numHumans = 0;
HelloLibrary.import_csv(args[0], ref humansPtr, ref numHumans);
HelloLibrary.human[] humans = new HelloLibrary.human[numHumans];
IntPtr[] ptrs = new IntPtr[numHumans];
IntPtr aIndex = (IntPtr)Marshal.PtrToStructure(humansPtr, typeof(IntPtr));
// Populate the array of IntPtr
for (int i = 0; i < numHumans; i++)
{
ptrs[i] = new IntPtr(aIndex.ToInt64() +
(Marshal.SizeOf(typeof(IntPtr)) * i));
}
// Marshal the array of human structs
for (int i = 0; i < numHumans; i++)
{
humans[i] = (HelloLibrary.human)Marshal.PtrToStructure(
ptrs[i],
typeof(HelloLibrary.human));
}
// Use the marshalled data
foreach (HelloLibrary.human human in humans)
{
Console.WriteLine("first:'{0}'", human.first);
Console.WriteLine("last:'{0}'", human.last);
HelloLibrary.contact_info contact = (HelloLibrary.contact_info)Marshal.
PtrToStructure(human.contact, typeof(HelloLibrary.contact_info));
Console.WriteLine("cell:'{0}'", contact.cell);
Console.WriteLine("home:'{0}'", contact.home);
}
Run Code Online (Sandbox Code Playgroud)
第一次human struct被编组很好.我在第一个之后得到了访问冲突异常.我觉得我错过了一些带有结构指针的编组结构.我希望我有一些我忽视的简单错误.你觉得这段代码有什么问题吗?
请参阅此GitHub要点获取完整资源.
// Populate the array of IntPtr
Run Code Online (Sandbox Code Playgroud)
这就是你出错的地方。您将返回一个指向指针数组的指针。第一个正确,实际上是从数组中读取指针值。但是你的 for() 循环出错了,只是将 4 (或 8)添加到第一个指针值上。而不是从数组中读取它们。使固定:
IntPtr[] ptrs = new IntPtr[numHumans];
// Populate the array of IntPtr
for (int i = 0; i < numHumans; i++)
{
ptrs[i] = (IntPtr)Marshal.PtrToStructure(humansPtr, typeof(IntPtr));
humansPtr = new IntPtr(humansPtr.ToInt64() + IntPtr.Size);
}
Run Code Online (Sandbox Code Playgroud)
或者更干净,因为已经支持简单类型的封送数组:
IntPtr[] ptrs = new IntPtr[numHumans];
Marshal.Copy(humansPtr, ptrs, 0, numHumans);
Run Code Online (Sandbox Code Playgroud)
我通过使用“调试”+“Windows”+“内存”+“内存”找到了该错误。 1. 将humanPtr放入“地址”字段中,切换到 4 字节整数视图,并观察到 C 代码正确执行。然后很快发现ptrs[]不包含我在内存窗口中看到的值。
不知道你为什么要编写这样的代码,除了作为一种心理练习。这不是正确的方法,例如,您完全忽略了再次释放内存的需要。这非常重要。在 C# 中解析 CSV 文件非常简单,并且与在 C 中解析一样快,它是 I/O 绑定的,而不是执行绑定的。您将轻松避免这些几乎不可能调试的错误,并从 .NET Framework获得大量帮助。