K
keyii
Unregistered / Unconfirmed
GUEST, unregistred user!
刚刚开始学习dll调用。请大家帮忙看看,为什么老提示错误。//dll代码library Synchronization;{ 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, ADODB, windows, StrUtils, Classes;{$R *.res}//取得服务器的时间function GetServerDateTime(ADOConn:TADOConnection)Char;stdcall;var ADOQ :TADOQuery;begin ADOQ := TADOQuery.Create(nil); with ADOQ do begin ADOQ.Connection := ADOConn; Close; Sql.Clear; Sql.Add('select GetDate() as FDate'); try open; Result := Pchar(formatdatetime('yyyymmddhhmmss',FieldValues['FDate'])); except Result := Pchar('00000000000000'); end; end;end;//同步到本机 0 错误 1 正确function Syn(SDateTimeChar):integer;stdcall;var sysTime: TSystemTime;begin Result := 1; with sysTime do begin wYear := StrToInt(LeftStr(SDateTime, 4)); wMonth := StrToInt(Copy(SDateTime, 5, 2)); wDay := StrToInt(Copy(SDateTime, 7, 2)); wHour := StrToInt(Copy(SDateTime, 9, 2)); wMinute := StrToInt(Copy(SDateTime, 12, 2)); wSecond := StrToInt(RightStr(SDateTime, 2)); wMilliseconds := 0; end; if not SetLocalTime(sysTime) then Result := 0;end;exports GetServerDateTime, Syn; beginend.//主程序procedure TMainForm.Edit1Click(Sender: TObject);type TIntFunc=function(ADOConn:TADOConnection)Char;stdcall; //function Syn(SDateTimeChar):integer;stdcall;var Th:Thandle; Tf:TIntFunc; Tp:TFarProc;begin Th:=LoadLibrary('Synchronization.dll'); {装载DLL} if Th>0 then try Tp:=GetProcAddress(Th,PChar('GetServerDateTime')); if Tp<>nil then begin Tf:=TIntFunc(Tp); Edit1.Text:=Tf(dm.ADOConnection1); {调用GetServerDateTime函数} end else ShowMessage('GetServerDateTime函数没有找到'); finally FreeLibrary(Th); {释放DLL} end else ShowMessage('Synchronization.dll没有找到');end;//目的很简单,就是在dll里面通过使用主程序的ADOconnection1来得到数据库服务器的时间,显示在edit1.text。