473,811 Members | 2,851 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 1619
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******** ******@TK2MSFTN GP09.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******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP09.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
3312
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 buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
2
1994
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 to the output of. Specify a filename to shove all of the data (into Excel), and start the whole thing going. I get that done no problem. The problem comes with the Data. The data is coming from a different application, and I am not 100% sure of...
0
853
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. The class sent to us can not be compiled as managed code so I have built a managed wrapper class... here is the general code #using <mscorlib.dll> #pragma once
5
1330
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 advanantages My question is if I've a unmanaged custom dll(say a.dll) built using VC++ 6.0 and if I were to create a wrapper class in .Net(say b.dll) to invoke the unmanaged dll, will my b.dll become a managed code or still it is unmanaged? As...
0
9722
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10393
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10124
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6882
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3015
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.