外部接口应该是c++的:int func(int &pA,char * pB)函数是在无限循环中调用,当有外部输入时,将触发该调用,外部信息通过参数返回。我一开始在c#引用的是:[DllImport("XXX.dll")]
 public static extern int func(out int pA,out string pB);
调用在一个新开线程中: int pA = 0;
 string pB = string.Empty();while(true)
{
    int result = func(out pA,out pB);    if(result == 0)
     {
        //do sth;
     }
}结果当外部输入时,代码运行到func函数调用触发异常 System.StackOverflowException,堆栈溢出尝试修改引用
[DllImport("XXX.dll")]
 public unsafe static extern int func(out int pA, out char* pB);
int pA = 0;
 char* pB;while(true)
{
    int result = func(out pA,out pB);    if(result == 0)
     {
        //do sth;
     }
}如此,遇到外部输入时,func函数正常返回0,但进入if判断,企图读取pB内容(char*)时,触发异常AccessViolationException,内容为:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。我就不知道该怎么办了。该dll附有一个delphi写的例程,例程运行正常,接受外部设备的输入并在界面显示输入内容。我在c#就是不行请教各位,谢谢

解决方案 »

  1.   

    public static extern int func(out int pA, string pB);
    看看。
      

  2.   


    不行,去掉out,直接报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
    加上out又报堆栈溢出而且我要用到参数返回值,还必须用out了
      

  3.   


    [DllImport(...)]
    static extern int func(ref int a, StringBuilder s);
        StringBuilder s = new StringBuilder(1024);
        int a = 0;
        int result = func(ref a, s);
      

  4.   

    DllImport(...)]
    static extern int func(ref int a, StringBuilder s);
        StringBuilder s = new StringBuilder(1024);
        int a = 0;
        int result = func(ref a, s);这个不错。试试看。