Delphi中的原型函数:Function DecryptionCode(EnStr,s:string):string; stdcall ;
Function LoginJudge(IP,User,Pwd,Dt :string):Boolean; stdcall ;
C#中调用方法:
using System.Runtime.InteropServices;[DllImport("LoginIdentAuth.dll")]          
public static extern string DecryptionCode(string myName, string myPwd);
//
[DllImport("LoginIdentAuth.dll")]           
public static extern bool LoginJudge(string IP, string User,string Pwd,string Dt);//
private void button1_Click(object sender, EventArgs e)
{
   string myValue=DecryptionCode("用户名","用户密码");//用户密码有特殊符,有单引号,双引号,$,&等.运行时报错:尝试读取或写保存的内存
   bool myBool=LoginJudge("192.168.0.1","sa","sa","sys");//运行时报错:外部组件发生异常
}

解决方案 »

  1.   

    在网上搜了一大圈,很多说是C#与Delphi中的string类型不一样,才导致这个错误,说将delphi中的string改成Pchar类型,可是Delphi写的dll没有源码,改不了.
      

  2.   


    delphi中的string 对应C#中的 StringBuilder
    我也遇到过这样的问题
      

  3.   

    就是这种方法,必须将这个DELPHI中编写的DLL的pchar修改成string
    才行
      

  4.   

    [DllImport("LoginIdentAuth.dll")]   
    public static extern string DecryptionCode(StringBuilder myName, StringBuilder myPwd);或者
    [DllImport("LoginIdentAuth.dll")]   
    public static extern StringBuilder DecryptionCode(StringBuilder myName, StringBuilder myPwd);试试看呢