DLL里面使用数据库问题 (80分)

  • 主题发起人 主题发起人 Jack Ye
  • 开始时间 开始时间
J

Jack Ye

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好,我是新来的,请多多赐教

我的程序是一个主窗口调用其他dll文件里面的窗口。

而DLL窗口用来实现 ado+access 数据库操作 DLL窗口中有DBGrid控件
但在 DELPHI IDE 中写程序的时候连接上了,DBGrid控件可以见到数据库内容
但编译后运行程序就见不到数据内容

请问各位大侠究竟如何才能在dll的窗口中用ado控件连接上数据库?


MainForm 清单如下:
=================================================
procedure TMainForm.BitBtn1Click(Sender: TObject);
type
Tin_frm=procedure (sender:TObject);
var
dllinstance:THandle;
PFunc:TFarProc;

begin
dllinstance:=loadlibrary('Incom.dll');
if dllinstance=0 then
begin
showmessage('找不到文件 ''Incom.DLL'' ');
exit;
end;
PFunc:=getprocaddress(dllinstance,'RunIncom');
if PFunc<>nil then
Tin_frm(PFunc)(MainForm)
else
showmessage('找不到输出函数');
freelibrary(dllinstance);
end;
===================================================

incom.dll 清单如下:
===================================================
uses
SysUtils,
Classes,
Forms,
in_form in 'in_form.pas' {in_frm},

{$R *.res}

function RunIncom:integer;stdcall;
var
in_frm:Tin_frm;
begin
in_frm:=Tin_frm.Create(Application);
Result:=in_frm.ShowModal;
in_frm.Free;
end;

exports
RunIncom;
====================================================

如果直接在 dll的 in_frm窗口中直接加入ado控件会出现
"Exception EOleSysError in module Incomm.dll at 0006F0F5"
的错误。


如果用datamodule组件,并在in_frm窗口的OnCreate事件中加入:
=====================================================
implementation
uses datamodule;
{$R *.dfm}

procedure Tin_frm.FormCreate(Sender: TObject);
var
datamodule1:Tdatamodule1;
begin
datamodule1:=Tdatamodule1.Create(application);
end;
=====================================================
则出现“尚未调用 Coinitialize” 的错误。

 
这是我以前写的日记,看看吧:)

今天在制作包含模式窗体的DLL时遇到了几个问题。

1、在模式窗体中包含Tadoconnection控件后调用时会提示“标记没有引用存储”,经bluerain讲解原来需要加入
CoInitialize(nil);
CoUninitialize;
原因是tadoconnection是com控件,com控件在初始化时需要调用CoInitialize。


library SQLExp;

{ 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, SysUtils, Classes, Forms, ActiveX,
SLinkU in 'SLinkU.pas' {SLink};

{$R *.res}

procedure GetSQLLnk(AppHandle: THandle; SqlStr: PChar; DataBase: ShortString;
User: ShortString = ''; Pasw: ShortString = ''); stdcall;
var
SLink: TSLink;
OldHandle: THandle;
begin
CoInitialize(nil);<--
Application.Initialize;

OldHandle := Application.Handle;
Application.Handle := AppHandle;

SLink := TSLink.Create(Application);
StrPCopy(SqlStr, SLink.GetSQLLink(DataBase, User, Pasw));
SLink.Free;

Application.Handle := OldHandle;
CoUninitialize;<--
end;

exports
GetSQLLnk;

begin
end.
 
首先十分感谢 远帆 !
现在我在dllform的oncreate中加入CoInitialize(nil);在onclose里面加入CoUnInitialize
之后就可以在dllform的DBGrid显示数据库内容了,也可以通过DBGrid增加和修改数据,但
却不能删除,每次按删除按钮就会出现
Access violation at address 00DE61AB in module 'incom.dll' Read of address 00000058
的错误。这个按钮是我自己加的,不是DBNavigator,用DBNavigator的删除按钮就可以删除

我的按钮代码是这样的:
===================================================
if application.MessageBox('当前记录将被删除,而且无法恢复,是否继续?','确认',
mb_yesno+mb_defbutton2+mb_iconquestion+mb_applmodal)=idYes then

datamodule1.ADOTable1.Delete;
====================================================

请问这又如何解决呢?
 
不知道是什么问题,你自已先查查?
 
虽然还没解决,但还是将分数给出去了。

希望哪位大侠能帮我解决这个问题。。。。。。。。。。。。
 
将如下代码加入你的in_form.pas元中,保险些.若加入dll输出函数中,
有些时候不一定有效.

uses
...ActiveX,...

...
...
...

initialization
Coinitialize(nil);
finalization
CoUninitialize;
end.
 
后退
顶部