Thanks for the quick response, Bob.

I may have made this more complicated than it needs to be.

I don't really need a full unmanaged C++ class -- I just need an
unmanaged C++ function. I am at a point where I could build a separate
DLL that exports the function and P/Invoke it from C#, but I really want
to avoid having two projects. I'd like to just be able to have my
project Foo contain two source files, Foo.cs and Foo.cpp.

So I guess my question is, what sort of keywords/directives, if any,
would I need to put in my .CPP file to let the compiler/linker make my
unmanaged function public? And what keywords/directives would I need to
put in my .CS file to let my C# code reference the unmanaged function?

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

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(r)  http://www.develop.com

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

===================================
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