473,322 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Managed C++ classes

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/.../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.

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 17 '05 #1
3 4315
ricky_casson wrote:
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 */ }
Nov 17 '05 #2
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
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:

Nov 17 '05 #3
Tony Nassar wrote:
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.

Tony
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:

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
6
by: Shai Levi | last post by:
Hi, I'm trying to migrate native c++ class to managed c++ class. The native class header definition looks as: class NativeClass { public: typedef void (CbFunc1)(int n,void* p);
10
by: Edward Diener | last post by:
The documentation states the names of the various managed operators but does not give the signature for them. Is there some documentation which I have missed that gives the correct signature ? In...
10
by: E.T. Grey | last post by:
Hi, I have a C++ DLL that I want to use from a C# project. I am actually usng a lot of advanced C++ features like templates, partial/specialized templates, functors and callbacks. I am also...
2
by: Steven Cool | last post by:
Hi, DA PROBLEM: Once I wrote a c++ dll. I wanted to use that dll in my new c# project, so I compiled it with the CLR option. The compilation was ok. Like I said, I wanted to use the dll (with...
5
by: Andrew | last post by:
I want to use a managed c++ class from an unmanaged class. Here is my code: // *** Unmanaged Code // .h file class UnmanagedClass { public: // Other stuff here
13
by: bonk | last post by:
Hello, I am trying to create a dll that internally uses managed types but exposes a plain unmanaged interface. All the managed stuff shall be "wrapped out of sight". So that I would be able to...
9
by: Herby | last post by:
Is possible to have a managed method within a Native(un-managed) class within a \clr project? E.g. class myClass { public: #pragma managed void myMethod(void);
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.