C#如何使用指针

C#如何使用指针,c#,pointers,C#,Pointers,使用“API”,其中函数的定义如下 int foo(char *arg1, short *arg2); 所以我做了这个 [DllImport(@"path\file.dll")] public static extern int foo(ref string arg1, ref int arg2); 但是当我给福打电话的时候 int ret_val = foo(ref arg1, ref arg2); 它抛出一个System.AccessViolationException 如果我实现ar

使用“API”,其中函数的定义如下

int foo(char *arg1, short *arg2);
所以我做了这个

[DllImport(@"path\file.dll")]
public static extern int foo(ref string arg1, ref int arg2);
但是当我给福打电话的时候

int ret_val = foo(ref arg1, ref arg2);
它抛出一个
System.AccessViolationException

如果我实现arg1时没有ref关键字,只有值类型,则不会引发异常


我做错什么了吗?如何定义C#中的指针?

字符串已经是引用,您没有字符**,但请注意System.string是wchar#t,而不是char。检查有关封送处理的文档尝试使用:publicstaticexternintfoo(StringBuilder arg1、StringBuilder arg2);StringBuilder正在工作,谢谢!不客气:)