Search code examples
c#c++com

Calling C# from C++, difficulties with "." in C# namespace


First off, I'll admit I'm cargo-culting this a little bit -- my nice clean sample code doesn't work when I'm wedging it into the real world. That being said...

I have a DLL called CPierce.CSharpCall.dll that has something like the following C# in it:

namespace CPierce.CSharpBridge
{
    [System.Runtime.InteropServices.Guid("3D08DF02-EFBA-4A65-AD84-B08ADEADBEEF")]
    public interface ICSide
    {
        // interface definition omitted...
    }
    [System.Runtime.InteropServices.Guid("CBC04D81-398B-4B03-A3D1-C6D5DEADBEEF")]
    public partial class CSide : ICSide
    {
        // class definition omitted...
    }
}

This is registered with regasm /tlb, etc.. Then, my C++ code looks something like this:

#import "CPierce.CSharpCall.tlb" named_guids

    // Contains syntax errors!
int myfunc()
{
    HRESULT hRes = S_OK;
    CoInitialize(NULL);
    CPierce.CSharpBridge::ICSide *pManagedInterface = NULL;

    hRes = CoCreateInstance(
            CPierce.CSharpBridge::CLSID_Class1, 
            NULL, CLSCTX_INPROC_SERVER, 
            CPierce.CSharpBridge::ICSide, 
            reinterpret_cast<void**> (&pManagedInterface));

    // Calls to the interface omitted....

    CoUninitialize();
    return 0;
}

The problem is, of course, the syntactically wrong bit about CPierce.CSharpBridge. I know in C++ if I want to have a similar namespace to the C# code I could say:

namespace CPierce
{
    namespace CSharpBridge
    {
        // definitions go here
    }
}

But I don't think that's what I'm looking for here, since I just need to refer to two constants that are in another namespace without putting the entire method in that namespace.

What is the C++ syntax I need to complete this call to CoCreateInstance?


Update: On deeper (much deeper) inspection, I'm finding that my .tlb file created by regasm is nearly empty. When I catenated all of my source into a single .cs file and compile with:

csc /debug /t:library BigFile.cs 
regasm BigFile.dll /tlb:BigFile.tlb

I get a hefty (and useful) tlb file.

When I compile the whole project from Visual Studio, I'm getting a .DLL all right, but regasm doesn't do anything with it but produce a minimal .tlb file. (ildasm shows almost no differences between the two DLL's)

If I compile BigFile.cs in Visual Studio, I get a DLL that's also useless.

I'm stumped.


Solution

  • This appears to be a problem on the C#/Visual Studio side. I'll abandon this question and open an appropriate one. Thank you for your help in narrowing it down.