VS2010-C#引用delphi6下编译的dll文件,读出byte[]报错“Cannot marshal 'return value': Invalid managed/unmanaged type combination.”
delphi6下编译的dll文件 源码如下:
library dataoutdll;
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids, DBClient, StdCtrls, Provider;
type
byteBuffer = array[0..1024] of byte;
var
mydataset:tclientdataset;
mydatasource:tdatasource;
myprovider: tdatasetprovider;
myquery:tadoquery;
sqlcmd:TStringList;
ms1,ms2,ms3:TMemoryStream;
buf: bytebuffer;
{$R *.res}
function getbytes():bytebuffer;stdcall;
begin
ms1:=TMemoryStream.Create;
ms2:=TMemoryStream.Create;
ms3:=TMemoryStream.Create;
mydataset:=tclientdataset.Create(nil);
mydatasource:=tdatasource.Create(nil);
myprovider:=tdatasetprovider.Create(nil);
myquery:=tadoquery.Create(nil);
sqlcmd:=tstringlist.Create;
sqlcmd.Add('select * from trequestprice');
myquery .ConnectionString :=
'Provider=SQLNCLI.1;Password=qqqqqq;Persist Security Info=True;Us' +
'er ID=sa;Initial Catalog=HSEms;Data Source=.' ;
myquery .CursorType := ctStatic ;
myquery.SQL.AddStrings(sqlcmd) ;
myquery.SQL.Strings [0];
myquery .Active := True;
myprovider.DataSet :=myquery;
mydataset.SetProvider(myprovider) ;
mydataset.Active := True ;
mydatasource.DataSet := mydataset;
mydataset.SaveToStream(ms3);
ms3.Position :=0;
FillChar(buf,1025,0);
ms3.Read (buf,sizeof(buf));
ms3.Position :=0;
result:=buf;
end;
Exports
getbytes;
begin
end.
代码功能:读取表trequestprice到内存buf中,作为dll的返回值吐出给C#。
VS2010-C#下调用该dll的代码如下:
[DllImport("dataoutdll.dll", EntryPoint = "getbytes", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern byte[] getbytes();
private void button1_Click(object sender, EventArgs e)
{
try
{
byte[] mybt = new byte[1024];
mybt = getbytes();//程序执行到这里报错
}
catch (Exception ex)
{ MessageBox.Show(ex .Message ); }
}
错误提示为:Cannot marshal 'return value': Invalid managed/unmanaged type combination.
请问各位:delphi6编译下的dll中的array[0..1024] of byte 跟C#下的byte[1024],在内存上是等效的吗?