473,473 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Mixing managed C++ with unmanaged C++

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
Nov 22 '05 #1
2 2164
Neil wrote:
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/de...omixedmode.asp

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

Richard
--
my email ev******@zicf.bet is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/
Nov 22 '05 #2

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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 22 '05 #3

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

Similar topics

0
by: kaalus | last post by:
Hello I have problem mixing managed and unmanaged C++ code in VS.NET 2003. I want to create a windows forms application which uses some C++ code that is incompatible with managed extensions...
2
by: Neil | last post by:
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...
4
by: Daniel Lidström | last post by:
Hello, is it possible to ``use CS to code a class, and (M)C++ for the logic''? Sorry if this sounds vague, I'm not really sure myself what it means. Does anyone have any ideas? -- Daniel
14
by: Jon | last post by:
Whether I can compile a class or not, depends on the order of functions in my class. My question is (see example below): 1) Is it a bug that the class 'WillCompile' will compile and execute, or...
13
by: DD | last post by:
I'm puzzled how to mix managed/unmanaged C++ in the following scenario: Unmanaged ------------ Callback listener class with method that should call an event in managed Form1. Initialization...
2
by: quat | last post by:
I am getting the error: error C4368: cannot define 'd3dPP' as a member of managed 'FormEx::Form1': mixed types are not supported I am trying to mixed managed and unmanaged code (d3dPP is...
3
by: frank | last post by:
Hi I've got aplication, which one is written in unmanaged c++ with stl, i've made for it gui in managed c++. Problem becomes when I'm starting to filling up for example datagrids, when I'm...
3
by: frank | last post by:
Hi I've got aplication, which one is written in unmanaged c++ with stl, i've made for it gui in managed c++. Problem becomes when I'm starting to filling up for example datagrids, when I'm...
2
by: jraul | last post by:
Hi, This is probably a noobie question but: I just created a new C++/CLI project in VS 2005. It created an empty class: public ref class Class1 { // TODO: Add your methods for this class...
2
by: Jon Slaughter | last post by:
How difficult is it for one to integrate unmanaged C++ into C#? I know for functions one can use DLLimport but how does one go about doing it for classes? Do I have to completely reimplement the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.