Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Including C# file in a VC++.NET project

40 views
Skip to first unread message

David Ching

unread,
Dec 3, 2005, 4:25:04 PM12/3/05
to
Hello, I am just starting to experiment with VC2005 to create a C++ WinForms
app. I have created a simple form, and it works. Now I want to add a C#
.cs source file containing a new class that I downloaded from CodeProject to
use in my C++ app. I've tried simply adding the C# source file to my C++
project, but there is no compiler options in the settings. The IDE doesn't
seem able to compile the .cs file. Am I able to add source files from other
.NET languages such as C# and VB.NET to my C++ project?

I did succeed in creating another project for a C# DLL to the same solution,
and was able to access the C# class by #using the DLL in my C++ app. But is
it required that modules in each language (like C#) be placed in a separate
module and be built together? If so, it is kind of inconvenient.

Thanks,
David
http://www.dcsoft.com

David Anton

unread,
Dec 3, 2005, 8:12:01 PM12/3/05
to
It is possible to convert the file. Our Instant C++ C# to C++ converter demo
will convert 100 lines at a time, so pasting the methods one at a time in
should work. (Although you will get a better conversion by converting the
entire file at once).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

"David Ching" wrote:

> Hello, I am just starting to experiment with VC2005 to create a C++ WinForms
> app. I have created a simple form, and it works. Now I want to add a C#

> ..cs source file containing a new class that I downloaded from CodeProject to

> use in my C++ app. I've tried simply adding the C# source file to my C++
> project, but there is no compiler options in the settings. The IDE doesn't
> seem able to compile the .cs file. Am I able to add source files from other

> ..NET languages such as C# and VB.NET to my C++ project?

Carl Daniel [VC++ MVP]

unread,
Dec 4, 2005, 12:21:42 AM12/4/05
to

Yes, that's a requirement. You can't build a single DLL or EXE from a mix
of languages.

You should, however, be able to access your C# class from the C++ winforms
project with the appropriate project references and #using directive. Make
sure that the class is public in the C# source code!

-cd


JAL

unread,
Dec 4, 2005, 2:32:02 AM12/4/05
to
David... I don't think you can call code in a C# netmodule from a C++/cli
netmodule, at least I have not been able to figure this out. But you can
compile a netmodule in C# using something like csc /target:netmodule
MyCSCode.cs and something like cl /clr /LN MyCPPCode.cpp in C++. And you
should be able to create an assembly with something like cl /clr
MyCSCode.netmodule MyCPPCode.netmodule MoreCPPCode.cpp. which mixes the C#
and C++/cli pure generated code into a single assembly. You can prove this by
looking at the IL using the ildasm.

"David Ching" wrote:

> Hello, I am just starting to experiment with VC2005 to create a C++ WinForms
> app. I have created a simple form, and it works. Now I want to add a C#

> ..cs source file containing a new class that I downloaded from CodeProject to

> use in my C++ app. I've tried simply adding the C# source file to my C++
> project, but there is no compiler options in the settings. The IDE doesn't
> seem able to compile the .cs file. Am I able to add source files from other

> ..NET languages such as C# and VB.NET to my C++ project?

Willy Denoyette [MVP]

unread,
Dec 4, 2005, 8:16:27 AM12/4/05
to
Sure you can call code in another netmodule, provided your C++ module is a
"safe" module, all you have to do is add a reference(s) to the 'other'
netmodule(s).
Here is how you can build a dll from two different code netmodules
cl /clr:safe /LN cppmod.cpp
// compile as cs file that calls into a cpp code module; add a reference to
the cpp netmodule
csc /t:module /addmodule:cppmod.netmodule csmod.cs
// link both modules into a single dll assembly file.
link /dll /out:cppcsmod.dll cppmod.netmodule csmod.netmodule

Willy.


"JAL" <J...@discussions.microsoft.com> wrote in message
news:DAED7CD7-B854-4971...@microsoft.com...

Holger Grund

unread,
Dec 4, 2005, 9:31:17 AM12/4/05
to
"Willy Denoyette [MVP]" <willy.d...@telenet.be> wrote

> Sure you can call code in another netmodule, provided your C++ module is a
> "safe" module, all you have to do is add a reference(s) to the 'other'
> netmodule(s).

Why the restriction to "safe modules" (I take it you mean /clr:safe)?

What's wrong with creating an ordinary object file (i.e. not a
netmodule) with VC and linking with the C# netmodule? (You
may of course still create a netmodule from the object file
to be used as a reference for C# code, so you can call C++
code from C#)

I.e.
cl /clr cppmod.cpp /c
link /NOASSEMBLY /DLL cppmod.obj
csc /t:module csmod.cs /r:cppmod.netmodule
link cppmod.obj csmod.netmodule

Things get of course more complicated if there are
recursive references between the modules.

-hg


Carl Daniel [VC++ MVP]

unread,
Dec 4, 2005, 10:05:28 AM12/4/05
to
Carl Daniel [VC++ MVP] wrote:
> David Ching wrote:
>> Hello, I am just starting to experiment with VC2005 to create a C++
>> WinForms app. I have created a simple form, and it works. Now I
>> want to add a C# .cs source file containing a new class that I
>> downloaded from CodeProject to use in my C++ app. I've tried simply
>> adding the C# source file to my C++ project, but there is no compiler
>> options in the settings. The IDE doesn't seem able to compile the
>> .cs file. Am I able to add source files from other .NET languages
>> such as C# and VB.NET to my C++ project?
>> I did succeed in creating another project for a C# DLL to the same
>> solution, and was able to access the C# class by #using the DLL in my
>> C++ app. But is it required that modules in each language (like C#)
>> be placed in a separate module and be built together? If so, it is
>> kind of inconvenient.
>
> Yes, that's a requirement. You can't build a single DLL or EXE from
> a mix of languages.

Ah so. Apparently you can - see other responses to this thread.

-cd


David Ching

unread,
Dec 4, 2005, 10:50:23 AM12/4/05
to
"Holger Grund" <holger...@remove.ix-n.net> wrote in message
news:O%23f$p4N%23FH...@TK2MSFTNGP12.phx.gbl...

Thanks for the help everyone. But how does my C++ code that uses the C#
class get compiled? How does the C++ compiler "know" what the C# class
looks like so it get generate the correct calls? IOW, how to "#include" a
.cs file?

Also, how to do this in the IDE, and not the command-line?

Thanks,
David


Willy Denoyette [MVP]

unread,
Dec 4, 2005, 5:13:20 PM12/4/05
to

"Holger Grund" <holger...@remove.ix-n.net> wrote in message
news:O%23f$p4N%23FH...@TK2MSFTNGP12.phx.gbl...

This doesn't work:


> csc /t:module csmod.cs /r:cppmod.netmodule

can't work, cppmod.netmodule is not an assembly, you should use
/addmodule:cppmod.netmodule
but even after correcting this, the following link step fails also:
> link cppmod.obj csmod.netmodule
because csmod.netmodule doesn't have a reference to the cppmod.netmodule
(remember csmod calls into cppmod), the linker only resolves managed types
from assemblies or netmodules, not .obj files
Now if you add the cppmod.netmodule to the link command, it will fail with:
fatal error LNK1302: only support linking safe .netmodules; unable to link
pure .netmodule


Willy.


Holger Grund

unread,
Dec 4, 2005, 7:18:15 PM12/4/05
to
"Willy Denoyette [MVP]" <willy.d...@telenet.be> wrote

> This doesn't work:


>> csc /t:module csmod.cs /r:cppmod.netmodule
> can't work, cppmod.netmodule is not an assembly, you should use
> /addmodule:cppmod.netmodule

Oh, I see. But that's a crappy implementation in C#. Netmodules
carry the metadata and I just want to pull the definitions.

"/addmodule" is not what I want. The C++ linker should be
able to resolve metadata references (so there's only one
module with CLR metadata)

Just remove the /NOASSEMBLY and use a reference to

Ok, here's an example (with some trickery to resolve the recursive
dependencies, the entry point should, OC be provided by the
CRT for unsafe code)

// cppmod.cpp
public ref struct CPlusPlus {
static void Func() {
System::Console::WriteLine(__FUNCTION__);
}
};

#ifdef FINAL
#using "csmod.netmodule"
int main(){
CSharp::Main();
}
#endif

// csmod.cs
class CSharp {
internal static void Main() {
System.Console.WriteLine("CSharp.Main 1");
CPlusPlus.Func();
System.Console.WriteLine("CSharp.Main 2");
}
}

// build with
cl /c /clr cppmod.cpp
link /DLL cppmod.obj
csc /t:module /r:cppmod.dll csmod.cs
cl /c /DFINAL /clr cppmod.cpp
link /LTCG cppmod.obj csmod.netmodule

I'm not sure whether the C# can really import assembly
scope definitions (may it can be tricked with the friends attribute).

-hg


Willy Denoyette [MVP]

unread,
Dec 5, 2005, 7:49:02 AM12/5/05
to

"Holger Grund" <h...@remove.ix-n.net> wrote in message
news:%23R2is7S%23FH...@TK2MSFTNGP15.phx.gbl...

> "Willy Denoyette [MVP]" <willy.d...@telenet.be> wrote
>
>> This doesn't work:
>>> csc /t:module csmod.cs /r:cppmod.netmodule
>> can't work, cppmod.netmodule is not an assembly, you should use
>> /addmodule:cppmod.netmodule
> Oh, I see. But that's a crappy implementation in C#. Netmodules
> carry the metadata and I just want to pull the definitions.
>

Not sure if I'm following here, the /r option is used to add a "reference"
to an assembly, but a "netmodule" is not an assembly, it doesn't carry an
"assembly manifest" in it's metadata.
The reason that /addmodule fails to add a 'non-safe' netmodule to an assemby
(module) is that C# doesn't allow you to 'produce' non verifiable assemblies
(modules), I would not call this crapy.

Well, here you built a full assembly (the matadata contains the assembly
manifest), this makes the csc happy, csmod.netmodule only has an external
reference to the cppmod.dll.

> csc /t:module /r:cppmod.dll csmod.cs


> cl /c /DFINAL /clr cppmod.cpp
> link /LTCG cppmod.obj csmod.netmodule
>

Ok, using this trick you can build a mixed mode (non verifiable) assembly.
Note, I did add the following at the end of the build, in order to get rid
of the manifest and netmodule files.

mt -manifest cppmod.exe.manifest /nologo /outputresource:"cppmod.exe;#2"
del *.manifest
del *.netmodule


> I'm not sure whether the C# can really import assembly
> scope definitions (may it can be tricked with the friends attribute).
>

Shouldn't be a problem, see if I have some sample hanging arround.


Willy.


JAL

unread,
Dec 6, 2005, 12:50:01 AM12/6/05
to
Hi Willy... Been busy, but finally tried your code and indeed I can call a
C++/cli netmodule method from C#. I still had a bit of problem calling a C#
netmodule from C++/cli but got it to work.

Thanks!

"Willy Denoyette [MVP]" wrote:

> Sure you can call code in another netmodule, provided your C++ module is a

> "safe" modulfe, all you have to do is add a reference(s) to the 'other'

JAL

unread,
Dec 6, 2005, 12:56:02 AM12/6/05
to
David... Go ahead and compile the C# code. Then launch the VS 2005 command
prompt from the _start_ menu so that the paths are correct. Navigate to the
C#.cs directory and do>csc /t:module MyCS.cs which will generate a
MyCS.netmodule.

Now copy the MyCS.netmodule to the C++/cli project folder where the
MyCPP.cpp code resides. In the C++/cli code add the line:

#using <MyCPP.netmodule>

Now you can just compile and link as>cl /clr MyCPP.cpp and run the program
as>start MyCPP

JAL

"David Ching" wrote:

> ..cs file?

David Ching

unread,
Dec 6, 2005, 10:49:40 AM12/6/05
to

"JAL" <J...@discussions.microsoft.com> wrote in message
news:61324636-84BA-4D96...@microsoft.com...

> David... Go ahead and compile the C# code. Then launch the VS 2005 command
> prompt from the _start_ menu so that the paths are correct. Navigate to
> the
> C#.cs directory and do>csc /t:module MyCS.cs which will generate a
> MyCS.netmodule.
>
> Now copy the MyCS.netmodule to the C++/cli project folder where the
> MyCPP.cpp code resides. In the C++/cli code add the line:
>
> #using <MyCPP.netmodule>
>
> Now you can just compile and link as>cl /clr MyCPP.cpp and run the program
> as>start MyCPP
>
> JAL
>

Thank you everyone! You're the best! I'm glad it is possible to link C#
code into a C++/cli app without creating a separate DLL. It works great! I
can even trace into the C# source code when debugging the C++ app in the
IDE, if I build the .cs file using the /debug switch:

csc/t:module /debug MyCS.cs

Cheers,
David
http://www.dcsoft.com


0 new messages