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

Tech-Archive recommends: Fix windows errors by optimizing your registry




"Holger Grund" <hg@xxxxxxxxxxxxxxx> wrote in message
news:%23R2is7S%23FHA.472@xxxxxxxxxxxxxxxxxxxxxxx
> "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> 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.

> "/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

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.


.



Relevant Pages

  • Re: how to register assembly in global assembly cache successfully?
    ... Visual Studio doesn't look in the GAC for assemblies to reference. ... assemby into global assembly cache. ... can be easily selected as a reference in any .NET project? ...
    (microsoft.public.dotnet.framework.setup)
  • .NET 2.0 unmanaged compilation
    ... I am using Visual Studio 2005 and I am ... reference to a C# assemby, which has a netmodule linked into it. ... Public Overrides Property WidthAs Unit ...
    (microsoft.public.dotnet.languages.vc)
  • .NET 2.0 and VB
    ... I am using Visual Studio 2005 and I am ... reference to a C# assemby, which has a netmodule linked into it. ... Public Overrides Property WidthAs Unit ...
    (microsoft.public.dotnet.languages.vb)