Link to home
Start Free TrialLog in
Avatar of geoffdb
geoffdb

asked on

Multiple Concurrent DLLs

I have a DLL which is called from an Office productivity app.   However, the initialisation data (provided by the caller) is unique to that instance.   This initial data is subsequently used for correct functioning of the DLL ....   At present it all works fine when I have just a single instance of the app & DLL active, but I want to progress to multiple documents ...

My question is, how do I handle multiple instances ?   I can have multiple instances of the app, and/or multiple document instances.   Can I tell the DLL to create a new instance each time (its not that big, so memory is not an issue), or should I create a new data structure for each instance with the unique data stored therein, or is there some other way ?

I would imagine this is a fairly common thing with MDI applications these days, but would like a 'heads up' before I start so I can go directly for the best solution.

Does anyone have any code snippets on this (preferably Delphi, but could convert VB or C code is necessary) ?   All help, references etc appreciated.

Thanks
Geoff
Avatar of gary_williams
gary_williams

Am I correct in assuming that your DLL is an "old style" DLL, i.e., not an ActiveX library?
Avatar of geoffdb

ASKER

Yes, correct.   In fact, its actually an XLL (a compiled equivalent of an Excel add-in), which is in effect a DLL with some additional special functions.

Geoff
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wim ten Brink
You can only load a DLL once per application. Not multiple times. Thus you can't create multiple instances of the DLL. But of whatever you do inside your DLL is up to you. You control if the objects in the DLL can be created only once or multiple times.

I'd advise to use a global variable to use for the DLL handle when you load it. Then you can share the DLL handle within all other parts of your application.
Avatar of geoffdb

ASKER

Alex & Slick812

Thanks for both your answers.   I understand better now.   I was hoping for some way to easily load a new DLL each time a new document was created (in the MDI instance), but it sounds like that is not possible.

Do you have, (or are able to point me to), some sample code of a simple example on this ?

Thanks
Geoff
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of geoffdb

ASKER

Slick812

That's great thanks, let me give it a whirl !!

Geoff
Avatar of geoffdb

ASKER

Slick812

I gave this a whirl today -it works a treat !!   Thanks.

I'm going to up the points for your good response, but have a couple of questions :

a) AttatchProc : Can you explain this to me please, and how it works, and where the cReason argument comes from - is this an automatic DLL argument ?      The function reference appears to be called only when the DLL is created, is it an implicit thing that it is called again on destroy with the DLL_Process_Detach argument ?

Alternatively, can one use create and destroy methods for the same thing ?

and secondly
b) Debugging DLLs : How do I interactively debug a DLL ?  
I was going to debug this to see how/where the AttatchProc func was called - then instantly realised that as it's a DLL can't do that with the IDE .. My experience is really only with executables, so using the IDE with them is fine ... my experience with DLLs is fairly new, have just used debug ShowMessage and/or wrteln type statements so far, but understand it is possible to debug interactively.

I'm using Delphi 6 BTW, though have 7 & 8 also.

Many thanks
Geoff
you should look in the Delphi help for "DLLProc" and it says -

   DLLProc is used to specify a procedure that is invoked every time a DLL's entry point is called, so on thread and process detatch it is called, , , Although in delphi this is not entirely accurate, you will need to ecplictly call your DLLProc with the  "DLL_PROCESS_ATTACH"  parameter to get that functioning, so that DLL_PROCESS_ATTACH is almost a usless kind of thing, you can just do any initialization code between the begin and end or call the DLLProc if you would rather - -



procedure AttatchProc(cReason: Cardinal);
var
i: Integer;
begin
if (cReason = DLL_PROCESS_ATTACH) then
  Storage := TList.Create;

if (cReason = Dll_Process_Detach) then
 begin
 for i := 0 to Storage.Count -1 do
   Dispose(Storage.Items[i]);
 FreeAndNil(Storage);
 end;
end;


begin
DLLProc := @AttatchProc;
AttatchProc(DLL_PROCESS_ATTACH);

end.


- - - - - - - - - - -  - - - - -  - - - - - - -  --

I do not usually Debug a DLL, except with a messageBox or some file write, however, as I understand it you can assign a program to run when you press the green "Run" arrow, and the program can run in the Debugger, allowing you to catch code misconduct, , but I have forgotten the steps needed to set a program to run, I guess it's in the delphi help