I'm not sure about the namespace issue, but you can call unmanaged C++ from
C# in two ways (that I can think of right now) but both require you to
implement some soft of wrapper to call them through interop.

The first is through COM, by wrapping or implementing your C++ classes as
COM objects.

The second is by creating a standard C Api that wraps your objects.
Generally you would need to provide cdecl function for every public method
of your classes and pass the object as a handle or something in the method,
for example (my C++ may be a little rusty):

class Foo
{

        Foo()
        {
        }

        int Use( int param );

}


extern "C" {
HFOO CreateFoo();
int UseFoo( HFOO h, int param );
};

HFOO CreateFoo()
{
        return (HFOO)( new Foo() );
}

int UseFoo( HFOO hfoo, int param )
{
        Foo* p = (Foo*)hfoo;
        return p->Use( param );
}

If you understand the C++ calling conventions and name mangling you may be
able to call directly into methods without the wrapper but I wouldn't
recommend trying that.

-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Owen Cunningham
Sent: Wednesday, August 10, 2005 10:30 AM
To: [email protected]
Subject: [ADVANCED-DOTNET] Can C# and unmanaged C++ coexist in the same
solution? Same project?

I would like to be able to write an unmanaged C++ class, have it reside
within a namespace already defined in my project/solution, and
instantiate/call it from within C# code in the same namespace.

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to