unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
// function ado1(yunbian:string;udl:string):string;stdcall;
private { Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
function ado1(yunbian:string;udl:string):string;stdcall;external'ADO.dll';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(ado1('aa','Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=db_export;Data Source=notebook'));
end;end.DLL:
library ADO;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;{$R *.res}function ado1(yunbian:string;udl:string):string;stdcall;
var
tql:TADOquery;
adoc:TADOConnection;
begin adoc:=TADOConnection.Create(application);
adoc.Close;
adoc.ConnectionString:=udl;
adoc.Open;
tql:=TADOquery.Create(application);
tql.Connection:=adoc;
tql.Close;
tql.SQL.Clear;
tql.SQL.Add('select * from t_consign');
tql.Open;
if not tql.Eof then
result:=tql.Fields[1].AsString; adoc.Free;
tql.Free;
end;
exports
ado1;
beginend.
当showmessage后,数据是显示出来了解,可是接着又出现 'Invalid Pointer operation' 请教!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
// function ado1(yunbian:string;udl:string):string;stdcall;
private { Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
function ado1(yunbian:string;udl:string):string;stdcall;external'ADO.dll';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(ado1('aa','Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=db_export;Data Source=notebook'));
end;end.DLL:
library ADO;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;{$R *.res}function ado1(yunbian:string;udl:string):string;stdcall;
var
tql:TADOquery;
adoc:TADOConnection;
begin adoc:=TADOConnection.Create(application);
adoc.Close;
adoc.ConnectionString:=udl;
adoc.Open;
tql:=TADOquery.Create(application);
tql.Connection:=adoc;
tql.Close;
tql.SQL.Clear;
tql.SQL.Add('select * from t_consign');
tql.Open;
if not tql.Eof then
result:=tql.Fields[1].AsString; adoc.Free;
tql.Free;
end;
exports
ado1;
beginend.
当showmessage后,数据是显示出来了解,可是接着又出现 'Invalid Pointer operation' 请教!
参数改为pchar申明试试function ado1(yunbian:pchar;udl:pchar):pchar;stdcall;external'ADO.dll';
改为如下方式声明试试
function ado1(yunbian:pchar;udl:pchar):pchar;stdcall;
还是不行
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
谢谢你,在dll中加了一个sharemem,在工程中也加了一个,都是在第一个位置,
当单击时没有错误可是当我关闭窗体时,还是出现这个错误'Invalid Pointer operation'
并且又附加一个错误:Runtime error 217 at 0041bb60
var
tql:TADOquery;
adoc:TADOConnection;
begin coInitialize(nil); //加上
adoc:=TADOConnection.Create(application);
adoc.Close;
adoc.ConnectionString:=udl;
adoc.Open;
tql:=TADOquery.Create(application);
tql.Connection:=adoc;
tql.Close;
tql.SQL.Clear;
tql.SQL.Add('select * from t_consign');
tql.Open;
if not tql.Eof then
result:=tql.Fields[1].AsString; adoc.Free;
tql.Free;
CoUninitialize; //加上
end;
exports
ado1;
beginend.
或在exe中把Application.handle传入
在Dll里
application.handle := 你传入的handle;
里面就可以用.Create(application);