在DLL中使用ADO连接SQL SVR为何不行??高手请进(100分)

  • 主题发起人 主题发起人 xdf_hubei
  • 开始时间 开始时间
X

xdf_hubei

Unregistered / Unconfirmed
GUEST, unregistred user!
我以前做了一个DLL,用DATAMODULE+DATABASE+QUERY连接远程SQL SERVER,可以运行,我现在改成了ADO,未什么不行了呢?请高手指点[?]
 
我用ADO也不行,ADO假如为动态创建好像还可以。
 
function log_record(aHandle:THandle;logtype,username:string;sortno,eventno:integer):integer;stdcall;
var
strdatetime:string;
strcomputer:string;
computername:pchar;
size:Dword;
logsource:string;
SQLSVR:string;
SQLUser:string;
SQLPwd:string;
begin
SQLsvr:='200.200.200.1';
SQLUser:='chiuser';
SQLPwd:='dvb';
logSource:=Application.ExeName;
while pos('/',logSource)<>0 do
logSource:=copy(logSource,pos('/',logSource)+1,length(logSource));
logSource:=copy(logSource,1,pos('.',logSource)-1);
Application.Handle:=aHandle;
Application.CreateForm(TDataModule1, DataModule1);
strdatetime:=datetimetostr(now);
size:=255;
GetMem(computername,size);
if GetComputerName(computername,size)=false then
begin
MessageBox(aHandle,'获取计算机名称失败','错误',MB_OK+MB_ICONERROR);
FreeMem(Computername);
Result:=0;
exit;
end;
strcomputer:=computername;
DataModule1.Query1.Close;
DataModule1.Query1.SQL.Clear;
DataModule1.Query1.SQL.Add('insert into logfile(logtype,eventdatetime,logsource,username,computer,sortno,eventno)');
DataModule1.Query1.SQL.Add('values('''+logtype+''','''+strdatetime+''','''+logsource+''','''+username+''','''+strcomputer+''','+inttostr(sortno)+','+inttostr(eventno)+')');
DataModule1.Query1.ExecSQL;
DataModule1.Query1.Close;
DataModule1.Free;
Result:=1;
end;
exports
log_record index 1 name 'logrecord' resident;

begin
 
要进行类初始化,在你的datamodual单元的最后加入
Initialization
coInitialize(nil);
 
procedure TDataModule1.DataModuleCreate(Sender: TObject);
begin
AdoCnn.Open;
end;
ADOCNN是ADOCONNECTION控件
 
fjnnz,是加在TDataModule1.DataModuleCreate(Sender: TObject);
这里吗??
 
我没有加的时候出现“标记没有引用存储”的错误,为何??[?]
 
对象要初始化.
 
如何初始化?
Initialization
coInitialize(nil);
用在什么地方?而且我在D5里找不到coinitialize所需要引用的单元,怎么办?我在MSDN里
找到要引用objbase,但是D5不认?怎么办?能详细点吗?很着急啊~!
 
这次我没有用datamodule,改成了下面这样
function xdf1(StrTable:string):boolean;stdcall;
var
Adocnn:Tadoconnection;
Adoquery1:Tadoquery;
begin
Adocnn:=Tadoconnection.Create(nil);
Adoquery1:=Tadoquery.Create(nil);
Adocnn.ConnectionString:='Provider=SQLOLEDB.1;Password=dvb;Persist Security Info=True;User ID=chiuser;Initial Catalog=ChiDVB;Data Source=200.200.200.1';
AdoCnn.LoginPrompt:=false;
AdoCnn.open;
Adoquery1.Connection:=Adocnn;
Adoquery1.Close;
Adoquery1.SQL.Clear;
Adoquery1.SQL.Add('select * from '+StrTable);
Adoquery1.Open;
if Adoquery1.RecordCount>0 then
result:=true
else
result:=false;
end;
//export
exports
xdf1 index 1 name 'logrecord' resident;

begin
end.
当我调用logrecord的时候,还是出错:“标记没有引用存储”
改成这样都不行,到底要怎样啊,我服了DELPHI了~疯了[:(!][:(!][:(!]
 
上面的代码我在FORM里面试过了,没有问题,可是到了DLL里面就不行,如何解决,请老大帮忙了~![^][^]
 
coInitialize(nil);
在ActiveX里面。
同时不要忘了释放
 
我的笨办法:你换个能直接联结SQL SERVER的控件如SQL DIRECT或不就行了,
为什么一定要用ADO
还有一办法:既然DELPHI的ADO接口不好用,就换用Diamond ADO
 
我的一个例子,刚做的。给你参考。
如果你有DATAMODULE的话,估计要在它创建前使用coninitialize。
要不,跟我一样,直接创建ado控件吧。

library invoke;

{ 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
Classes,
SysUtils,
Windows,
AdoDB,
ActiveX,
Forms,
U_Common in 'U_Common.pas',
u_CallFunction in '//Lyt/ypgl/programs/u_CallFunction.pas',
U_Global in '//Lyt/Êý¾ÝÄ£¿é/U_Global.pas';

{$R *.res}

function InitInvokeDLL(pAppInfo :LPAppInfo): integer;
var
AdoQuery: TAdoquery;
begin
Result := INVOKE_SUCCESS;
if not Assigned(GNBHList) then //Èç¹û²»ÊǵÚÒ»´Îµ÷Óã¬ÔòµÈÓÚË¢ÐÂÒ»´ÎGNBHList;
GNBHList := TStringList.Create;

AppInfo:=TAppInfo(pAppInfo^);
Application.Handle := AppInfo.AppHandle;
CoInitialize(nil);

if not Assigned(AdoConnection) then
begin
AdoConnection := TAdoconnection.Create(Application);
AdoConnection.LoginPrompt := False;
AdoConnection.ConnectionString := AppInfo.ConnectionStr;
try
Adoconnection.Open;
except
FreeAndNil(AdoConnection);
FreeAndNil(GNBHList);
Result := INVOKE_CONNECTITODBFAILED;
end;
end;


Adoquery := TAdoquery.Create(Application);

with Adoquery do
begin
Connection := ADOConnection;
sql.Add('select gnbh from JBGNB order by gnbh');
try
try
open;
GNBHList.Clear;
while not eof do
begin
GNBHList.Add(Trim(FieldByName('gnbh').asstring));
Next;
end;
finally
Close;
Free;
CoUninitialize;
end;
except
Result := INVOKE_INITDLLEXCEPTION;
ClearMem;
end;
end;

//showmessage(gnbhlist.text);
end;

procedure ShutDownInvokeDLL;
begin
try
ClearMem;
except
end;
end;



exports
InitInvokeDLL index 0,
ShutDownInvokeDLL index 1,
InvokeFunction index 2,
GetNetComputerName index 30;


procedure DLLEntryPoint(dwReason: DWord);
begin
case dwReason of
DLL_PROCESS_ATTACH: ; {ShowMessage('Attaching to process');}
//if not InitDLL then ;
DLL_PROCESS_DETACH:
begin
ClearMem;
end;
DLL_THREAD_ATTACH: MessageBeep(0);
DLL_THREAD_DETACH: MessageBeep(0);
end;
end;

begin
{ First, assign the procedure to the DLLProc variable }
DllProc := @DLLEntryPoint;
{ Now invoke the procedure to reflect that the DLL is attaching to the process }

Appinfo.IsSup:=false;
Appinfo.CurrentUserName:='';
Appinfo.CurrentUnitFlag:='';

DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
 
你在Dll封装的form中使用了Ado控件。
在调用Dll的程序中加上一句
Application.Initialize;试一试!

procedure TForm1.Button1Click(Sender: TObject);
begin
Application.Initialize; //加上这句
hDll := LoadLibrary('project1.dll'); //在调用函数
if hDll <= 0 then exit;
@TestShowFrm := GetProcAddress(hDll,PChar('ShowMyFrm'));
if not(@TestShowFrm = nil) then
TestShowFrm(Application.Handle)
else
showmessage('failed');
FreeLibrary(hDll);
end;

http://www.delphibbs.com/delphibbs/dispq.asp?lid=1005844
 
多谢几个大哥的教导~!放分了,解决问题的办法贴在
http://www.delphibbs.com/delphibbs/DispQ.asp?LID=1052682
那里面的分也放了~![:D][:D][:D][:D][:D][:D][8D][8D]
 
后退
顶部