Solved

how to link C# project into C++ project

Posted on 2009-06-27
14
560 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
Comment Utility
>> 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
Comment Utility
0
 

Author Comment

by:dude2009
Comment Utility
I have to invoke C# function from metu bar item in C++ dll.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> 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
Comment Utility
Just make a C# COM visible component.

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

Expert Comment

by:evilrix
Comment Utility
>> 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Expert Comment

by:iUsername
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Is it possible to use WCF for this.
0
 
LVL 40

Expert Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now