Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loading MFC dll in C# Form

Posted on 2006-06-28
14
Medium Priority
?
1,021 Views
Last Modified: 2012-05-05
I want to load MFC dll inside the C# from?

How can I do?

Suppose there is an MFC dll which creates some CWnd. I need to display this CWnd on C# Form as otherchild controls of Form.
Some Functions are exported in MFC dll.

Need Dynamic Loading-Unloading

I am an MFC programmer and very new to C#.

Thanks in advance.

0
Comment
Question by:jitendra_wadhwani
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
14 Comments
 
LVL 11

Assisted Solution

by:Jase-Coder
Jase-Coder earned 1000 total points
ID: 17001045
you do:

DllImport("My.dll")]
          Supply the signiture of the function you want to use from the dll here
0
 
LVL 25

Assisted Solution

by:dstanley9
dstanley9 earned 600 total points
ID: 17001165
There are a few options with integrating MFC and C#:  PInvoke and C++.NET.  PInvoke requires that you export C-style functions from the DLL, while using C++.NET allows you to create a .NET "wrapper" around your MFC DLLs using C++ that can be consumed by C# clients.

These articles may help:

http://msdn.microsoft.com/msdnmag/issues/06/05/MixAndMatch/
http://www.developer.com/net/cplus/article.php/3560871
0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17001341
Will se the links soon

Thanks to both of you for the quick response

I have implemented DllImport method....

I have more than 500 dlls in my project and I dont want to import each and every dll althought all the dlls have same exported functions for creating and destroying.

Suppose I have created One CWnd and I want to set its parent as C# form.

I need to pass the parent to MFC dll and want to use in MFC dll....

I am anyhow able to pass the handle of C# form as long but getting CWnd* from that long created some problem in MFC.


I need one function in which I will pass DLL Name and parameters. Want to load that particular DLL and also there should be any unloading process.

Dll name will be decided on runtime.

Hope you understood my requirement.



Also tried pInvoke...
need a very small example specially to pass parent handle and how to use in in MFC also

Simple example like creating Edit box on Form thruogh MFC
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:AlexFM
ID: 17001771
I think that it is impossible. You can waste a lot of time without any results.
You need to think about other solution. If you want to discuss this, describe your problem, maybe there are other ways to do this.
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17001892
This is not possible.  You cannot convert MFC class pointers to c# handles.  Confirmation from two MS MVPs:

http://www.dotnet247.com/247reference/msgs/58/293040.aspx
0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17007014
Mr AlexFm,

You may be right because I have already wasted lot of time......


Can consider a simple problem

I an MFC DLL there is a function

here any parameter should be passed which can be used as parent

extern "C" int CreateComponent(Any paramater)
{

CWnd* pParent = Anyhow Get Pointer to set pointer from passed Paramater


      theApp.m_pTestWnd = new TestWnd();


      theApp.m_pTestWnd->CreateTestWindow(pParent);
                                              OR
      theApp.m_pTestWnd->CreateTestWindow( Any Parameter if pParent is not got yet);



}





void TestWnd::CreateTestWindow(parameter )
{

if pParent is not passed then

CWnd* pParent = Anyhow Get Pointer to set pointer from passed Paramater

Create(_T("EDIT"), "Hi", WS_TABSTOP |WS_CHILD | WS_VISIBLE|WS_BORDER,
       CRect(20, 120, 200, 150),  pParent , 1234);

}




If I am passing the CSharp Form Handle as long and here I am typecasting it to HWND

And From HWND I am Getting CWnd* and using it...

This process gives some failure two times and on clicking Ignore it somehow displays the required Window on the form but didn't getting
the requiree parent child relation ship....

If is say GetParent and change its caption it is changing form's caption but by pressing TAB the window does not get focus....

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17007088
I am talking about general design, not about specific technical problems, I think there is no direct solution.
For example, you have MFC library with some functionality, and want to use this functionality in new C# application by using edit box. Assuming that this is impossible, is there other way? For example, rewrite control in C#. Or use C# textbox which calls functions from MFC library for validation and other purposes.
0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17007132
I have 500 Dlls(even more that that) in my project, Everything was in VC++

I use then by LoadLibrary and FreeLibrary in VC++ and GetProcAddress to call teh required exported functions....


Most of them are CWnd Based, whose parent should be some Window, which was earliear mainframe window...

Now I want to replace main application can say core part with C#, so that I can resuse those dll by minor modifications in those dlls.


I hope you understood my req.


Regards
Jitendra
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17007361
Moving to C#, you can use legacy code for operations which are not related to Windows: calculations, sockets, image processing, file handling etc. UI-related tasks should be rewritten in C#.
Future .NET versions allow to host .NET controls in MFC applications, but I didn't see anything which allows to host MFC controls in .NET applications. .NET Windows form class requires from child control to be instance of Control class and anything else.
In your case I would continue to work with existing MFC application.  You can add .NET support to it on low levels, but general framework should be MFC.
Other way is to use .NET on high level, and use existing code in low level, but these functions should not expose native windows, only general stuff. Windows should be created by .NET.

It is possible, however, to use existing dialogs from MFC in C# applications, but not child windows.
0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17007418
Means I have wasted time till now and still wasting my time...
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 400 total points
ID: 17007574
I suggest you to ask this question in Microsoft C# forum:
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=31&SiteID=1

Having opinion from Microsoft developers you can decide what to do.
0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17007703
yes....This can be a Great suggestion....
0
 
LVL 11

Assisted Solution

by:Jase-Coder
Jase-Coder earned 1000 total points
ID: 17007958
I dont know if this will help but here is a article from the msdn magazine regarding mfc and window forms:

http://msdn.microsoft.com/msdnmag/issues/06/05/MixAndMatch/
0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17008766
Thanks to all for the help.....

To Mr. AlexFM and dstanley9 ....... It is posible and I have done it.....

Although The provided Help has not solved my issue but I have achieved the Displaying part now other part is remainning....

My 500 Dlls were developed in 3000 man days.....

Your provide help has enriched my knowledge. Splitting the points....

Regards
Jitendra
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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