我的代码如下:
library dllado;{ 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
SysUtils,
Classes,
Forms,
Windows,
Activex,
DB,
ADODB,
datamodule in 'datamodule.pas' {DLLDM: TDataModule},
functions in 'functions.pas',
varibles in 'varibles.pas';{$R *.RES}var
DllApplication: TApplication;procedure DLLUnloadProc(Reason: Integer); register;
begin
try
if Reason = DLL_PROCESS_DETACH then
begin
if Assigned(DLLDM) then
begin
DLLDM.Free;
end;
CoUnInitialize;
Application:=DllApplication;
end;
if Reason = DLL_PROCESS_ATTACH then
begin
DllApplication:=Application;
CoInitialize(Nil);
if DLLDM=nil then
begin
DLLDM:=TDLLDM.Create(nil);
end;
end;
except
end;
end;exports
GetMessage;
begin
DLLProc := @DLLUnloadProc;
DLLUnloadProc(DLL_PROCESS_ATTACH);
end.
unit datamodule;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ADODB;type
TDLLDM = class(TDataModule)
conn: TADOConnection;
ads: TADODataSet;
private
{ Private declarations }
public
{ Public declarations }
end;var
DLLDM: TDLLDM;implementation{$R *.DFM}end.
unit functions;interfaceuses Windows,Forms,DB,ADODB;function ConnectToDataBase(vStr:String):Boolean;
function GetMessage(PApp:TApplication;connectionstring:String):String; export; stdcall;implementationuses DataModule,varibles;function GetMessage(PApp:TApplication;connectionstring:String):String; export; stdcall;
begin
Application:=PApp;
if Not ConnectToDataBase(connectionstring) then
begin
Result:='can not connect to database!';
end
else begin
with DLLDM.ads do
begin
Close;
CommandText:='select count(*) from tb_yhda';
OPen;
Result:=Fields[0].AsString;
Close;
end;
end;
end;function ConnectToDataBase(vStr:String):Boolean;
begin
try
if Connstr<>vstr then
begin
connstr:=vStr;
DLLDM.conn.Close;
DllDM.conn.ConnectionString :=connstr;
DLLDM.conn.Open;
end;
Result:=True;
except
Result:=False;
end;
end;end.
调用GetMessage时结果能出来,但报Invalid Pointer Operation错误,麻烦帮我看看
library dllado;{ 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
SysUtils,
Classes,
Forms,
Windows,
Activex,
DB,
ADODB,
datamodule in 'datamodule.pas' {DLLDM: TDataModule},
functions in 'functions.pas',
varibles in 'varibles.pas';{$R *.RES}var
DllApplication: TApplication;procedure DLLUnloadProc(Reason: Integer); register;
begin
try
if Reason = DLL_PROCESS_DETACH then
begin
if Assigned(DLLDM) then
begin
DLLDM.Free;
end;
CoUnInitialize;
Application:=DllApplication;
end;
if Reason = DLL_PROCESS_ATTACH then
begin
DllApplication:=Application;
CoInitialize(Nil);
if DLLDM=nil then
begin
DLLDM:=TDLLDM.Create(nil);
end;
end;
except
end;
end;exports
GetMessage;
begin
DLLProc := @DLLUnloadProc;
DLLUnloadProc(DLL_PROCESS_ATTACH);
end.
unit datamodule;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ADODB;type
TDLLDM = class(TDataModule)
conn: TADOConnection;
ads: TADODataSet;
private
{ Private declarations }
public
{ Public declarations }
end;var
DLLDM: TDLLDM;implementation{$R *.DFM}end.
unit functions;interfaceuses Windows,Forms,DB,ADODB;function ConnectToDataBase(vStr:String):Boolean;
function GetMessage(PApp:TApplication;connectionstring:String):String; export; stdcall;implementationuses DataModule,varibles;function GetMessage(PApp:TApplication;connectionstring:String):String; export; stdcall;
begin
Application:=PApp;
if Not ConnectToDataBase(connectionstring) then
begin
Result:='can not connect to database!';
end
else begin
with DLLDM.ads do
begin
Close;
CommandText:='select count(*) from tb_yhda';
OPen;
Result:=Fields[0].AsString;
Close;
end;
end;
end;function ConnectToDataBase(vStr:String):Boolean;
begin
try
if Connstr<>vstr then
begin
connstr:=vStr;
DLLDM.conn.Close;
DllDM.conn.ConnectionString :=connstr;
DLLDM.conn.Open;
end;
Result:=True;
except
Result:=False;
end;
end;end.
调用GetMessage时结果能出来,但报Invalid Pointer Operation错误,麻烦帮我看看
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;function GetMessage(PApp:TApplication;connectionstring:String):String; stdcall; external 'dllado.dll' implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
constr:String;
begin
constr:='连接串';
showmessage(getmessage(application,constr));
end;end.