Link to home
Start Free TrialLog in
Avatar of cyrix05
cyrix05

asked on

Problems with Full access to Delphi DLL from C++

Anyone with any idea please........

1) I created a Delphi DLL (Application in the DLL).

2) Tried calling it from a Delphi program, no problems. Working 100%

3) tried calling it from C++, Application lauches. Everything works EXCEPT. All the SHOWMESSAGES in the DLL are not working, together with all the dialogs (Open, save, etc). Must be something about opening an external window? Remember this works fine when i was calling it form delphi.

4) I am using a CFormView with Style as Child. Calling an external window AFXMESSAGEBOX and Open save dialogs all works fine from this project. I donno if that makes any difference.

This is really bugging me. anyone please help. Thanks!
Avatar of Member_2_1001466
Member_2_1001466

Are you trying to access it from VC++? It should be no problem from BCB.

The problem you have seems to be related to threads. Additional threads should not open a window in MFC. Otherwise messages from the main thread might get lost. Is the DLL creating a new thread?
Avatar of cyrix05

ASKER

Nope. No threads in the DLL

I have tested it, trying to launch from a CDialog, same results. I guess problem lies with C++. This is how i call the DLL

      HINSTANCE hinst=LoadLibrary("xxx.dll");
      typedef void (*DLLFUNCTION)();
      DLLFUNCTION dllfunction;
      if (hinst)
            dllfunction=(DLLFUNCTION)GetProcAddress(hinst, "LoadDll");
      if (dllfunction)
            dllfunction();

Anyone has any idea? Please......
Avatar of cyrix05

ASKER

Solved it myself. Thanks to SteH for putting in some effort. =)
You might need to add WINAPI or __stdcall to the function definition:

typedef void   (__stdcall * DLLFUNCTION)();
C++ does calling of functions differently from C/Pascal/Delphi. The compiler has to be made aware of the different calling conventions used with that function.
Avatar of cyrix05

ASKER

Hi,

I have a new problem. Delphi's showmodal only works for model dialogs,

show works for modeless. When i use show, this si what happens

1) When i launch my program and close the program its fine. subsequent opening and closing is fine.

2) When i open a file with the program and close the program WITHOUT closing the file first, and when i try to lauch the program again, there is an access violation error.

IN my Delphi code, my export fnuction is a LoadMain where i create the forms i need and do a mainform.show.

When i close or exit, I use Mainform.free. When i use Close; It crashes when i close the program.

Whoever helps with this issue will get the points. Thanks.
Why is the file not closed before quitting the program? If not closed yet a cleanup routine should close it before the application is ended.

For Show and ShowModal they do things a bit similar for different type of dialogs. A modal dialog will stay on top of its parent and no execution will happen in the main thread until this dialog is closed. This is initiated with a ShowModal call. In the other case the dialog is just another window which is displayed. It will popup on top of its parent but the parent can still be used. The dialog will not end by itself in this case but only when the application terminates or the dialog is closed either by closing the window or by some message handler which calls its close function.

>IN my Delphi code, my export fnuction is a LoadMain where i create the forms i need and do a mainform.show.
>When i close or exit, I use Mainform.free. When i use Close; It crashes when i close the program.

I don't understand what your problem here is. Can you try to explain it in more detail please?
Avatar of cyrix05

ASKER

Hi Steh,

I know the diff between show and showmodal. its jsut really weird. I use Showmodal I have no problems...i just close the form. var Action: TCloseAction

When i use. show it complains when i use show. I have to go An extra line, MainForm.free. But when I do it, and i relaunch the Program, it gives me an error. Odviously this happens cause my library is not completed unloaded, But how do you tell my C++ program that my delphi DLL is finished and needs to freelibrary?

I hope it makes sense.....Am i missing something  here? thanks a million.
ShowModal will clean up when the dialog/window has finished. When you display a window via Show the compiler does not know when you actually want to clean up. You have to provide the code for this. You can call Show (SW_HIDE) which will not show the window but hide it. It is then constructed and ready for display.

Override OnClose to do the cleanup. In principle you need to release all memory you allocated. This holds true for external modules and DLLs as well.
Avatar of cyrix05

ASKER

OK so my function

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
MainForm.Free;//Effectively this is the only clean up i do? Anything else? Where do i put SW_Hide?
first:= false;//My Own Stuff
bCen := false;//My Own Stuff
PutRegistryData;//My Own Stuff
end;

What has to change? Sorry abt it, This is my first run with delphi. Thanks
Avatar of cyrix05

ASKER

OH sorry, I got what you are saying.....So i should go something like MainForm.Show(SW_Hide) But I do not see how that would help.....You eman in my cleaning hide the form instead of closing it? But now the problem is with the number of times i do a show? Or the first time I am not cleaning up properly? Cause i am not sure which part has to change. Thanks.
I am not sure whether you can profit from only hiding the window. You said that the window is called from a DELPHI DLL? In that case it could be an idea to create the window when loading the DLL and showing it when actually needed and hiding when no longer needed. Clean up has to take place in that case when the DLL gets unloaded.

But your problem seems more that you are allocating something at one place which doesn't gets deallocated. It is difficult to propose something for a thing I am not sure what it actually is. To provide a solution I need code to see where you allocate and where you do freeing to be able to spot what not gets freed.
Avatar of cyrix05

ASKER

HI Steh, Sorry for all this trouble.....

OK So The Delphi Function i am calling from C++ (every time a button is pressed)is LoadMain

procedure LoadMain;stdcall;
begin
          Application.CreateForm(TMainForm, MainForm);
          Application.CreateForm(TRawForm, RawForm);
          Application.CreateForm(TMultiSliceForm, MultiSliceForm);
          Application.CreateForm(TTipForm, TipForm);
          Application.CreateForm(TLicensingForm, LicensingForm);
          Application.Title := 'GC1000Dicom';
          Application.CreateForm(TAboutBox, AboutBox);
          MainForm.Show;
          first := false; //my own crap
end;

This is my FormClose Method.

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
MainForm.Free;
first:= false;//My Own Stuff
bCen := false;//My Own Stuff
PutRegistryData;//My Own Stuff
end;

So this works fine when i press the loadMain button for the first time. But when i close the form and load it again, it has problems. Thanks Is this enough?
ASKER CERTIFIED SOLUTION
Avatar of Member_2_1001466
Member_2_1001466

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 cyrix05

ASKER

Yeah its MainForm.Free, Thats Delphi.....

YEah but it doesn't work i have tried it already. Anyway Thanks......
Have you tried it with your order or mine? And are you creating something during some actions in any of the forms which doesn't get freed?

Otherwise step trough the code when the error happens. What line of code is the first of yours?
Avatar of cyrix05

ASKER

Although my problem is still not solved SteH helped out the most.....anyone with more suggestiions will get the points if it works.....