?
Solved

Loading MFC Dll in C# Form

Posted on 2006-06-28
13
Medium Priority
?
1,403 Views
Last Modified: 2013-11-20
Suppose there is an MFC dll which creates some CWnd.

Some Functions are exported in MFC dll.

I need to display this CWnd on C# Form as otherchild controls of Form.

How Should I do to implement it?

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
  • 7
  • 6
13 Comments
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17000831
// Sample program to call unmanaged code

using System;
using System.Runtime.InteropServices;

class PInvoke1App
{
    [DllImport("user32.dll")]
    static extern int MessageBoxA(int hWnd, string strMsg,string strCaption,int iType);

    public static void Main()
    {
        MessageBoxA(0,   "Hello, World!", "This is called from a C# app!",  0);
    }
}

above calls MessageBoxA function from user32.dll



following may be of your use refer:
'Call Unmanaged DLLs from C#, ' :http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/
'Using Unmanage Code in C# ' http://www.codeguru.com/Csharp/Csharp/cs_syntax/unmanagedcode/article.php/c5887/

-MAHESH
0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17001308
Hi mahesh...

Thanks for the quick response

I have implemented this 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.




0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17001350
Will see the specified urls also...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:mahesh1402
ID: 17002160
>>Dll name will be decided on runtime.

you can also create a DLL which can forward function calls to any location and load any DLL further into your address space... using LoadLibrary() function..

Have a look following codeproject article :
Late binding on native DLLs with C#
http://www.codeproject.com/csharp/dyninvok.asp

-MAHESH
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17002187
rather look into this for quick solution :

Dynamic Invoke from Unmanaged DLL
http://www.codeproject.com/csharp/dynamicinvokedll.asp <== this may be of your use

-MAHESH
0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17007108
>>http://www.codeproject.com/csharp/dyninvok.asp
Already seen.......

>>http://www.codeproject.com/csharp/dynamicinvokedll.asp 
Tried similar to this one not exactly same......

Will try and confirm....
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 2000 total points
ID: 17007267
You may take consider using LoadLibrary() and GetProcAddress()

Also You can create dynamic PInvoke methods with Reflection Emit. See
http://www.msjogren.net/dotnet/eng/samples/dotnet_dynpinvoke.asp 

-MAHESH
0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17007277


http://www.msjogren.net/dotnet/eng/samples/dotnet_dynpinvoke.asp 

404

The requested document was not found on this server.


--------------------------------------------------------------------------------

Web Server at msjogren.net
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17007292
check this link if u able to download this
http://www.msjogren.net/dotnet/download.asp?dynpinvoke.zip
0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17007315
yes downloaded....
wil check it..
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17007368
again click on link it will open its opening at my end
0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17007432
Not opening....

Downloaded the zip.....


I treid to open
http://www.msjogren.net/dotnet/

Showing

The site is temporarily down, sorry. Please check back in a couple of weeks.


Anyway....trying to understad the code given in zip file and will implement...
0
 
LVL 3

Author Comment

by:jitendra_wadhwani
ID: 17008746
Hi Mahesh,
Thanks for the help.....

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. You are the only person who responded for this question...Giving Grade A

Thanks

Jitendra
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
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…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 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