Solved

how to link C# project into C++ project

Posted on 2009-06-27
14
598 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
[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
  • 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
Technology Partners: 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 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

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.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

630 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