Solved

Problems with Full access to Delphi DLL from C++

Posted on 2004-10-12
17
230 Views
Last Modified: 2012-08-13
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!
0
Comment
Question by:cyrix05
  • 9
  • 8
17 Comments
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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?
0
 

Author Comment

by:cyrix05
Comment Utility
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......
0
 

Author Comment

by:cyrix05
Comment Utility
Solved it myself. Thanks to SteH for putting in some effort. =)
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
You might need to add WINAPI or __stdcall to the function definition:

typedef void   (__stdcall * DLLFUNCTION)();
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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.
0
 

Author Comment

by:cyrix05
Comment Utility
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.
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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?
0
 

Author Comment

by:cyrix05
Comment Utility
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Expert Comment

by:SteH
Comment Utility
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.
0
 

Author Comment

by:cyrix05
Comment Utility
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
0
 

Author Comment

by:cyrix05
Comment Utility
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.
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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.
0
 

Author Comment

by:cyrix05
Comment Utility
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?
0
 
LVL 13

Accepted Solution

by:
SteH earned 250 total points
Comment Utility
Not too sure about Delphi but I would expect to see something like

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

*You* need to delete (Free) all forms *you* created.
Is MainForm.Free acceptable or should it be MainForm.Free ()?
0
 

Author Comment

by:cyrix05
Comment Utility
Yeah its MainForm.Free, Thats Delphi.....

YEah but it doesn't work i have tried it already. Anyway Thanks......
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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?
0
 

Author Comment

by:cyrix05
Comment Utility
Although my problem is still not solved SteH helped out the most.....anyone with more suggestiions will get the points if it works.....
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

11 Experts available now in Live!

Get 1:1 Help Now