Mixing managed C++ with unmanaged C++

N

Neil

I am developing a demo in C# using Managed DirectX. I wanted to use a
C++ static library in the demo (its a class for handling physics), so
I decided to create my Graphics classes in C#, inherit from them in
MC++. So far so good......

I modified my MC++ project to run as mixed mode (removed nochkclr.obj,
added msvcrt.lib, etc etc), and created 2 unmanaged classes which
handled the calls to the static lib (btw all the code works fine in
other unmanaged C++ projects - i just copied it straight out) . I then
created managed wrappers around them. Again so far so good......

BUT THEN!

When I try and call the code from C++ it falls over. I get a null
object exception. Now my unmanaged classes use some static members,
and if I remove the calls to static member variables it all runs OK -
obviously it doesnt do what I want, but at least it doesnt crash. If I
put them back in it fails.

I debugged the code to see if my suspicions were right - it seems the
static members are being reset - eg (c#)(btw these arent my class
names - changed for clarity)

MyGFXSys mgfxs= new MyGfxSys(); //calls unmanaged constructor in
here which initialises static members - shows in the debugger fine.

MyGFXObject obj3d = new MyGFXObject() //calls unmanaged constructor
in here which accesses the static member from above. This call fails -
static members variables appear un-initialised.

The static members I refer to are part of the UNMANAGED class btw, in
case that makes a difference. I also tried making them (ugh) global,
and got the same effect.

OK, I know what I'd be thinking about now - why not find another way
to do it without static members or globals? Unfortunately the static
lib makes use of these, and I dont have and cant get the source for
this - so I need to solve this.

Any ideas, help, or simply prayers would be much appreciated.

Cheers

Neil
 
R

Richard Grimes [MVP]

Neil said:
When I try and call the code from C++ it falls over. I get a null
object exception. Now my unmanaged classes use some static members,
and if I remove the calls to static member variables it all runs OK -
obviously it doesnt do what I want, but at least it doesnt crash. If I
put them back in it fails.

You've answered yout own question. The CRT is responsible for calling static
constructors. Have you looked at the docs that mention how tro initialize
the CRT from outside the library?

http://msdn.microsoft.com/library/d...tsfrompureintermediatelanguagetomixedmode.asp

(becareful about line wrapp - this must be the longest name for an HTML page
I have ever seen!)

Richard
 
N

Neil Danson

Unfortunately I followed that document. I ave another project which does
a similar thing, but I could only get it to work when I link to libc.lib
too. Unfortunatley this isnt an option as the static lib I'm using
requires that libc is excludded fro the build as qsort is defined in the
static lib file I'm using.

I've noticed in my project that works (using zlib) that the link order
makes adifference to whether it links correctly or fails. Is this a
feature or a bug?

Regards

Neil Danson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top