Mixture of Managed and Unmanaged

  • Thread starter Thread starter The unProfessional
  • Start date Start date
T

The unProfessional

In my current project (my first project using vc w/ managed extensions), I'm
directly #using <mscorlib.dll>, so it's necessary for me to use the __nogc
and __gc constructs when defining classes or structs.

The concept seems simple... some classes are managed, some aren't. I'm just
wondering if there are any major caveats here... anything I need to take
into consideration or keep an eye on.

I'm only using managed extensions so I can harness easy GUI creation... in
order words, no MFC or direct WinAPI stuff. But in terms of logic, I'd like
to keep everything unmanaged.

Is it not as simple as it seems?

Thanks!
 
Hi Bill,

I enjoy using the maanged code, hope you will as well. Unmanaged code
has a couple of
things to watch for:

1) You are responsible for constructor (except default) and destructor
invocation for unmanaged
variables, the CLR takes care of lifetime management for managed objects.

2) The CLR can move managed objects (and thus pointers to them) at any
time [usually
during garbage collection]. Thus you cannot pass a pointer to a managed
object to your unmaanged
class - otherwise the pointer could go stale or worse and your unmanged code
will do [very] bad things.
You need to __pin the pointer before passing it to unmanaged code. The
compiler tries to help
you here by making casts to managed objects cause errors in unmanaged code.
PINned pointers
can be cast easily.

3) The #include files for VC7.x, Win32, and WinDDK have a lot of
conflicts, and really mess up a
lot of legacy code. My solution is to isolate unmanaged code to separate
files so that headers don't
cross between the files. You can declare a forward reference pointer to a
non-managed object from
within your managed code without the compiler complaining -- so that helps
reduce the #include
dependency problem.

-- Tom
 
The said:
In my current project (my first project using vc w/ managed extensions), I'm
directly #using <mscorlib.dll>, so it's necessary for me to use the __nogc
and __gc constructs when defining classes or structs.

The concept seems simple... some classes are managed, some aren't. I'm just
wondering if there are any major caveats here... anything I need to take
into consideration or keep an eye on.

I'm only using managed extensions so I can harness easy GUI creation... in
order words, no MFC or direct WinAPI stuff. But in terms of logic, I'd like
to keep everything unmanaged.

Is it not as simple as it seems?

Thanks!

Yes it is as simple as it seems. This is the power of C++.NET.

Actually this is why I love .NET so much - C++.NET just makes it so
useable in a way which VB6 never ever got even close to.

C# is a great language too - I'd seriously consider writing most of your
app in C# and only use C++.NET to interface with native code (in an
assembly obviously).

David.
 
Back
Top