473,405 Members | 2,338 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,405 software developers and data experts.

Managed/Unmanaged C++ Inheritance

Folks,

I have a bit of a quandary I was hoping for some expert guidance on. Keep in mind I am not a C++ developer, but I have worked with the language a bit.

I have a native C++ API which I am currently wrapping in managed C++ in order to expose the API to .Net clients. So far, so good, everything is working out just fine. However, I have ventured into the callback portion of the API and I am stumped.

In the native API, the callback functionality is abstract. It is left to the consumer to create their class, inheriting from the abstract and coding the actual implementation of the method. Here is the callback.h file from the native API:

Expand|Select|Wrap|Line Numbers
  1. #ifndef Callback_h
  2. #define Callback_h
  3.  
  4. namespace NMSP {
  5.  
  6. class Conn;
  7. class Msg;
  8. /** @class Callback
  9.  * @brief This class is the abstract base class for received message callbacks. 
  10.  * A user created class, derrived from this class, can be passed into 
  11.  * %Subscribe() and %Request() to have user code executed asynchronously
  12.  * when a message is received.
  13.  *
  14.  * Example Callback class:
  15.  * @code
  16.  * class PublishCallback : public Callback
  17.  * {
  18.  *    public:
  19.  *    virtual void CALL_TYPE OnMessage(Conn *conn, Msg *msg)
  20.  *    {
  21.  *        const char *tmp;
  22.  *        msg->ToXML(tmp);
  23.  *        cout << tmp << endl;
  24.  *    }
  25.  * };
  26.  * @endcode
  27.  *
  28.  * Example Callback registration:
  29.  * @code
  30.  * PublishCallback cb;
  31.  * result = conn->Subscribe("NMSP.*.publish",&cb);
  32.  * if( result.isError() )
  33.  *    // handle error
  34.  * @endcode
  35.  *
  36.  * @sa Conn::Subscribe(const char *subject, Callback *cb) @n
  37.  *     Conn::Request(Msg *request, long timeout, Callback *cb)
  38. */          
  39. class API Callback
  40. public:
  41.     virtual void CALL_TYPE OnMessage(Conn *conn, Msg *msg) = 0;
  42. };
  43.  
  44. } //namespace NMSP {
  45.  
  46. #endif  // Callback_h
Now, since my C# (or other .Net clients) developers will need to use the callback mechanism, I need to expose this through the managed layer. To do this, do I:

1. just create a class in managed c++ inheriting from callback? e.g. class ManagedCallback : public Callback? Or,
2. do I create an unmanaged implementation, then create a managed C++ wrapper class for the implementation?

I have been struggling with this for awhile, so any help is greatly appreciated.

Thom
Jul 24 '08 #1
1 2673
Banfa
9,065 Expert Mod 8TB
I don't think you can do 1 so I guess you better do 2.

You will need to read up on Delegates and Events which are the names .NET uses for its call back mechanisms. Delegates are the callback procedures and you register them with an event. Your class will then just raise an event and the users delegates will be called.
Jul 24 '08 #2

Sign in to post your reply or Sign up for a free account.

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...
1
by: Eric Twietmeyer | last post by:
Hello, I'm starting to investigate cs, managed c++ and interoperating with a very large unmanaged code base. We are going to use Windows Forms (written in cs) to replace our old fashioned GUI. ...
5
by: Chris Kiechel | last post by:
I am writing a .NET Windows application and it needs to perform DDE calls to a legacy system. I created a C++ unmanaged class that performs the actual DDE connection and communication. However,...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
3
by: WithPit | last post by:
I am trying to create an managed wrapper but have some problems with it by using abstract classes. In my unmanaged library code i had the following three classes with the following hierarchy ...
3
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple...
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...
2
by: A n g l e r | last post by:
Hello everybody. I've got a bizarre problem concerning inheritance of the managed c++ code when using templates. Consider the following wrapper for the unmanaged c++ class: template...
25
by: Koliber (js) | last post by:
sorry for my not perfect english i am really f&*ckin angry in this common pattern about dispose: ////////////////////////////////////////////////////////// Public class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.