473,498 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help - New to Managed C++ Question

Hi

I am an experienced C programmer and we have a large app written in C which
I have just recompiled with /clr to start to add in managed code to use .NET
framework and windows forms bit by bit.

I am new to managed C ++(and C++ for that matter).

I am struggling with the fact that it seems you cannot have global managed
variables although I think I understand why you can't.

i.e. if I declare a managed type or class or object as I would a normal C
global variable, I get a complier error:

error C3145 cannot declare a global or static managed type object or an _gc
pointer.

My big problem is I initially want to write some managed C++
functions/classes in a new C++ file which I then want to call from my
existing C code. However these new managed C++ functions need to preserve
data between themselves and between calls from the C code so it seemed
global variables where the ideal solution.

So the scenario is something like

C code

call managed C++ function 1 to initialize data

loop
do some processing
call managed C++ function 2 to work with initialized data
end loop

C++ code

function1
creates some managed data (but where does it preserve this if I cant have
global managed variables)

funtion2
works on previously created managed data

Can anyone advise on how best to tackle this problem.

Thanks

Steve

Nov 17 '05 #1
3 1603
Below is a very very simple example of my problem (in reality things are
more complicated obviously but the principle is the same)

I want to call functions test to initialize the data and test 2 to retrieve
it later from my C code which obviously requires that dog is preserved
between calls

In my C++ file I have:

__gc class animal
{
public:
int legs;
};

extern "C" void test()
{
animal* dog;
dog=new animal;
dog->legs=4;
}

extern "C" int test2()
{
return dog->legs;
}

Firstly this obvioulsy doesn't compile as test2 doesnt know about dog. A
global; variable would solve it but if I declare dog as variable I get the
C3145 compiler error.

Secondly, won't dog be garbage collected once test has been called by by C
code so that when I later call test2 dog doesnt exist anyway.

It seems to me a global variable would solve all this but managed C++ won't
allow it.

Steve
Nov 17 '05 #2
Hi,

You could make a class that initialises and holds 'dog' and makes it
statically available. MC++ has static constructors, so you can use that to
ensure you never access dog before it's been initialised.

Steve

"Steve Marsden" <st***@conquest.ltd.uk> wrote in message
news:Of**************@TK2MSFTNGP09.phx.gbl...
Below is a very very simple example of my problem (in reality things are
more complicated obviously but the principle is the same)

I want to call functions test to initialize the data and test 2 to
retrieve it later from my C code which obviously requires that dog is
preserved between calls

In my C++ file I have:

__gc class animal
{
public:
int legs;
};

extern "C" void test()
{
animal* dog;
dog=new animal;
dog->legs=4;
}

extern "C" int test2()
{
return dog->legs;
}

Firstly this obvioulsy doesn't compile as test2 doesnt know about dog. A
global; variable would solve it but if I declare dog as variable I get the
C3145 compiler error.

Secondly, won't dog be garbage collected once test has been called by by C
code so that when I later call test2 dog doesnt exist anyway.

It seems to me a global variable would solve all this but managed C++
won't allow it.

Steve

Nov 17 '05 #3
Steve

Thanks for the reply. Could you expand on this a bit please. I'm not sure
exaclty what you mean.

Thanks

Steve

"Steve McLellan" <sjm AT fixerlabs DOT com> wrote in message
news:OB**************@tk2msftngp13.phx.gbl...
Hi,

You could make a class that initialises and holds 'dog' and makes it
statically available. MC++ has static constructors, so you can use that to
ensure you never access dog before it's been initialised.

Steve

"Steve Marsden" <st***@conquest.ltd.uk> wrote in message
news:Of**************@TK2MSFTNGP09.phx.gbl...
Below is a very very simple example of my problem (in reality things are
more complicated obviously but the principle is the same)

I want to call functions test to initialize the data and test 2 to
retrieve it later from my C code which obviously requires that dog is
preserved between calls

In my C++ file I have:

__gc class animal
{
public:
int legs;
};

extern "C" void test()
{
animal* dog;
dog=new animal;
dog->legs=4;
}

extern "C" int test2()
{
return dog->legs;
}

Firstly this obvioulsy doesn't compile as test2 doesnt know about dog. A
global; variable would solve it but if I declare dog as variable I get
the C3145 compiler error.

Secondly, won't dog be garbage collected once test has been called by by
C code so that when I later call test2 dog doesnt exist anyway.

It seems to me a global variable would solve all this but managed C++
won't allow it.

Steve


Nov 17 '05 #4

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

Similar topics

7
3284
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
2
1971
by: Andrew S. Giles | last post by:
OK, Ive run my head into this wall for too long. I need help. I am developing an applicaiton in C# to present a user with a GUI to specify a configurable list of machines that he wants to listen...
0
840
by: Rick | last post by:
We have an class sent to us by a vendor in a .h file. This class controls access to a subsystem for gathering data. Currently we are using C++ 6.0 to access this class. Now we want to move to .net....
5
1320
by: achutha.sridhar | last post by:
I've a pretty basic question on managed code. I read through couple of.net documentation, and reached a state where I know that managed code would be run by the .Net run time and it has so and so...
1
6891
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...
0
7381
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
5465
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,...
1
4916
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...
0
4595
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
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
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...

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.