在c#中调用DELPH编写的DLL的函数时,老是出现未将对象引用到实例的错误:
delphi编写的dll中函数声明:function DeliverfromEsm(var Smtype, Content,Caller:pchar):integer;
我在c#中声明:
[DllImport("EsmApi.dll",CharSet   =   CharSet.Ansi,CallingConvention   =   CallingConvention.StdCall)]
static extern int DeliverfromEsm(ref string smtype,ref string content,ref string caller);
调用:
string smtype="";
string content="";
string caller="";
int reportval=DeliverfromEsm( ref smtype,ref content,ref caller);
出现未将对象引用到实例的错误,请问我该如何正确调用?

解决方案 »

  1.   

    哪一行出现的错误?如果是最后一行,请用public static   extern   int   DeliverfromEsm(ref   string   smtype,ref   string   content,ref   string   caller); 
      

  2.   

    估计是你的delphi代码有问题
     library   Project2;   
      function   DeliverfromEsm(var   Smtype,   Content,Caller:pchar):integer;stdcall;  begin
          Smtype:='Smtype'  ;
          Content:='Content'  ;
          Caller:='Caller'  ;
          result:=0;
      end;
          exports
          DeliverfromEsm;
      begin
      end.[DllImport("EsmApi.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            static extern int DeliverfromEsm(ref   string smtype, ref   string content, ref   string caller);
            private void button1_Click(object sender, EventArgs e)
            {
                string smtype = "";
                string content = "";
                string caller = "";            int i=DeliverfromEsm(ref smtype, ref content, ref caller);        }
      

  3.   

    调用这个函数的时候DeliverfromEsm(   ref   smtype,ref   content,ref   caller); 出问题
    前边加public,这么改不行
      

  4.   

    function   DeliverfromEsm(var   Smtype,   Content,Caller:pchar):integer;stdcall;我测试过我的代码是没有问题的
      

  5.   

    to:jinjazz
    我试过你的方法,虽然这样调用可以,但delphi提供的dll,是别人提供的,我不知怎么实现这个函数的,content参数是个地址,可能指向字符串,也可能指向一个结构。
      

  6.   

    这么测试是可以的,但实际调用dll中的那个函数就有问题了,关键不知delphi中那个函数怎么实现的啊
      

  7.   

    http://msdn2.microsoft.com/zh-cn/library/tcy5wf0h(vs.80).aspx
      

  8.   

    调用产生的异常:
    System.NullReferenceException: 未将对象引用设置到对象的实例。
       at UMSClient.Form1.DeliverfromEsm(String& Smtype, String& Content, String& Caller)
      

  9.   

    我在c#中这么改: 
    [DllImport("EsmApi.dll",CharSet       =       CharSet.Ansi,CallingConvention       =       CallingConvention.StdCall)] 
    static   extern   int   DeliverfromEsm(string   smtype, string   content, string   caller); 
    调用: 
    string   smtype=""; 
    string   content=""; 
    string   caller=""; 
    int   reportval=DeliverfromEsm(  smtype,content,caller); 
    调用正常,但几个参数没有值。为什么ref报错呢
      

  10.   

    PChar本身就是指针,相当于C/C++的char*,而前面加var后相当于C/C++的char**
    delphi编写的dll中函数声明:function   DeliverfromEsm(var   Smtype,   Content,Caller:pchar):integer;
    我在c#中怎么调用呢?
      

  11.   

    Change string with StringBuilder[DllImport("EsmApi.dll",CharSet       =       CharSet.Ansi,CallingConvention       =       CallingConvention.StdCall)]
    static   extern   int   DeliverfromEsm( StringBuilder smtype,StringBuildercontent,StringBuilder caller);
    调用:
    StringBuilder smtype= new StringBuilder(256);//Init buffer
    StringBuilder content= new StringBuilder(256);//Init buffer
    StringBuilder caller= new StringBuilder(256);//Init buffer
    int   reportval=DeliverfromEsm(   smtype,content,caller); 
      

  12.   

    to:knight94
    谢谢,这样不报错,3个参数也取到值了,但取到的值后两个是乱码,第一个也不是想要的结果。
    我要做的东西是,根据联通提供的企信通开发接口,调用他们提供的用delphi做的dll发送短信,dll只有文档说明,没有具体调用示例代码。联通的人只会说别人可以正常调用,别的什么也不知道。
      

  13.   

    Have a try!
    [DllImport("EsmApi.dll",CharSet               =               CharSet.Auto,CallingConvention               =               CallingConvention.StdCall)]
    static       extern       int       DeliverfromEsm(   StringBuilder   smtype,StringBuildercontent,StringBuilder   caller);
      

  14.   

    content参数有可能是手机号码,也有可能是一个结构体,可能是这个原因,是手机号码的时候正常显示,是结构的时候显示乱码
      

  15.   

    Content:短信内容或报告(若Smtype为即时消息或离线消息,则Content表示短信内容;否则,Content表示返回的状态报告情况);
       
    Content表示返回的状态报告Report结构:
         DWORD TaskID; //客户端序列号 TaskID
        DWORD MsgID; //客户端序列号 MsgID
        char UserNumber[22]; //接收手机号
        unsigned char state; //状态
        unsigned char ErrCode; //错误代码我该怎么声明和调用呢?content用StringBuilder类型,是状态报告的时候,收到是乱码。定义一个结构体,用这个类型的时候,接收状态报告时,出错。
      

  16.   

    [DllImport("EsmApi.dll",CharSet  =    CharSet.Auto,CallingConvention =    CallingConvention.StdCall)] 
    static    extern   int    DeliverfromEsm( StringBuilder smtype,StringBuilder content,StringBuilder  caller);
    CharSet 不管用什么值,用下边的方法,content都是2个字节啊。
    byte[] bytecontent=Encoding.Unicode.GetBytes(content.ToString());
    bytecontent.Length.ToString()怎么回事呢?
      

  17.   

    我该怎么声明和调用呢?content用StringBuilder类型,是状态报告的时候,收到是乱码。定义一个结构体,用这个类型的时候,接收状态报告时,出错。
    ===
    试试IntPtr content
    使用Marshal.StructureToPtr()和Marshal.StringToBSTR()方法调用
      

  18.   

    谢谢,zxkid
    问题差不多解决了,我再测试测试。很快就结贴
      

  19.   

    谢谢大家了,尤其感谢zxkid。结贴
      

  20.   

    调用API发送 我都没搞定呢!
    虽然能正常调用了
    但初始化总返回-1
    郁闷那API说明又狂简单