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

managed/unmanaged interop critique ...

Hi,

I am faced with the following common managed/unmanaged C++ interop
problem:

- I have a .NET form that acts as a front end GUI to a processing
engine in the background. The processing engine runs as a thread that
is managed by the front-end form class.
- The processing engine must have a callback mechanism to update the
form about progress, and to send status messages that will be displayed
by the GUI.

I am looking for the *simplest* solution that involves least changes to
the processing engine code and doesn't involve writing wrappers for
everything. Based on reading previous posts from this newsgroup, I
ended up with the following pattern:

// Form.h

class Form
{
// controls.

// event handlers.
void btnStart_Click(...);

// GUI status update functions.
void updateStatus(...);

};

---------
// Form.cpp

// the engine object - global variable :-(
Engine myEngine;

// form function implemetations.
void run_engine()
{
try
{
// ... setup the engine ...
myEngine.set_listener(this);

myEngine.process();

// ... cleanup the engine ...
}
catch(/* any exception thrown by Engine */)
{
// exception handler code
}
};
---------
// Engine.h
#include <vcclr.h>

public __gc class Form;

class Engine
{
gcroot<Form *_pListener;

// engine related variables.

public:

void set_listener(gcroot<Form *pListener);
// engine related member functions.
};

--------
// Engine.cpp

#include "Engine.h"
#include "Form.h"

#define STATUS(x) {/*format the message*/;_pListener->updateStatus();}
void Engine::process()
{
// processing code.
STATUS(("Running test %d of %d.", ...));
// processing code.
}

The method is low-impact because the only changes I did to the Engine
source code were to add 3-4 lines to setup the listener inside the
class, and change the status "#define" to update the status in the
listener. The rest of the code remains untouched and no wrappers were
written.

But, I am unable to have a Engine object as a member variable of the
Form class because of the presence of <gcrootin the Engine class. I
worked around it by instantiating Engine as a global variable (yuk !)
in Form.cpp, but it *works* so far.

I'd appreciate it if the .NET gurus in this group would make a
constructive critique of my pattern, and/or make any suggestions to
make it more elegant. Is it possible to follow the same pattern, but
avoid the global variable (or is wrappers the only way to go) ?

Thanks,
Vijay.

Sep 17 '06 #1
6 1602
I am faced with the following common managed/unmanaged C++ interop
problem:
...
But, I am unable to have a Engine object as a member variable of the
Form class because of the presence of <gcrootin the Engine class. I
worked around it by instantiating Engine as a global variable (yuk !)
in Form.cpp, but it *works* so far.
And what's a problem with referring to Engine in Form class?
Sep 18 '06 #2
I am getting compiler errors telling me that I cannot use declare an
Engine object as a member function of Form, because of the presence of
gcroot<in the Engine class.

Thanks,
Vijay.

PS: I am a newbie to the .NET platform ...
Vladimir Nesterovsky wrote:
I am faced with the following common managed/unmanaged C++ interop
problem:
...
But, I am unable to have a Engine object as a member variable of the
Form class because of the presence of <gcrootin the Engine class. I
worked around it by instantiating Engine as a global variable (yuk !)
in Form.cpp, but it *works* so far.

And what's a problem with referring to Engine in Form class?
Sep 18 '06 #3
>I am getting compiler errors telling me that I cannot use declare an
Engine object as a member function of Form, because of the presence of
gcroot<in the Engine class.

Thanks,
Vijay.

PS: I am a newbie to the .NET platform ...
Can we see a strip of code that fails to compile?

--
Vladimir Nesterovsky
Sep 18 '06 #4
Oops ! I was mistaken. The reason the code did not compile was because
I had elements of the standard library (std::vector, specifically) as
members of engine. The precise error message I am getting is :

error C3633: cannot define 'engine' as a member of managed 'myForm'
because of the presence of destructor 'std::vector<_Ty>::~vector' on
class 'std::vector<_Ty>' with
[
_Ty=int
]
and
[
_Ty=int
]
and
[
_Ty=int
]
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\vector(386) : see declaration of
'std::vector<_Ty>::~vector'
with
[
_Ty=int
]
and
[
_Ty=int
]

Sorry,
Vijay.

Vladimir Nesterovsky wrote:
I am getting compiler errors telling me that I cannot use declare an
Engine object as a member function of Form, because of the presence of
gcroot<in the Engine class.

Thanks,
Vijay.

PS: I am a newbie to the .NET platform ...

Can we see a strip of code that fails to compile?

--
Vladimir Nesterovsky
Sep 18 '06 #5
Oops ! I was mistaken. The reason the code did not compile was because
I had elements of the standard library (std::vector, specifically) as
members of engine. The precise error message I am getting is :

error C3633: cannot define 'engine' as a member of managed 'myForm'
because of the presence of destructor 'std::vector<_Ty>::~vector' on
class 'std::vector<_Ty>' with

You can define engine as a pointer to Engine in your managed class.
Don't forget to create it in constructor and free it in Dispose of the form.
--
Vladimir Nesterovsky
Sep 19 '06 #6
Thanks very much for the suggestion !

-Vijay.

Vladimir Nesterovsky wrote:
Oops ! I was mistaken. The reason the code did not compile was because
I had elements of the standard library (std::vector, specifically) as
members of engine. The precise error message I am getting is :

error C3633: cannot define 'engine' as a member of managed 'myForm'
because of the presence of destructor 'std::vector<_Ty>::~vector' on
class 'std::vector<_Ty>' with


You can define engine as a pointer to Engine in your managed class.
Don't forget to create it in constructor and free it in Dispose of the form.
--
Vladimir Nesterovsky
Sep 19 '06 #7

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

Similar topics

16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
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. ...
1
by: Zapbbx | last post by:
I have a 3rd party application that can reference external dll's. The dll's have to be written in unmanaged code with an exported function I can reference and call. I would like it to call a C# dll...
3
by: _BNC | last post by:
I have an old C DLL that I want to access via C#. I'm doing this via an outer DLL that wraps the old C DLL in an unmanaged C++ class, which is in turn wrapped in a Managed C++ class. Both these...
3
by: Tommy Svensson \(InfoGrafix\) | last post by:
I've been instructed to work againt a huge unmanaged C++ API from a C# application. Now, the only way, as I've understood it, is to go the Managed Extensions for C++ way. This means I have to...
1
by: Ryan Melville | last post by:
Hi, I need to use WIA (Windows Image Acquisition) from managed C++. Is there a "new and improved" way to access WIA from managed C++ (i.e., through .net)? Or, is it the same COM calls as from...
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...
5
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have a class that is writen in unmanaged pure native C++. This class files (h and cpp) are inserted to a managed C++ (VC++ 2005, C++/CLI) DLL compoenet. This DLL compoenet is used in a C#...
7
by: A n g l e r | last post by:
Hi all. Can you please tell me if you have any problems to use managed C++ DLL libraries that wrap some code from unmanaged external DLL 32 bit libraries? If you called such managed 32 bit DLL...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.