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 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?
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
"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.
"JAL" <J...@discussions.microsoft.com> wrote in message
news:DAED7CD7-B854-4971...@microsoft.com...
> 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
Ah so. Apparently you can - see other responses to this thread.
-cd
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
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.
> 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
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.
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'
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?
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