我用delphi7写了一个DLL,在delphi7下调用没有任何问题,在c#里调用提示:
未处理的“System.NullReferenceException”类型的异常出现在 D7DLL.exe 中。其他信息: 未将对象引用设置到对象的实例。delphi7写的dll的源代码:
library XlsRW;uses
  XLSReadWriteII2,
  ActiveX,
  SysUtils,
  Classes;var
    Xls: TXLSReadWriteII2;{$R *.res}function XlsGet_Cell(SrcFile: string; iSheet, nRow, nCol: Word): WideString; stdcall;
begin
try
    Xls := TXLSReadWriteII2.Create(nil);
    Xls.Filename := SrcFile;
    Xls.Read;
    Result := Xls.Sheets[iSheet - 1].aswidestring[nCol - 1, nRow - 1];
finally
    Xls.Free;
end;
end;exports
    XlsGet_Cell;begin
  Coinitialize(nil);
end.
delphi7调用的代码:
function XlsGet_Cell(SrcFile: string; iSheet, nRow, nCol: Word): WideString; stdcall; external 'XlsRW.dll';procedure TForm1.Button3Click(Sender: TObject);
var
  s: WideString;
begin
    s := XlsGet_Cell(edit1.text, 1, 1, 1);
    Memo1.Lines.Add(s);
end;c#调用DLL的代码:
[DllImport("XlsRW.dll",CharSet=CharSet.Ansi,EntryPoint="XlsGet_Cell")]
public static extern string XlsGet_Cell(string SrcFile, int iSheet, int nRow, int nCol);private void button2_Click(object sender, System.EventArgs e)
{
textBox2.Text +=  XlsGet_Cell(textBox1.Text, 1, 1, 1);
}
编译可以通过,就报错:
未处理的“System.NullReferenceException”类型的异常出现在 D7DLL.exe 中。其他信息: 未将对象引用设置到对象的实例。在网上查了一天了,也没解决,急死了高手们,指望你们了!!!

解决方案 »

  1.   

    SrcFile: string; 改为:
    SrcFile: pchar;
      

  2.   

    to  aiirii(ari-http://spaces.msn.com/members/aiirii/) ( ) 
    你说的方法我也试过了,不行,同样的错误
      

  3.   

    delphi要如下声明:
    function XlsGet_Cell(SrcFile: pchar; iSheet, nRow, nCol: Word): pchar; stdcall;C#要如下引用:
    [DllImport("XlsRW.dll",CharSet=CharSet.Ansi,EntryPoint="XlsGet_Cell",CallingConvention=CallingConvention.StdCall)]
    public static extern string XlsGet_Cell(string SrcFile, int iSheet, int nRow, int nCol);
      

  4.   

    如果还有问题,再试试如下的:
    [DllImport("XlsRW.dll",CallingConvention=CallingConvention.StdCall)]
    public static extern string XlsGet_Cell(string SrcFile, int iSheet, int nRow, int nCol);
      

  5.   


    SrcFile: string; 改为:
    SrcFile: pchar;  
    应该就可以了啊.我以前也遇到过,就是这样一来解决的啊.
      

  6.   

    delphi不要用string ,全部该成pChar ,除非你的dll也有问题
      

  7.   

    问各位高手:
    我想在dll里exports几个函数,如:
    exports
        XLSCreateObject,
        XLSClose,
        XLSOpen,
        XLSGet_Cell,
        XLSSet_Cell,
        XLSSave;在 XLSCreateObject里创建了一个对象,然后在其它函数里引用,这样的DLL在delphi里可以用,拿到c#里如何用呢?
      

  8.   

    1、Delphi里不要用string类型也不要用widestring,用pchar代替
    2、参数传递的方向需要一致,stdcall
    3、去网上搜索一下.Net P/Invoke的相关文章
      

  9.   

    楼上说的很全面了,除了上面的就是pinvoke ,注意你所引用的api是标准的,要把他们引用过来
      

  10.   

    看看http://www.graphics.net.cn/bbs/delphi/0538/084.asp
    我也遇到过一次类似情况,不管怎么写都调用不了,后来就按COM来处理了,比较麻烦。
    除此之外,也可以试试反射。
      

  11.   

    我已经写了一个com,但是,由于这个dll又要在pb里用,不知道pb能否掉用com,所以只能写成普通的dll来用,并且楼上的几位好像都没有回答:
    我想在dll里exports几个函数,如:
    exports
        XLSCreateObject,
        XLSClose,
        XLSOpen,
        XLSGet_Cell,
        XLSSet_Cell,
        XLSSave;在 XLSCreateObject里创建了一个对象,然后在其它函数里引用,这样的DLL在delphi里可以用,拿到c#里如何用呢?