Solved

Using multiple instances of the same DLL

Posted on 2004-04-02
11
1,320 Views
Last Modified: 2007-12-19
I have a DLL that provides an interface to view data from a Database.  This DLL is used by the mother app with no problem, but now I need to be able to call that same DLL from diferent programs.

Situation: I have 2 EXE files, named Interface1 and Interface2, that call the DLL as well as a batch file that uses rundll32 to call my DLL

Problem:
1. If I call the DLL from each interface (Interface1, Interface2 and Rundll32) one at a time (terminating it's use before I call it again) I have no problem.
2. If I call the DLL using 2 or more interfaces, including multiple instances of the same interface, I get an error on the interface when I close it, after the DLL_PROCESS_DETACH, on all open interfaces except the last one.
3. The error I get is
  "Interface1.exe - Aplication error"
  "The exception unknown software exception (0x0eedfade) occurred in the application at location 0x7c59ba9d."

and then I get
  "Error"
  "Runtime error 217 at 00E5088B"

My guess is that when I try to free the DLL, it somehow has a conflict with the other open instances.

// ------------- The exported functions are:
-InitCC - Opens the interface as a regular window
-InitCCMDI - Opens the interface as a MDI child

// ------------- Batch code:
  rundll32.exe ccorrentes.dll,InitCC  (I use full paths for both files)

// ------------- The Delphi code in Interface1 and Interface2 is:
   DllHandle := LoadLibrary('ccorrentes.dll');
   if DllHandle <> 0 then @Child := GetProcAddress(DllHandle, 'InitCCMDI');
   if @Child <> nil then Child(Application);

// ------------- The DLL dpr file is:
library ccorrentes;

uses
  Forms,
  Windows,
  SysUtils,
  Form_extracto_ccDll in 'Form_extracto_ccDll.pas' {F_extracto_cc};

{$R *.RES}

var
  MyApp:TApplication;

procedure InitCC; export; stdcall;
begin
  Application.CreateForm(Tf_extracto_cc, f_extracto_cc);
  f_extracto_cc.ParentWindow:=Application.Handle;
  f_extracto_cc.ShowModal;
  f_extracto_cc.Free;
end;

procedure InitCCMDI(TApp:TApplication); export; stdcall;
begin
  Application:=TApp;
  Application.CreateForm(Tf_extracto_cc, f_extracto_cc);
  f_extracto_cc.ParentWindow:=TApp.Handle;
  f_extracto_cc.FormStyle:=fsMDIChild;
  f_extracto_cc.Show;
end;

procedure DLLEntryPoint(Reason: DWORD);
begin
  if Reason = DLL_PROCESS_DETACH then Application:=MyApp;
end;

exports
  InitCC,
  InitCCMDI;

begin
  MyApp:=Application;
  DllProc := @DLLEntryPoint;
end.
0
Comment
Question by:xxflip
11 Comments
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
instead of passing Application pass only it's handle

procedure InitCCMDI(hApp: Cardinal); stdcall;
begin
  if hApp > 0 then
    Application.Handle:=hApp;


....

// ------------- Batch code:
  rundll32.exe ccorrentes.dll,InitCC, 0  (I use full paths for both files)

// ------------- The Delphi code in Interface1 and Interface2 is:
   DllHandle := LoadLibrary('ccorrentes.dll');
   if DllHandle <> 0 then @Child := GetProcAddress(DllHandle, 'InitCCMDI');
   if @Child <> nil then Child(Application.Handle);

0
 
LVL 2

Author Comment

by:xxflip
Comment Utility
That didn't work.

Passing just the handle causes more problems, I think that is because I'm using a form that is a MDI child when called by the Interface1 and Interface2 apps.

Also note that I only get an error with my solution, if another instance of the DLL is loaded by another interface.
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
Hello

  For MDI child, you need to pass the application and screen objects, see my comments in this paq
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20653308.html
0
 
LVL 2

Author Comment

by:xxflip
Comment Utility
I won't be back to the office until monday, but I will check that then, if it works the points are yours!
0
 
LVL 2

Author Comment

by:xxflip
Comment Utility
I have passed the screen object alogn with the application, just like the example, but it once again created more problems (got some errors about not being able to assign a TFont to a TFont) that I think are due to some components I'm using.
But I did try something else I read in the linked post:

FreeLibraryAndExitThread(DllHandle, 0);


Having this in mind I then proceeded to using the windows API to terminate the thread on the OnDestroy event of the form, I used this instead of FreeLibraryAndExitThread inside the interface code, because I will not be able to control the interface, only the DLL.

TerminateThread(GetCurrentThread,0);


Result: I get no errors regardless of how many symultaneous interfaces I open (Hurra) but the interfaces are not removed from the running process list (they are not using the processor but they are still loaded and using the DLL).
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 2

Author Comment

by:xxflip
Comment Utility
Update:
I changed the TerminateThread with ExitThread, and now I get no errors whatsoever, but there is still something I have to solve.
Let's say that I start 3 instances of each (interface1, interface2 and rundll) and then close them randomly. They all close with no errors BUT the last interface1 to be closed (which is the only one that loads the form as MDI) does not completly close.

What I mean is if I look at the processes it is still there (although I can't see it anymore on the screen/taskbar) and the DLL reports as still being used.

Any suggestions??


Here is the code for the DLL:

//----------------------- DLL
library ccorrentes;

uses
  Forms,
  Windows,
  SysUtils,
  Form_extracto_ccDll in 'Form_extracto_ccDll.pas' {F_extracto_cc};

{$R *.RES}

var
  MyApp:TApplication;

procedure InitCC; export; stdcall;
begin
  Application.CreateForm(Tf_extracto_cc, f_extracto_cc);
  f_extracto_cc.ParentWindow:=Application.Handle;
  f_extracto_cc.ShowModal;
end;

procedure InitCCMDI(TApp:TApplication); export; stdcall;
begin
  Application:=TApp;
  Application.CreateForm(Tf_extracto_cc, f_extracto_cc);
  f_extracto_cc.ParentWindow:=TApp.Handle;
  f_extracto_cc.FormStyle:=fsMDIChild;
  f_extracto_cc.Show;
end;

procedure CloseCCMDI; export; stdcall;
begin
  if Assigned(f_extracto_cc) then f_extracto_cc.Free;
end;

procedure DLLEntryPoint(Reason: DWORD);
var ec:dWord;
begin
  if Reason = DLL_PROCESS_DETACH then begin
    Application:=MyApp;
    ExitThread(0);
  end;
end;

exports
  InitCC,
  InitCCMDI,
  CloseCCMDI;

begin
  MyApp:=Application;
  DllProc := @DLLEntryPoint;
end.
0
 
LVL 2

Author Comment

by:xxflip
Comment Utility
I have tried the previous code in other machines with the same OS, and the funny thing is I had no problems on them, so this is the solution I will be using.

Solution used:
I added a Close procedure to release forms, to make both situations clearer (MDI and non-MDI)
I added the line ExitThread(0); in the DLL_PROCESS_DETACH which eliminates the errors


Lee_Nover: Your suggestion was "instead of passing Application pass only it's handle"
mnasman: Your suggestion was "For MDI child, you need to pass the application and screen objects"

Since I used neither solution I will be asking for a refund of my points, but I would like to thank you both for the time you spend on this issue.
0
 
LVL 3

Expert Comment

by:smurff
Comment Utility
Although they didnt find the exact answer, they still spent time helping you. I normally give delete this question and post others just saying "points for XXX" just 50pts but its nice to say thx for helping out.

My 2 pence worth.
smurff
0
 
LVL 2

Author Comment

by:xxflip
Comment Utility
I agree with you, but I had read something about this not being a correct procedure.
I have investigated the matter further in the help pages and found the proper way to do so.


Reason for giving 50 points to mnasman:
Although I didn't get the answer for my question, it was through the link provided by mnasman that I got the information needed to solve it myself.


Points to question link: http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/Delphi/Q_20946203.html
0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
Comment Utility
Closed, 250 points refunded.
Lunchy
Friendly Neighbourhood Community Support Moderator
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now