Namespace with managed C++ DLL and C# usage [Try #3]

M

Mr Topom

Hello,

I have a C++ (unmanaged) class named K in a DLL.
I create a managed C++ class named K in an other DLL that wrapped the
unmanaged one.

To refer the unmanaged one I use ::K and the managed one is in a
namespace MyNameSpace.

If want to use my managed DLL in a C# project,so I added : #using
Mynamespace;

If in the code I put :
K klass = new K;
There is compilation error. It seems that le K sybol is the unmanaged
one and identifyed as a 'struct'.

Is there a solution to remove the knowledge of the unmanged symbol to be
able to refer K in C# with only its name and not Mynamespace.K ?

Thx
 
K

Kapil Khosla [MSFT]

Is there a solution to remove the knowledge of the unmanged symbol to be
able to refer K in C# with only its name and not Mynamespace.K ?

I tried your example with beta2 toolset and I am able to call into the
managed assembly and dont get the compilation error. This is what I did.

cl /EHsc /LD def.cpp
cl /clr /LD m.cpp /link def.lib
csc /r:m.dll t.cs


////////////////def.cpp
#include <stdio.h>
class __declspec(dllexport) Native
{
public:
void foo()
{
printf("native foo called\n");
}
};


////////////////m.cpp

using namespace System;
class __declspec(dllimport) Native
{
public:
void foo();
};

namespace N
{
public ref class Native
{
public:
void foo()
{
Console::WriteLine("N::Native::foo");
}
};
}

///////////////////t.cs

using System;
using N;

class Test
{
static void Main()
{
Native obj = new Native(); // No compilation error
obj.foo(); // foo in namespace N called
}
}



Does this work for you? If not, can you tell me the compiler you are using
so that we can try to find what is going on?
Thanks,
Kapil
 
M

Mr Topom

Hello,

Thx for the answer.

I just tested and it works... But If I modify the managed class like
that :

namespace managedDll
{
public __gc class Native {
protected:
// Put the line below in comment and everything works
fine
::Native __nogc *unmanagedNativePtr; // <--- Reference
to the unmaged class

public:
void foo();
};
}

I have a compile problem and Native in the C# code is highlighted as
struct.

I am with VC 2003 and I can't change the compiler...

Any Idea ?
 
D

Denny Huber

Kapil:

I tried this under VS8 Beta2 and I'm confused. Why is there a class in
def.cpp called Native and a class in m.cpp also called Native?

When I build this, I see "N::Native::foo". I would like to see "native foo
called".

I would like to instantiate an object of the class Native that lives in
def.cpp and run it inside N::Native::foo(). Can this be done?

Thanks,
Denny Huber
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top