Opening winforms from an MFC application

G

Guest

Hi all,

I guess this question has been asked many times in the past, but as a .NET
newbie ....

I have an MFC application from which I want to display some WinForms. The
desired sequence is something like:
- User presses a button
- Winform opens up like any MFC modal dialog.
- User enters some data, clicks the Winform's close button and control
returns to the MFC application.

Trying to achieve this I created a C# class library project, added a form to
it with a public SayHello() method. marked it as registered for Interop (in
project settings). Then created a simple project and imported the generated
TLB.

I encountered the first problem: the generated C++ tlh files contains only
CoClass and interface declaration. My SayHello() method is not exposed in
the com interface. Trying to mark it as ComVisible(true) didn't work.

Second (major) problem: What's the replacement of Application.Run() in MFC?
What's the right way to invoke the form to open up?


Thanks,

Omer
 
D

Daniel Carlsson

If you want vs.net to generate a full TLB you need to add the
ClassInterface(ClassInterfaceType.AutoDual) attribute to your class. Please
read the documentation about why thats not the default mode though. In short
its because if you use AutoDual you can get versioning clashes.

You can also create your own TLB but, again, check the documentation on
ClassInterface on that. Im not so good with COM so cant explain it any
better Im afraid.

Appliation.Run starts the standard process message handling loop, so in
theory at least you wouldnt need to do that as your program already has one
running. Im not sure if that works in practice though.

/Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top