DLL与ADOConnection的问题(30分)

  • 主题发起人 主题发起人 wuzhonglinquan
  • 开始时间 开始时间
W

wuzhonglinquan

Unregistered / Unconfirmed
GUEST, unregistred user!
DLL与ADOConnection的问题
我自己做了有个测试,
在主程序里有个MainADOConnection,并设置好其ConnectionStrin属性,置Connected:=true;
在DLL里面也有个ADOConn控件,我希望主程序中的MainADOConnection传递到DLL中,并ADOConn:=MainADOConnection(我是认为是这样可以,不知道这样做行不行?)
主程序代码:
procedure TForm1.Button1Click(Sender: TObject);
var
hdll:THandle;
ShowMdll:TShowMdll;
begin
hdll:=LoadLibrary('pp.dll');
if hdll<>0 then
begin
@ShowMdll:=GetProcAddress(hdll,'ShowTLDForm');
if @ShowMdll<>nil then
ShowMdll(handle,DMform.ADOConnection1);
end;
end;

DLL代码:
library pp;
uses
Forms,ADODB,Dialogs,
UnitTLDForm in 'UnitTLDForm.pas' {TLDForm};
{$R *.res}
function ShowTLDForm(handle:THandle;AConn:TADOConnection):integer;stdcall;
var
TLDForm: TTLDForm;
begin
Application.Handle:=handle;
try
TLDForm:=TTLDForm.Create(Application);
Showmessage(TLDForm.ADOConnection1.ConnectionString);
result:=TLDForm.ShowModal;
TLDForm.ADOConnection1:=AConn;
TLDForm.ADOConnection1.Connected:=true;
finally
TLDForm.Free;
end;
end;

exports
showTLDForm;
begin

end.

测试的结果是弹出错误[ODBC数据源未连接],希望谁能指点一下??
 
DMform.ADOConnection1 你有没设置哎
就不能把源码全帖出来麽??直接考下来才好调试发现问题哎。。
我自己写了个是可以的你自己看看
dll的
library Project1;
uses
ADODB,
Dialogs,
SysUtils,
Classes;
{$R *.res}
function ShowTLDForm(AConn:TADOConnection):integer;stdcall;
begin
try
AConn.Connected := True;
except
ShowMessage('error');
end;
ShowMessage('access');
end;

exports
showTLDForm;
begin
end.

调用的
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;
type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
con1: TADOConnection;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function ShowTLDForm(AConn:TADOConnection):integer;stdcall;external 'Project1.dll';
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
begin
ShowTLDForm(con1)
end;

end.
 
设置断点,看看TLDForm.ADOConnection1的值是不是你设置的就知道了嘛
 
可传 ADO的指针作参数
主程序代码:
procedure TForm1.Button1Click(Sender: TObject);
var
hdll:THandle;
ShowMdll:TShowMdll;
begin
hdll:=LoadLibrary('pp.dll');
if hdll<>0 then
begin
@ShowMdll:=GetProcAddress(hdll,'ShowTLDForm');
if @ShowMdll<>nil then
ShowMdll(handle, dword( DMform.ADOConnection1));
//转成指针
end;
end;

DLL代码:
library pp;
uses
Forms,ADODB,Dialogs,
UnitTLDForm in 'UnitTLDForm.pas' {TLDForm};
{$R *.res}
function ShowTLDForm(handle:THandle;AConn:dword ):integer;stdcall;
// dword
var
TLDForm: TTLDForm;
tmpConn : TADOConnection ;
//用它作临时的
begin
Application.Handle:=handle;
try
TLDForm:=TTLDForm.Create(Application);
Showmessage(TLDForm.ADOConnection1.ConnectionString);
result:=TLDForm.ShowModal;
tmpConn := TADOConnection(AConn) ;
//转成ADOConnection ;
//TLDForm.ADOConnection1:=AConn;
改成如下
Form.ADOConnection1.ConnectionObject = tmpConn.ConnectionObject ;
TLDForm.ADOConnection1.Connected:=true;
finally
TLDForm.Free;
end;
end;

exports
showTLDForm;
begin

end.
 
TLDForm:=TTLDForm.Create(Application);
tmpConn := TADOConnection(AConn) ;
//转成ADOConnection ;
//TLDForm.ADOConnection1:=AConn;
改成如下
Form.ADOConnection1.ConnectionObject = tmpConn.ConnectionObject ;
TLDForm.ADOConnection1.Connected:=true;
Showmessage(TLDForm.ADOConnection1.ConnectionString);
result:=TLDForm.ShowModal;
 
@jettop and abraveboy
主程序:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ADODB, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TShowMdll=function (AHandle : THandle;Acon:Dword):integer;stdcall;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
hdll:THandle;
ShowMdll:TShowMdll;
begin
hdll:=LoadLibrary('pp.dll');
if hdll<>0 then
begin
@ShowMdll:=GetProcAddress(hdll,'ShowTLDForm');
if @ShowMdll<>nil then
ShowMdll(handle,DWord(DMform.ADOConnection1));
end;
end;

end.

Dll程序:
library pp;
uses
Forms,ADODB,Dialogs,Types,
UnitTLDForm in 'UnitTLDForm.pas' {TLDForm};
{$R *.res}
function ShowTLDForm(handle:THandle;AConn:DWord):integer;stdcall;
var
TLDForm: TTLDForm;
tmpConn : TADOConnection;
begin
Application.Handle:=handle;
try
TLDForm:=TTLDForm.Create(Application);
tmpConn := TADOConnection(AConn) ;
TLDForm.ADOConnection1:=tmpConn;
TLDForm.ADOConnection1.ConnectionObject:=tmpConn.ConnectionObject;
TLDForm.ADOConnection1.Connected:=true;
Showmessage(TLDForm.ADOConnection1.ConnectionString);//这句执行通过
TLDForm.ADOQuery1.Open;
//但是执行到这句的时候就抱错:[ODBC 驱动程序]没有找到数据源.用我以前的方法,也是报这样的错误。
result:=TLDForm.ShowModal;
finally
TLDForm.Free;
end;
end;

exports
showTLDForm;
begin

end.
 
可否把TLDForm的Dfm代码贴出来看看。。。
 
后退
顶部