关于DLL的问题.(100分)

  • 主题发起人 主题发起人 hying95
  • 开始时间 开始时间
H

hying95

Unregistered / Unconfirmed
GUEST, unregistred user!
在DLL中我想动态地创建一个Timer控件,然后给它加一个事件,比如让它每5分钟给一按纽发一鼠标按键消息.请大家看怎样实现?
 
这还不简单啊。 &nbsp;有很多做法。<br>1, 你只在工程文件中引用相关单元,在载入阶段创建它, 并赋给它事件处理函数。<br>结束时释放就可以了。<br>2,也可以在dll中加入窗体,或者其他单元控制。
 
直接用 SetTimer 得了,还搞什么定时器控件啊。
 
这是我的DLL文件.<br>library Timerdll;<br>uses<br> &nbsp;SysUtils,<br> &nbsp;ExtCtrls,<br> &nbsp;Classes,<br> &nbsp;Windows, <br> &nbsp;Messages;<br><br>{$R *.res}<br>var<br> &nbsp;t: ttimer;<br> &nbsp;<br>procedure FileCopyFile(const sfromFile, sToFile: string);<br>begin<br> &nbsp;if AnsiCompareFileName(sfromFile, sToFile) &lt;&gt; 0 then<br> &nbsp; &nbsp;CopyFile(PChar(sfromFile), PChar(sToFile), False);<br>end;<br><br>begin<br> &nbsp;t:=Ttimer.Create();//这里的参数怎么填,是不是还得创建一个控件来释放它?<br>end.<br>楼上的朋友说setTimer怎么弄?
 
library Timerdll;<br>uses<br> &nbsp;SysUtils,<br> &nbsp;ExtCtrls,<br> &nbsp;Classes,<br> &nbsp;Windows,<br> &nbsp;UBackup in '../设置定时备份/UBackup.pas' {FrmBackup};<br><br>{$R *.res}<br>var<br> &nbsp;TID: Cardinal;<br> &nbsp;<br>procedure FileCopyFile(const sfromFile, sToFile: string);<br>begin<br> &nbsp;if AnsiCompareFileName(sfromFile, sToFile) &lt;&gt; 0 then<br> &nbsp; &nbsp;CopyFile(PChar(sfromFile), PChar(sToFile), False);<br>end;<br><br>Procedure SnTimer;<br>begin<br> &nbsp;FileCopyFile('e:/gms/user/common/pattern/0.000','f:/gms/user/common/pattern/0.000');<br> &nbsp;KillTimer(0,TID);//如果不在这里释放,在其它地方如何释放?<br>End;<br><br>Exports<br>ShowBackup;<br><br>begin<br> &nbsp;TID:=SetTimer(0,0,0,@SnTimer);<br>end.
 
我这个DLL准备给一个没有源码,不能更新的程序加一个自动保存功能,我已把它加入到程序中,并在原程序里添加了事件处理代码,当执行我的DLL中的窗口时窗口中的定时器能把程序中取得焦点的文件备份到另一地方,窗体是模态的,但关掉窗口后,定时器也关了,我这个DLL是静态调用,我想在加载DLL时,就打开定时器,让它备份文件.<br><br>请大家帮帮我?
 
没人回答,自己顶下!
 
去查查以下四个关键字,你就会明白了。<br> &nbsp; &nbsp;DLL_PROCESS_ATTACH:<br> &nbsp; &nbsp;DLL_PROCESS_DETACH:<br> &nbsp; &nbsp;DLL_THREAD_ATTACH:<br> &nbsp; &nbsp;DLL_THREAD_DETACH:
 
你动态创建一个timer ,然后在定义一个实践的处理过程,然后把timer的事件关联到你定义的处理过程,就可以了,如果实在不行的话,你可以在dll总定义form,然后在form中加timer 和相关的处理过程
 
我才通过编译的DLL<br>library _NewSize;<br><br>uses<br> &nbsp;SysUtils,<br> &nbsp;Windows,<br> &nbsp;Dialogs,<br> &nbsp;inifiles,<br> &nbsp;Classes,<br> &nbsp;UBackup in '../设置定时备份/UBackup.pas' {FrmBackup};<br><br>{$R *.res}<br>var<br> &nbsp;ini,Pini: tinifile;<br> &nbsp;TimerBackup, TimerSave, TimerOn: Cardinal; //定时器变量<br> &nbsp;H: hWnd; //PDS窗口句柄<br> &nbsp;TBackup,TSave: Integer;<br> &nbsp;//BHnd: Hwnd;<br> &nbsp;S,P,BackupPath,filename,sDir,aCaption: string; //文件路径<br> &nbsp;SaveExit: Pointer;<br> &nbsp;StartSave: Bool=False;<br> &nbsp;StartBackup: Bool=False;<br> &nbsp;SaveBool: Bool=False;<br> &nbsp;BackupBool: Bool=false;<br><br>procedure LibExit;<br>begin<br> &nbsp;// 库退出时的处理<br> &nbsp;KillTimer(0,TimerBackup);<br> &nbsp;KillTimer(0,TimerOn);<br> &nbsp;ExitProc := SaveExit; &nbsp;// 恢复退出链<br>end;<br><br>Function MCopyFile(var sourcefile:string; targetfile:string):boolean;<br>var<br> &nbsp;s,t:Tfilestream;<br>begin<br> &nbsp;MCopyFile:=True;<br> &nbsp;s:=Tfilestream.Create(sourcefile,fmopenread);<br> &nbsp;try<br> &nbsp; &nbsp;Try<br> &nbsp; &nbsp; &nbsp;t:=Tfilestream.Create(Targetfile,fmopenwrite or fmcreate);<br> &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp;t.CopyFrom(s,s.size);<br> &nbsp; &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp; &nbsp;t.free;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;s.free;<br> &nbsp; &nbsp;end;<br> &nbsp;except<br> &nbsp; &nbsp;MCopyFile:=False;<br> &nbsp;end;<br>end;<br><br>//提取字符串中指定子字符串后的字符串<br>function After(Str,S:string): string ;<br>var<br> f: Word;<br>begin<br> &nbsp;f:=pos(s,str);<br> &nbsp;if F=0 then<br> &nbsp;After:=''<br> &nbsp;else<br> &nbsp;After:=COPY(Str,F+1,length(str)-length(s));<br>end;<br><br>procedure Call_PicShow();stdcall;<br>Var<br> &nbsp;dllName,FuncName:string;<br> &nbsp;LibHandle:HWND;<br> &nbsp;DName:Function():bool;stdcall;<br>begin<br> &nbsp;dllName:='_NewRule.dll';<br> &nbsp;FuncName:='PicShow';<br> &nbsp;LibHandle:=LoadLibrary(pchar(dllName));<br> &nbsp;if LibHandle&lt;32 then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,pchar('Not Found'+ dllName),'Error',0);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;@DName:=GetProcAddress(LibHandle,pchar(FuncName));<br> &nbsp;if @DName=NIL then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,pchar('Not Found '+FuncName+' in'+ dllName),'Error',0);<br> &nbsp; &nbsp;FreeLibrary(LibHandle);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;try<br> &nbsp; &nbsp;DName();<br> &nbsp;finally<br> &nbsp; &nbsp;FreeLibrary(LibHandle);<br> &nbsp;end;<br>end;<br><br>procedure Call_NewSize();stdcall;<br>Var<br> &nbsp;dllName,FuncName: string;<br> &nbsp;LibHandle:HWND;<br> &nbsp;DName:Function:bool;stdcall;<br>begin<br> &nbsp;dllName:='_NewRule.dll';<br> &nbsp;FuncName:='NewSize';<br> &nbsp;LibHandle:=LoadLibrary(pchar(dllName));<br> &nbsp;if LibHandle&lt;32 then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,pchar('Not Found'+ DllName),'Error',0);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;@DName:=GetProcAddress(LibHandle,pchar(FuncName));<br> &nbsp;if @DName=NIL then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,pchar('Not Found '+FuncName+' in '+ DllName),'Error',0);<br> &nbsp; &nbsp;FreeLibrary(LibHandle);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;try<br> &nbsp; &nbsp;DName;<br> &nbsp;finally<br> &nbsp; &nbsp;FreeLibrary(LibHandle);<br> &nbsp;end;<br>end;<br><br>procedure Call_PicSearch();stdcall;<br>Var<br> &nbsp;dllName,FuncName: string;<br> &nbsp;LibHandle:HWND;<br> &nbsp;DName:Function:bool;stdcall;<br>begin<br> &nbsp;dllName:='_NewRule.dll';<br> &nbsp;FuncName:='PicSearch';<br> &nbsp;LibHandle:=LoadLibrary(pchar(dllName));<br> &nbsp;if LibHandle&lt;32 then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,pchar('Not Found'+ DllName),'Error',0);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;@DName:=GetProcAddress(LibHandle,pchar(FuncName));<br> &nbsp;if @DName=NIL then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,pchar('Not Found '+FuncName+' in '+ DllName),'Error',0);<br> &nbsp; &nbsp;FreeLibrary(LibHandle);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;try<br> &nbsp; &nbsp;DName;<br> &nbsp;finally<br> &nbsp; &nbsp;FreeLibrary(LibHandle);<br> &nbsp;end;<br>end;<br><br>procedure Call_TajimaDGML();stdcall;<br>var<br> &nbsp;s: string;<br>begin<br> &nbsp;s:=ini.ReadString('Programs','TajimaDGML','E:/DGMLWin/bin/DG.exe');<br> &nbsp;WinExec(Pchar(s),5);<br>end;<br><br>procedure Call_BiaoZhunSize();StdCall;<br>Var<br> &nbsp;dllName,FuncName: string;<br> &nbsp;LibHandle:HWND;<br> &nbsp;DName:Function:bool;stdcall;<br>begin<br> &nbsp;dllName:='_JJGSize32.dll';<br> &nbsp;FuncName:='BiaoZhunCC';<br> &nbsp;LibHandle:=LoadLibrary(pchar(dllName));<br> &nbsp;if LibHandle&lt;32 then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,pchar('Not Found'+ DllName),'Error',0);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;@DName:=GetProcAddress(LibHandle,pchar(FuncName));<br> &nbsp;if @DName=NIL then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,pchar('Not Found '+FuncName+' in '+ DllName),'Error',0);<br> &nbsp; &nbsp;FreeLibrary(LibHandle);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;try<br> &nbsp; &nbsp;DName;<br> &nbsp;finally<br> &nbsp; &nbsp;FreeLibrary(LibHandle);<br> &nbsp;end;<br>end;<br><br>procedure CallDll_BZCC();stdcall;<br>Var<br> &nbsp;dllName,FuncName: string;<br> &nbsp;LibHandle,fwnd:HWND;<br> &nbsp;DName:Function(AHandle:THandle):bool;stdcall;<br>begin<br> &nbsp;dllName:='_JJGSize32.dll';<br> &nbsp;FuncName:='ShowFrom';<br> &nbsp;fwnd:=findwindow('TMDIForm',nil);<br> &nbsp;LibHandle:=LoadLibrary(pchar(dllName));<br> &nbsp;if LibHandle&lt;32 then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,pchar('Not Found'+ dllName),'Error',0);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;@DName:=GetProcAddress(LibHandle,pchar(FuncName));<br> &nbsp;if @DName=NIL then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,pchar('Not Found '+FuncName+' in'+ dllName),'Error',0);<br> &nbsp; &nbsp;FreeLibrary(LibHandle);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;try<br> &nbsp; &nbsp;if fwnd&lt;&gt;0 then<br> &nbsp; &nbsp;DName(fwnd);<br> &nbsp;finally<br> &nbsp; &nbsp;FreeLibrary(LibHandle);<br> &nbsp;end;<br>end;<br><br>procedure CallDll_E();Stdcall;<br>Var<br> &nbsp;dllName,FuncName: string;<br> &nbsp;LibHandle:HWND;<br> &nbsp;DName:Function:bool;stdcall;<br>begin<br> &nbsp;dllName:='_NewRule.dll';<br> &nbsp;FuncName:='ShowNarrate';<br> &nbsp;LibHandle:=LoadLibrary(pchar(dllName));<br> &nbsp;if LibHandle&lt;32 then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,pchar('Not Found'+ DllName),'Error',0);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;@DName:=GetProcAddress(LibHandle,pchar(FuncName));<br> &nbsp;if @DName=NIL then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(0,pchar('Not Found '+FuncName+' in '+ DllName),'Error',0);<br> &nbsp; &nbsp;FreeLibrary(LibHandle);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;try<br> &nbsp; &nbsp;DName;<br> &nbsp;finally<br> &nbsp; &nbsp;FreeLibrary(LibHandle);<br> &nbsp;end;<br>end;<br><br>procedure CallDll_F();stdcall;<br>begin<br> &nbsp;ShowBackup;<br>end;<br><br>procedure Call_Capture();stdcall;<br>var<br> &nbsp;s:string;<br>begin<br> &nbsp;s:=ini.ReadString('Programs','Capture','');<br> &nbsp;s:=after(s,',');<br> &nbsp;WinExec(pchar(s),5);<br>end;<br><br>procedure Call_Order();stdcall;<br>var<br> &nbsp;s:string;<br>begin<br> &nbsp;s:=ini.ReadString('Programs','Order','');<br> &nbsp;s:=after(s,',');<br> &nbsp;WinExec(pchar(s),5);<br>end;<br><br>procedure Call_Layout();stdcall;<br>var<br> &nbsp;s:string;<br>begin<br> &nbsp;s:=ini.ReadString('Programs','LayoutMarker','');<br> &nbsp;s:=after(s,',');<br> &nbsp;WinExec(pchar(s),5);<br>end;<br><br>procedure Call_Plotter();stdcall;<br>var<br> &nbsp;s:string;<br>begin<br> &nbsp;s:=ini.ReadString('Programs','OutputManager','');<br> &nbsp;s:=after(s,',');<br> &nbsp;WinExec(pchar(s),5);<br>end;<br><br>procedure pTest;<br>Begin<br> &nbsp;StartBackup:=Pini.ReadBool('Program','chkBackup',False);<br> &nbsp;StartSave:=Pini.ReadBool('Program','chkSave',False);<br> &nbsp;if StartBackup=true then<br> &nbsp;begin<br> &nbsp; &nbsp;h:=findwindow('TMDIForm',nil);<br> &nbsp; &nbsp;if h&lt;&gt;0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;aCaption:=EIGetWinText(GetFocusedChildWindow(h));<br> &nbsp; &nbsp; &nbsp;filename:=after(aCaption,'&gt;');<br> &nbsp; &nbsp; &nbsp;sDir:=before(aCaption,'-');<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;if pos('&gt;',aCaption)&lt;&gt;0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;P:=BackupPath+'/'+sDir+'/Pattern';<br> &nbsp; &nbsp; &nbsp;if not DirectoryExists(P) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if ForceDirectories(P) then &nbsp;//创建多级目录<br> &nbsp; &nbsp; &nbsp; &nbsp;forceDirectories(P);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;BackupBool:=true;<br> &nbsp; &nbsp; &nbsp;if StartSave=true then<br> &nbsp; &nbsp; &nbsp;SaveBool:=true;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>End;<br><br>Procedure TBackupOn;<br>Begin<br> &nbsp;if BackupBool=true then<br> &nbsp;begin<br> &nbsp; &nbsp;s:=SJDir(ExpandFileName('_NewSize.dll'),2)+'/User/'+sDir+'/Pattern/'+filename;<br> &nbsp; &nbsp;//执行代码;<br> &nbsp; &nbsp;FileCopyFile(S,BackupPath+'/'+sDir+'/Pattern/'+filename);<br> &nbsp;end;<br>End;<br><br>exports<br>CallDll_BZCC,<br>Call_PicShow,<br>Call_NewSize,<br>Call_PicSearch,<br>Call_BiaoZhunSize,<br>CallDll_E,<br>CallDll_F,<br>Call_Capture,<br>Call_Order,<br>Call_Layout,<br>Call_TajimaDGML,<br>Call_Plotter;<br><br>begin<br> &nbsp;//... &nbsp;// 库加载处理<br> &nbsp;ini:=tinifile.Create(ExtractFilePath(Paramstr(0))+'Eastmark.ini');<br> &nbsp;Pini:=tinifile.Create(ExtractFilePath(Paramstr(0))+'Pds.ini');<br> &nbsp;TBackup:=Pini.ReadInteger('Program','BackupTimes',50000);//备份定时器时间<br> &nbsp;BackupPath:=Pini.ReadString('Program','BackupPath','F:/Gms/User');<br> &nbsp;TSave:=Pini.ReadInteger('Program','SaveTimes',50000); //保存定时器时间<br> &nbsp;TimerBackup:=SetTimer(0,0,TBackup*10000,@TBackupOn);<br> &nbsp;TimerOn:=SetTimer(0,0,500,@pTest);<br> &nbsp;ExitProc := @LibExit; &nbsp;//装入退出过程<br> &nbsp;SaveExit := ExitProc; &nbsp;//保存退出链<br>end.
 
多人接受答案了。
 
后退
顶部