Solved

Using multiple instances of the same DLL

Posted on 2004-04-02
11
1,323 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
ID: 10739809
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
ID: 10740704
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:Mohammed Nasman
ID: 10745139
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
ID: 10745546
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
ID: 10756048
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:xxflip
ID: 10757176
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
ID: 10765976
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
ID: 10772650
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
ID: 10773096
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
ID: 10797997
Closed, 250 points refunded.
Lunchy
Friendly Neighbourhood Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delphi custom sort exception 6 135
Delphi XE2 application frozen on Windows 10 10 270
delphi prevent click fast 2 189
Communication Between RC4 Delphi <-> PHP 3 100
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

21 Experts available now in Live!

Get 1:1 Help Now