Managed C++ classes

R

ricky_casson

I'm attempting to create a managed C++ class from some an already
existing application that I have however some of the classes allow
private access via the 'friend' keyword and i've heard that this
presents a problem when creating managed code. Does anyone know how i
could get around this? Is it possible to still create managed code
without having to make the classes managed?
Moreover, what i'm trying to do is make the methods of an API, that
allows the playback of audio through kernel streaming, that has been
written by Microsoft accessible through C#. The application that has
been developed using the API is available here:

http://www.microsoft.com/whdc/hwdev/tech/audio/DirectKS.mspx

My plan has been to export classes used in the API written in C++ into
a managed dll, however, as I have described previously, I am now
having problems with this approach. Does anyone know of any other way
in which this problem could be dealt with?

Cheers very much for any help,

Richard.
 
E

Edward Diener

ricky_casson said:
I'm attempting to create a managed C++ class from some an already
existing application that I have however some of the classes allow
private access via the 'friend' keyword and i've heard that this
presents a problem when creating managed code. Does anyone know how i
could get around this?

You can define a __nogc C++ class which is a nested class of your __gc class
and a pointer to the __nogc class as a private data member of your __gc
class. In the constructor to your __gc class, create your __nogc object
using 'new', assigning the result to the private data member, and pass the
constructor to your __nogc class a pointer to the __gc class. Have the
__nogc class store this pointer as a data member using gcroot<> and you can
now access any data in your __gc class through that pointer ( new C++ 2003
DR which allows a nested class to access its surrounding class's members ala
"friend" ). You can then call functions in your __nogc class from your __gc
class's member functions, and the __nogc class has access to the data of the
__gc class. The __nogc class can use any standard C++ containers, algorithms
and code which it needs. As an example:

// header MyGCClass.h
#include <vcclr.h>
public __gc class MyGCClass
{
public:
MyGCClass();
void AGCMemberFunction();
private:
__nogc class MyCppClass
{
public:
MyCppClass(MyGCClass *);
void ACppMemberFunction();
private:
gcroot<MyGCClass *> GCClass;
};
MyCppClass * CppClass;
int AGCDataMember;
};

// source MyGCClass.cpp
#include "MyGCClass.h"
MyGCClass::MyGCClass() : CppClass(new CppClass(this)),AGCDataMember(0) {}
void MyGCClass::AGCMemberFunction() { CppClass -> ACppMemberFunction(); /*
any other Managed C++ functionality...*/ }
MyGCClass::MyCppClass::MyCppClass(MyGCClass * p) : GCClass(p) { }
void MyGCClass::MyCppClass::ACppMemberFunction() { GCClass -> AGCDataMember
= 1; /* any other C++ standard functionality */ }
 
T

Tony Nassar

Edward,

Nice posting! I'm having a difficult time using STL containers in __gc
classes; I assume that something like this would be the solution?

Tony
 
E

Edward Diener

Tony said:
Edward,

Nice posting! I'm having a difficult time using STL containers in __gc
classes; I assume that something like this would be the solution?

Yes, of course.

Don't even try to use the C++ standard library in __gc classes for the
current VC++ release. You will get compiler errors and even some Internal
compiler errors, which I am sure MS already knows about. Instead use a
__nogc class as I specified and call the appropriate functions there from
your __gc class. From within your __nogc class it is basically the same as
standard C++.

If you need to hold onto pointers to your __gc classes as data members of
your __nogc classes, just use gcroot in your __nogc class. All __delegate,
__property and __event declarations go in your __gc class, but you are free
to call into your __nogc class to do any actual work. Similarly all event
handlers must be in the __gc class but once again you are free to call into
your __nogc class to do the actual work of handling the event.

I am told that the next Whidbey release, in a year or so, will make it
easier to mix standard C++ and managed C++ code in the same __gc class, but
for the time being you need to use the solution I have outlined.
 

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