我要调用一个用C语言写的函数库中的一个函数.本来已经用delphi实现了,但现在我想用C#来调用该函数,但试了很多种办法,都提示尝试读取或写入受保护的内存.触发AccessViolationException异常的问题.麻烦高手帮忙解答.
C 函数申明
    int __stdcall dev_Init(char *ServerAddr, int ServerPort, void *event, int clientmode, char *LocalDir)delphi调用的函数申明
    function OnInit(ServerAddr: PCHAR; ServerPort: Integer; event: Pointer; ClientMode: Integer; LocalDir: PChar; LogerDir: PChar): Integer; stdcall;我用C#调用时是这么写的:
第一种写法:
public delegate void Init();[DllImport("CommLibWan.dll", ExactSpelling = false, SetLastError = true,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
    public static extern int dev_Init(string ServerAddr, int ServerPort, Init i, int clientmode, string  LocalDir);第二种写法:
[DllImport("CommLibWan.dll", ExactSpelling = false, SetLastError = true,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
    public static extern int dev_Init(StringBuilder ServerAddr, int ServerPort, Init i, int clientmode, StringBuilder  LocalDir);我还尝试过一些其他的办法,但似乎也不行.不知道是怎么回事?delphi

解决方案 »

  1.   

    看样子这个帖子是没有回复了
    不过我已经找到解决方法.
    原来我的在delphi中抄到的函数不是原函数,而是一个代理函数.
    C 原函数是这个:
      int __stdcall dev_Init(char *ServerAddr, int ServerPort, void *event, int clientmode, char *LocalDir, char *LogerDir, char *Prefix)改为C#函数为:   [DllImport("CommLibWan.dll", ExactSpelling = false, SetLastError = true,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
        public static extern int dev_Init(string ServerAddr, int ServerPort, Init i, int clientmode, string Local,string LogerDir, string Prefix);这样调用就没有问题了.