我用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 中。其他信息: 未将对象引用设置到对象的实例。在网上查了一天了,也没解决,急死了高手们,指望你们了!!!
未处理的“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 中。其他信息: 未将对象引用设置到对象的实例。在网上查了一天了,也没解决,急死了高手们,指望你们了!!!
解决方案 »
- vs2005怎么写代码的时候数字好像是大写的啊,怎么变成小写啊
- Winform 窗体中panel控件嵌套另一个窗体?求疑惑
- 最近做个小东西 ,遇到了一些问题 ,希望诸位慷慨解囊
- C# WINFORM 鼠标事件问题
- SqlHelper.ExecuteDataset及其方法在msdn里没有,为什么,是不是这个方法落后了,还是什么原因?
- 问个关于WinFormUI的问题
- 求教---《BEGINNING C# 入门经典》为什么我的MenuItemFiles 中没有Top level属性
- 我在得到datagrid行数据的时候,遇到以下问题
- 求教-----一条SQL语句的写法
- 急!高分求解,关于调用COM+组件方法的问题
- C#中socket编程如果重用本地端口
- 高分紧急求援:Socket 接收数据问题 在线等待
SrcFile: pchar;
你说的方法我也试过了,不行,同样的错误
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);
[DllImport("XlsRW.dll",CallingConvention=CallingConvention.StdCall)]
public static extern string XlsGet_Cell(string SrcFile, int iSheet, int nRow, int nCol);
SrcFile: string; 改为:
SrcFile: pchar;
应该就可以了啊.我以前也遇到过,就是这样一来解决的啊.
我想在dll里exports几个函数,如:
exports
XLSCreateObject,
XLSClose,
XLSOpen,
XLSGet_Cell,
XLSSet_Cell,
XLSSave;在 XLSCreateObject里创建了一个对象,然后在其它函数里引用,这样的DLL在delphi里可以用,拿到c#里如何用呢?
2、参数传递的方向需要一致,stdcall
3、去网上搜索一下.Net P/Invoke的相关文章
我也遇到过一次类似情况,不管怎么写都调用不了,后来就按COM来处理了,比较麻烦。
除此之外,也可以试试反射。
我想在dll里exports几个函数,如:
exports
XLSCreateObject,
XLSClose,
XLSOpen,
XLSGet_Cell,
XLSSet_Cell,
XLSSave;在 XLSCreateObject里创建了一个对象,然后在其它函数里引用,这样的DLL在delphi里可以用,拿到c#里如何用呢?