Hmm good; will try it with threads and see... Jangita. http://www.jangita.com [EMAIL PROTECTED]
-----Original Message----- From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of Katja Bergman Sent: Monday, August 15, 2005 6:16 PM To: [email protected] Subject: [delphi-en] Re: DLLs and Threads It may or may not be useful to you, but when I am dealing with DLL's, I use this simple unit that I've written once: unit untDLL; interface uses Windows; type IDLL = interface(IUnknown) ['{D1CB2BE4-7183-41EF-852F-8B9AE377444C}'] function GetProcAddress(ProcName: string): FARPROC; function Handle: THandle; function Name: string; end; function NewDLL(const Filename: string): IDLL; implementation uses SysUtils; type TDLL = class(TInterfacedObject, IDLL) private FHandle: THandle; protected // IDLL function GetProcAddress(ProcName: string): FARPROC; function Handle: THandle; function Name: string; public constructor Create(const Filename: string); destructor Destroy; override; end; function NewDLL(const Filename: string): IDLL; begin Result := TDLL.Create(Filename); end; constructor TDLL.Create(const Filename: string); begin inherited Create; FHandle := LoadLibrary(PChar(Filename)); if (FHandle = 0) then raise Exception.Create(SysErrorMessage(GetLastError)); end; destructor TDLL.Destroy; begin try if (FHandle <> 0) then FreeLibrary(FHandle); finally inherited; end; end; function TDLL.GetProcAddress(ProcName: string): FARPROC; begin Result := Windows.GetProcAddress(FHandle, PChar(ProcName)); if (Result = nil) then raise Exception.Create(SysErrorMessage(GetLastError)); end; function TDLL.Handle: THandle; begin Result := FHandle; end; function TDLL.Name: string; begin Result := GetModuleName(FHandle); end; end. (Removed blank lines to make this email smaller.) Now, the usage is very simple. I just declare a variable (DLL) of type IDLL and then use: DLL := NewDLL('Somefile.dll'); This either results in an exception or it will work. I can then call DLL.GetProcAddress('Method') with just the name of the procedure or function that I need. The advantage of this interface is that when I assign nil or another NewDLL object to the DLL variable, then it will unload the DLL automatically. And of course, if the DLL variable is a local variable then it will also unload the DLL when the variable goes out of scope. (In other words, the Delphi Garbage collection will clean it.) So far I haven't had any problems working with DLL's this way. Now the trick is, of course, to avoid having to reread those methods every time. So basically, after loading the DLL you should just get all the addresses of procedures that you want to call and store them. And maybe, if you like, you could add a special event to this interface that will clear all those procedure variables for you when your DLL is released. One other advantage is that you can pass this DLL object to any other function or procedure without having to worry about when you have to release it. The reference counter in the object will release it once the last reference to the object is nil'ed. In other words, this is a good way to keep a DLL in memory for as long as you need it, and not much longer... With kind regards, X Katja Bergman. --- In [email protected], "Chris @ IT" <[EMAIL PROTECTED]> wrote: > Hi All, > > I have this application that loads DLL's dynamically as plugins and as such > I use the loadlibrary, getprocaddress, free library routines. > > The host application loads the dll and calls its process export function so > that the dll can do what its meant to do. The problem is there are usually > many requests to be done (about 500 every second) and when I call the dll > for each sequentially its takes quite a bit of time > > What I want to try is spawn a new thread that loads the dll and calls the > function. With this approach the dll will be loaded many times and its > function called many times. > > Anything I should know about calling dlls in this manner? All dlls and apps > written in Delphi 7 > > Chris. ----------------------------------------------------- Home page: http://groups.yahoo.com/group/delphi-en/ To unsubscribe: [EMAIL PROTECTED] Yahoo! Groups Links ------------------------ Yahoo! Groups Sponsor --------------------~--> <font face=arial size=-1><a href="http://us.ard.yahoo.com/SIG=12h91chnb/M=362131.6882499.7825260.1510227/D=groups/S=1705115362:TM/Y=YAHOO/EXP=1124126907/A=2889191/R=0/SIG=10r90krvo/*http://www.thebeehive.org ">Get Bzzzy! (real tools to help you find a job) Welcome to the Sweet Life - brought to you by One Economy</a>.</font> --------------------------------------------------------------------~-> ----------------------------------------------------- Home page: http://groups.yahoo.com/group/delphi-en/ To unsubscribe: [EMAIL PROTECTED] Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/delphi-en/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/

