Solved

how to link C# project into C++ project

Posted on 2009-06-27
14
569 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Does this SQL code look like it will work using VS2010 with .NetFramework 4.0 ? 2 42
Host asp.net pages 5 25
SQL Login 17 40
Entity Framework 7 32
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

910 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

16 Experts available now in Live!

Get 1:1 Help Now