How To: Managed C++ class calling/wrapping C functions and structs 
Author Message
 How To: Managed C++ class calling/wrapping C functions and structs

Hello,

I have started to wrap a multi threaded library written in C with a managed
C++ set of classes.

In particular, a secondary thread is started with a C call and takes a
pointer to a struct as an argument. Using the __pin keyword on the structs
allows me to make this call. However, the struct members of the class I
__pin with a local variables could possibly be moved in memory because the
__pin pointers  go out of scope.

How can I ensure that the structs will not be moved in memory since the C
thread function will exist for a long time (and requires the address of at
least one struct). I cannot use a __pin pointer as a member of the class,
nor can I declare the struct member as a __nogc type.

Any suggestions to keep the C code from blowing up?

Thank you.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

www.identec.com



Tue, 12 Oct 2004 00:19:22 GMT  
 How To: Managed C++ class calling/wrapping C functions and structs
IMHO you make a fundamental mistake here.

You should avoid pinning as much as possible.

Use an unmanaged struct (with a reference to a managed whatever internally)
to receive the calbacks and forward them to the managed area. Do NOT turn
over the pointer to a managed class. When you unload the dll, you can delete
this unmanaged class as well.

BTW - structs are not garbage collected (unless you box them, which you did
not mention). Passing a pointer of an unboxed struct out is something which
is not valid.

--
Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

** Warning: do NOT ever address questions directly to me unless you know me
beforehand.
** IF you do, you will be marked as a junk-mailer and I will not even see
your question anymore.


Quote:
> Hello,

> I have started to wrap a multi threaded library written in C with a
managed
> C++ set of classes.

> In particular, a secondary thread is started with a C call and takes a
> pointer to a struct as an argument. Using the __pin keyword on the structs
> allows me to make this call. However, the struct members of the class I
> __pin with a local variables could possibly be moved in memory because the
> __pin pointers  go out of scope.

> How can I ensure that the structs will not be moved in memory since the C
> thread function will exist for a long time (and requires the address of at
> least one struct). I cannot use a __pin pointer as a member of the class,
> nor can I declare the struct member as a __nogc type.

> Any suggestions to keep the C code from blowing up?

> Thank you.

> --

> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

> www.identec.com



Tue, 12 Oct 2004 00:38:21 GMT  
 How To: Managed C++ class calling/wrapping C functions and structs
Hi Thomas,

Thank you for the advice. I knew that pinning wasn't the answer (because the
pointers go out of scope). However, I am a tad confused.

Are you suggesting the following?
-Create an unmanaged class to encapsulate the C functions
    -this class has struct members required by the C functions that are also
unmanaged
    -have a member of this class be a pointer to a managed class

I don't understand how I can expose methods in the managed class that will
call the unmanaged functions to do the actual work.

Note, you say that the structs are not garbage collected, yet my class
encapsulating the structs could be garbage collected (or possibly moved in
memory). Does that not in turn garbage collect the struct members? Will that
not affect the location in memory of the structs and thus what the C
functions are relying on?

Please advise.

-Chris



Quote:
> IMHO you make a fundamental mistake here.

> You should avoid pinning as much as possible.

> Use an unmanaged struct (with a reference to a managed whatever
internally)
> to receive the calbacks and forward them to the managed area. Do NOT turn
> over the pointer to a managed class. When you unload the dll, you can
delete
> this unmanaged class as well.

> BTW - structs are not garbage collected (unless you box them, which you
did
> not mention). Passing a pointer of an unboxed struct out is something
which
> is not valid.

> --
> Thomas Tomiczek
> THONA Consulting Ltd.
> (Microsoft MVP C#/.NET)

> ** Warning: do NOT ever address questions directly to me unless you know
me
> beforehand.
> ** IF you do, you will be marked as a junk-mailer and I will not even see
> your question anymore.



Tue, 12 Oct 2004 00:59:31 GMT  
 How To: Managed C++ class calling/wrapping C functions and structs
Hello, inline with ***

--
Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

** Warning: do NOT ever address questions directly to me unless you know me
beforehand.
** IF you do, you will be marked as a junk-mailer and I will not even see
your question anymore.


Quote:
> Hi Thomas,

> Thank you for the advice. I knew that pinning wasn't the answer (because
the
> pointers go out of scope). However, I am a tad confused.

> Are you suggesting the following?
> -Create an unmanaged class to encapsulate the C functions
>     -this class has struct members required by the C functions that are
also
> unmanaged

No, I just suggest that you dont have the struct as a managed struct. You
can mix managed/unmanaged whatever as you wish. And you can create
singletons etc. Static global variables. Everything yo ucan do with C++.

I can not advice on specific design without knowing how the C level
interface looks.

But yes, 1st thing I would do is make a class having all the functions in
there - the "object" representation of the lower DLL.

Quote:
>     -have a member of this class be a pointer to a managed class

Simple. DO IT. Managed classes can have unmanaged pointers.

Quote:
> I don't understand how I can expose methods in the managed class that will
> call the unmanaged functions to do the actual work.

Expose functions? You definitly know how to do this, or?

Quote:
> Note, you say that the structs are not garbage collected, yet my class
> encapsulating the structs could be garbage collected (or possibly moved in
> memory). Does that not in turn garbage collect the struct members? Will
that
> not affect the location in memory of the structs and thus what the C
> functions are relying on?

Hm, would it do in C++? What about a global ariable holding a pointer to
this struct?

Quote:
> Please advise.

Not without the exact API. Specific advice can not be given blind. Give
samples.


Tue, 12 Oct 2004 01:53:38 GMT  
 How To: Managed C++ class calling/wrapping C functions and structs
Hi Thomas,

Thank you for taking the time to reply to my cries for help.

I'll simplifiy it.

We have already written a C++ wrapper, so the OO design is not an issue. We
cannot use global varaiables for the structures because more than one
"iCard" may be connected to the computer. For reference, the "iCard" is a
proprietary hardware device we make and it communicated with via our C
library through a serial port or PCMCIA slot. By wrapping the C structs with
classes (and methods) the structs are no longer the application programmer's
management.

So here's the question:
If I have a managed class, can I have a pointer to an unmanaged class as a
member? For example the object this unmanaged pointer points to will be
instantiated in the managed class' constructor and deleted in the
destructor. (Here's the important part) The *unmanaged* class will
encapsulate the structs, and as such are we *guaranteed* that these structs
will not be moved around in memory? If so, then that is all I need to know,
and our architecture should be fine.

Thanks again,
-Chris



Tue, 12 Oct 2004 04:38:46 GMT  
 How To: Managed C++ class calling/wrapping C functions and structs
Inline with ***

--
Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

** Warning: do NOT ever address questions directly to me unless you know me
beforehand.
** IF you do, you will be marked as a junk-mailer and I will not even see
your question anymore.


Quote:
> Hi Thomas,

> Thank you for taking the time to reply to my cries for help.

> I'll simplifiy it.

> We have already written a C++ wrapper, so the OO design is not an issue.
We
> cannot use global varaiables for the structures because more than one

And? What about a dynamically reallocated or statically allocated array?

Still not enough information :-) For example, I did the same with CAPI, and
there the number of ISDN cards is hardcoded in the specs - the max number.

Quote:
> "iCard" may be connected to the computer. For reference, the "iCard" is a
> proprietary hardware device we make and it communicated with via our C
> library through a serial port or PCMCIA slot. By wrapping the C structs
with
> classes (and methods) the structs are no longer the application
programmer's
> management.

> So here's the question:
> If I have a managed class, can I have a pointer to an unmanaged class as a
> member? For example the object this unmanaged pointer points to will be

Yes.

Quote:
> instantiated in the managed class' constructor and deleted in the
> destructor. (Here's the important part) The *unmanaged* class will
> encapsulate the structs, and as such are we *guaranteed* that these
structs
> will not be moved around in memory? If so, then that is all I need to

know,

Yes, AS LONG as the unmanaged class has only an unmanaged pointer and does
not "inline" the struct. Dynamic allocation again.

Unmanaged things are handled exactly the same as before. Pure boring
standard C++.

So if the managed class has an unmnaged pointer to the struct

(sth like structtype __nogc *variable if I recall this right)

then you need to dynamically allocate the memory and it is taken from the
C++ memory. No garbage collection (be aware of memory leaks - use the class
deconstructor).

Regards

Thomas



Tue, 12 Oct 2004 14:06:21 GMT  
 How To: Managed C++ class calling/wrapping C functions and structs
Thanks,

I'll see what happens.

-Chris

Quote:
> Yes, AS LONG as the unmanaged class has only an unmanaged pointer and does
> not "inline" the struct. Dynamic allocation again.

> Unmanaged things are handled exactly the same as before. Pure boring
> standard C++.

> So if the managed class has an unmnaged pointer to the struct

> (sth like structtype __nogc *variable if I recall this right)

> then you need to dynamically allocate the memory and it is taken from the
> C++ memory. No garbage collection (be aware of memory leaks - use the
class
> deconstructor).

> Regards

> Thomas



Wed, 13 Oct 2004 00:05:32 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. C# client crashs when calling into Managed C++ which calls unmanaged c++ function

2. Wrapping MCF classes in Managed c++ library

3. Help me!! wrap unmanaged class with managed c++

4. Calling classes in other cs files

5. Newbie: separate big .cs file into small .cs files

6. Calling Managed C++ function from C# with array parameters

7. Calling Managed C++ function from C# with array parameters

8. How To: Wrapping a C library in Managed C++

9. STRUCT IN CS!!

10. Managed access to C++ class by modifying existing C++ classes rather than wrapping it.

11. How to call managed C++ DLL from unmanaged C++ EXE

12. managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception

 

 
Powered by phpBB® Forum Software