Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mr Topom - 25 Apr 2005 15:41 GMT
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
Kapil Khosla [MSFT] - 25 Apr 2005 16:38 GMT
> 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

Signature

This posting is provided "AS IS" with no warranties, and confers no
rights."Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"

> Hello,
>
[quoted text clipped - 17 lines]
>
> Thx
Mr Topom - 26 Apr 2005 11:29 GMT
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 ?
Denny Huber - 02 Jun 2005 22:03 GMT
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

>> 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 ?
[quoted text clipped - 78 lines]
>>
>> Thx

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.