Solved

how to link C# project into C++ project

Posted on 2009-06-27
14
586 Views
Last Modified: 2012-05-07
Hi
I want to link C# project into main C++ application.
So from C++ application it calls C# project as part of event handling( in C++ app).

Thanks,
0
Comment
Question by:dude2009
  • 7
  • 3
  • 2
  • +1
14 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 375 total points
ID: 24729235
>> I want to link C# project into main C++ application.
You can't "link" a C# application into a C++ application... that is not possible. You can use a mixed mode DLL to allow unmanaged and managed C++ code to reside in the same assembly to allow you to call C# code in a different assembly.

http://msdn.microsoft.com/en-us/library/x0w2664k.aspx

"Mixed assemblies are capable of containing both unmanaged machine instructions and MSIL instructions. This allows them to call and be called by .NET components, while retaining compatibility with components that are entirely unmanaged. Using mixed assemblies, developers can author applications using a mixture of managed and unmanaged functionality. This makes mixed assemblies ideal for migrating existing Visual C++ applications to the .NET Platform."
0
 
LVL 21

Assisted Solution

by:Tapan Pattanaik
Tapan Pattanaik earned 125 total points
ID: 24729268
0
 

Author Comment

by:dude2009
ID: 24738496
I have to invoke C# function from metu bar item in C++ dll.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 40

Expert Comment

by:evilrix
ID: 24738527
>> I have to invoke C# function from metu bar item in C++ dll.
You can do that using managed C++ using a mixed mode DLL as linked above. You managed C++ assembly can reference the C# assembly. Managed and unmanaged (native) code can reside in the same C++ assembly.
0
 
LVL 5

Expert Comment

by:iUsername
ID: 24738528
Just make a C# COM visible component.

Than you can load the C# COM component in C++.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24738597
>> Than you can load the C# COM component in C++.
That really is the hard way to do this. C++ can be compiled as managed/unmanaged in the same assembly and the managed portion can access C# direct (no need to resort to com) using IJW (It Just Works -- no I did not make that up it is a Microsoft-ism). The managed C++ marshals call from native C++ to C#.

[unmanged C++] -- calls --> [managed C++ in bridge DLL] -- calls C# --> [C# assembly]
0
 
LVL 5

Expert Comment

by:iUsername
ID: 24738894
How can you call directly from unmanaged C++ to managed C++ ?
Are you talking about exported functions???

Besides, using COM visible is REALLY simple in .Net.
Just mark the project as COM visible, and add a GUID attribute to the interface and GUID to the class that implements the interface.
Than:

[unmanaged C++] -- calls --> [C# assembly]
(there are some hidden things like the interop and all that, but it is hidden from the programmer).
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 375 total points
ID: 24740021
>> How can you call directly from unmanaged C++ to managed C++ ?
Because it's a mixed mode DLL that contains managed and unmanaged C++, which can directly call each other (remember, IJW?)

>> Are you talking about exported functions???
No. I am talking about a mixed mode DLL that contains managed and unmanaged C++ living side by side interfacing seamlessly.

As noted in my very first post...
"Mixed assemblies are capable of containing both unmanaged machine instructions and MSIL instructions. This allows them to call and be called by .NET components, while retaining compatibility with components that are entirely unmanaged. Using mixed assemblies, developers can author applications using a mixture of managed and unmanaged functionality. This makes mixed assemblies ideal for migrating existing Visual C++ applications to the .NET Platform."

http://msdn.microsoft.com/en-us/library/x0w2664k.aspx

>> Besides, using COM visible is REALLY simple in .Net.
But it is NOT so simple in unmanaged C++ to consume a and why go to the effort of messing with COM when managed and unmanaged C++ can just coexist?

>> Just mark the project as COM visible, and add a GUID attribute to the interface and GUID to the class that implements the interface.
You are introducing additional technologies (COM in this case) that are just unnecessary.

>> there are some hidden things like the interop and all that, but it is hidden from the programmer
Like Microsoft say, It Just Works! You don't need to know nor care, you just have managed and unmanaged C++ living in one assembly co-existing calling each other, that's it. Simple.

More on IJW: http://msdn.microsoft.com/en-us/library/aa712982.aspx
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 375 total points
ID: 24740080
Below is an example of IJW at work. Unfortunately, the only code snippet I have to hand is managed C++ marshalling C# to unmanaged C++ but the principle the other way around is identical. This simple bit of code shows just how simple it is to use IJW to interface between managed and unmanaged code, no need to mess with GUIDs or other nasty COM things.
// C++ (all in one IJW assembly)
 
#include <iostream>
 
namespace UnmanagedCode
{
        void foo()
        {
                std::cout << "Hello, world" << std::endl;
        }
}
 
namespace ManagedCode
{
        public ref class fooWrapper
        {
        public:
                static void foo()
                {
                        UnmanagedCode::foo();
                }
        };
}
 
 
// C# code (seperate assembly)
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace scratchcs
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagedCode.fooWrapper.foo();
        }
    }
}

Open in new window

0
 

Author Comment

by:dude2009
ID: 24765209
Hi I am trying to use bridge assmebly approach. Meanwhile can you give more details...on using COM approach.

Thanks
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24765309
See if this helps. You probably want to read "Part II : Consuming .NET Components from COM aware Clients"
http://www.codeproject.com/KB/COM/cominterop.aspx
0
 

Author Comment

by:dude2009
ID: 24805311
Is it possible to use WCF for this.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24805910
>> Is it possible to use WCF for this.
I'm afraid I have no idea.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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