Groups | Blog | Home
all groups > dotnet interop > september 2007 >

dotnet interop : C# and C++ in same assembly


nickdu
9/19/2007 2:46:03 PM
I've been reading through the MSDN docs and just can't seem to find an answer
to my question. What I'm trying to find out is whether it's possible to
introduce C# into our C++ application. I don't want to do this via a DLL but
instead would like the C# code to be statically linked in the executable. Is
this possible?

What I'm having a hard time figuring out is how the C++ code would know
about the C# classes. In the unmanaged world you simply include a header
file for the external types. If what I want to do is possible how would the
C++ code be able to new the C# class? How would it know about it? Is there
some pragma statement?

In the research I've done it seems as if I should be able to compile a CS
file to a .netmodule and then link it with my C++ code. Is this not the case?
--
Thanks,
Nick

nicknospamdu@community.nospam
G Himangi
9/20/2007 12:00:00 AM
Yes, its possible if your C++ supports Managed Extensions, you specify the
..netmodule on the Linker options page - "Additonal Input Modules"

---------
- G Himangi, Sky Software http://www.ssware.com
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
---------



[quoted text, click to view]

nickdu
9/20/2007 3:36:01 AM
Great, thanks.

Unfortunately the MSDN docs don't discuss this or I'm just bad at finding
it. As I mentioned I read up on the .netmodule and some of that
documentation led me to believe it was possible. The part I was missing was
the /FU on the c++ compile. Since I didn't know about this I was not
understanding how the C++ code would be made aware of the C# classes. This
explains it (I hope). I'll give it a try.

Your example is great as I usually work from the command line and use
makefiles. I'm not a big fan of Visual Studio. Unfortunately most others in
the group are using VS so I'm going to need to figure out what GUI element
corresponds to the /FU flag.
--
Thanks,
Nick

nicknospamdu@community.nospam
remove "nospam" change community. to msn.com


[quoted text, click to view]
nickdu
9/20/2007 3:42:01 AM
Great, thanks.

Unfortunately the MSDN docs don't discuss this or I'm just bad at finding
it. As I mentioned I read up on the .netmodule and some of that
documentation led me to believe it was possible. The part I was missing was
the /FU on the c++ compile. Since I didn't know about this I was not
understanding how the C++ code would be made aware of the C# classes. This
explains it (I hope). I'll give it a try.

Your example is great as I usually work from the command line and use
makefiles. I'm not a big fan of Visual Studio. Unfortunately most others in
the group are using VS so I'm going to need to figure out what GUI element
corresponds to the /FU flag.
--
Thanks,
Nick

nicknospamdu@community.nospam
remove "nospam" change community. to msn.com


[quoted text, click to view]
wawang NO[at]SPAM online.microsoft.com (
9/20/2007 10:22:36 AM
Thanks G Himangi for your informative input.

Hi Nick,

Here's a quick sample on how to achieve this (based on this blog
http://blogs.msdn.com/texblog/archive/2007/04/05/linking-native-c-into-c-app
lications.aspx, it's linking native c++ into C#, what we needed is the
opposite but steps are similar):


1) Create a C++ file clrcode.cpp:


using namespace System;
using namespace ClassLibrary1;

void main()
{
Class1^ c1 = gcnew Class1();
c1->Test();
}



2) Create a C# class class1.cs:

using System;
using System.Collections.Generic;
using System.Text;


namespace ClassLibrary1
{
public class Class1
{
public void Test()
{
Console.WriteLine("Test");
}
}
}


3) Start VS2005 environment command prompt:

csc /target:module Class1.cs
cl /clr /LN /MD clrcode.cpp /FU Class1.netmodule

link /LTCG /CLRIMAGETYPE:IJW /SUBSYSTEM:CONSOLE
/ASSEMBLYMODULE:clrcode.netmodule /OUT:MixedApp.exe clrcode.obj
Class1.netmodule

MixedApp


Hope this helps.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
wawang NO[at]SPAM online.microsoft.com (
9/21/2007 12:00:00 AM
Hi Nick,

For the "/FU" switch, please see document here:

http://msdn2.microsoft.com/en-us/library/fwkeyyhe(VS.80).aspx
<quote>
/FU Forces the use of a file name as if it had been passed to the #using
directive.
</quote>


This is equivalent to following code:

#using "ClassLibrary1.dll"

using namespace ClassLibrary1;

int _tmain(int argc, _TCHAR* argv[])
{
Class1^ c1 = gcnew Class1();
c1->Test();
return 0;
}


I would suggest you to leave the .netmodule generating and linking to your
makefile, in Visual Studio, use the #using and call the external c#
assembly. This is because currently there's no GUI option to generate a
module from your C# class, unless you use post-build event to call external
build scripts or customize the project file (msbuild):

#Sebastien St-Laurent's (AKA Sebby) WebLog : Practical .NET2 and C#2: An
introduction to MSBuild
http://blogs.msdn.com/sebby1234/archive/2006/04/01/565084.aspx


Please feel free to let me know if there's anything unclear. Thanks.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
wawang NO[at]SPAM online.microsoft.com (
9/26/2007 3:32:15 AM
Hi Nick,

I'm writing to check the status of this post. Please feel free to let me
know if there's anything else I can help. Thanks.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button