Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Using multiple instances of the same DLL

Avatar of xxflip
xxflip asked on
Delphi
10 Comments1 Solution1354 ViewsLast Modified:
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.
ASKER CERTIFIED SOLUTION
Avatar of Lunchy
LunchyFlag of Canada image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answers