473,569 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ 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 2177
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.b et 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
2812
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 (__fastcall etc.). I have tried disabling "compile as managed" option for these specific files, but it does not help: by trial and error I figured out that...
2
489
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 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...
4
1684
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
6453
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 2) or is it a bug that the class 'WillNotCompile' will not compile?. #include "stdafx.h" #include <iostream> #include <memory> using namespace...
13
2240
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 code for the listener that launches the listener thread. Managed ----------
2
4434
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 unmanaged, where the form is managed). Can I not have an unmanaged instance in a managed class?
3
2176
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 adding row to datagrid , some varibles (vectors etc) in unmanaged class are cleared or filled with null. I want mention also when i compile only...
3
4694
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 adding row to datagrid , some varibles (vectors etc) in unmanaged class are cleared or filled with null. I want mention also when i compile only...
2
16841
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 here.
2
12350
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 classes in managed C++ as a wrapper to the unmanaged C++ classes or is there an easier way? Essentially what I have done is written a C++ kernel...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2117
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 we have to send another system
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.