Solved

how to link C# project into C++ project

Posted on 2009-06-27
14
597 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

734 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