473,325 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Only one type of Unmanaged code?

Hi,

my company is going to migrate a large VC++ application to .NET to
make use of Windows Forms (the old class library is not updated any
more). We are not planning to migrate the rest of the code which works
well.

I understand the basic concept: our code is unmanaged, Windows Forms
is Managed and Unmanaged may not call Managed code. I read about
Wrappers, PInvoke, Runtime Callable Wrappers for COM and about It just
Works (IJW). But I still have some unsolved questions.

1. If I write a managed wrapper for an unmanaged class, I use a
pointer of the unmanaged class in my managed class. Doesn't that make
my Managed Class Unmanaged? In which cases are pointers allowed in
Managed code?

2. If I compile my old code with the /clr switch (IJW) I get MSIL
code, which is unmanaged. Does this mean, that there are two types of
unmanaged code, one native (cpu dependent code), one MSIL? This would
probably answer 4. & 5. as well.

3. Is the __nogc completely optional? It seems that every class
without __gc gets an __nogc implicitly.

4. What does #pragma unmanaged do? Does it have the same effect as
writing __nogc?

5. I can't call Managed Code from Unmanaged Code. But (in the easy
examples) if I have an unmanaged function and a managed function in
the same file, the unmanaged one may call the managed one. Is there a
rule in which cases Unmanaged may call Managed?

6. Our code uses multiple inheritance. Does that mean that it can't be
compiled as Unmanaged MSIL (using IJW, /clr)?

Thanks in advance, those questions keep bugging me.
Kay
Nov 17 '05 #1
1 1588
Sparhawk wrote:
Hi,

my company is going to migrate a large VC++ application to .NET to
make use of Windows Forms (the old class library is not updated any
more). We are not planning to migrate the rest of the code which works
well.
If your program has an elaborate UI built on MFC, you might consider waiting
for VC8, which will have seamless interop between MFC and Windows forms
(e.g. simple hosting of a WinForms control within an MFC window and
vice-versa).
I understand the basic concept: our code is unmanaged, Windows Forms
is Managed and Unmanaged may not call Managed code. I read about
Wrappers, PInvoke, Runtime Callable Wrappers for COM and about It just
Works (IJW). But I still have some unsolved questions.

1. If I write a managed wrapper for an unmanaged class, I use a
pointer of the unmanaged class in my managed class. Doesn't that make
my Managed Class Unmanaged? In which cases are pointers allowed in
Managed code?
No, it doesn't make your class unmanaged. It's a managed class with data
member of value-type (pointer) that happens to point to some unmanaged
memory. As far as the CLR is concerned, it's no different than a 32 bit
integer.
2. If I compile my old code with the /clr switch (IJW) I get MSIL
code, which is unmanaged. Does this mean, that there are two types of
unmanaged code, one native (cpu dependent code), one MSIL? This would
probably answer 4. & 5. as well.
If you compile existing C++ code with /clr you get managed code that
manipulates unmanaged data.
3. Is the __nogc completely optional? It seems that every class
without __gc gets an __nogc implicitly.
That depends on whether you're operating under the influence of #pragma
managed (the default with /clr) or #pragma unmanaged.
4. What does #pragma unmanaged do? Does it have the same effect as
writing __nogc?
Yes. It causes everything not explicitly marked __gc to be implicitly
marked __nogc.
5. I can't call Managed Code from Unmanaged Code. But (in the easy
examples) if I have an unmanaged function and a managed function in
the same file, the unmanaged one may call the managed one. Is there a
rule in which cases Unmanaged may call Managed?
You just named one. That's the principal behind IJW - within an MC++
compiland, managed can call unmanaged and vice-versa and It Just Works.
6. Our code uses multiple inheritance. Does that mean that it can't be
compiled as Unmanaged MSIL (using IJW, /clr)?


No. It means that your inheritance hierarchy cannot be expressed in terms
of the CLR concept of inheritance. Normal C++ code (lacking any __gc
qualifiers), when compiled with /CLR exposes neither inheritance nor even
classes to the CLR. It's simply code targeting a different "machine
language" (the CLI) that has complete control over the data it references.

-cd
Nov 17 '05 #2

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

Similar topics

1
by: JC | last post by:
How does garbage collection work in C# and VB.NET for data returned from COM object? For example, a COM object written in C++ returns a SAFEARRAY to C# or VB.NET, will this SAFEARRAY (mapped to...
1
by: Jón Sveinsson | last post by:
Hello everyone I have been trying to read and write struct to binary files, I'm using to functions to convert the struct to bytes and bytes to struct, I always receive the following error ...
5
by: Gernold Schneider | last post by:
I have a managed code function calling a unmanaged code function which returns a "bool". Obviously the return value is always "true", if i am not running the code in the debugger. If i run the...
2
by: Daniel Mori | last post by:
Hi, Im hoping someone can give me some advice. Im doing some development using VS Whidbey 2005 beta 1. Im about to implement some highly time critical code related to a managed collection of...
1
by: Ghost | last post by:
Hi all, I wrote a program in C# and now I have to "translate" it i visual C++ (MFC) using .NET, but I had a little problem: *ERRORS* error C3181: 'CTestDlg' : invalid operand for __typeof,...
4
by: RedJoy | last post by:
I am switching from VS2003 .NET to VS2005 and I receive the following errorwhen compiling my project: error C3821: 'File': managed type or function cannot be used in an unmanaged function...
3
by: Steve | last post by:
I'm trying to call some unmanaged methods from a DLL. I did this awhile ago, a couple years ago... I'm a little rusty. I will show you what I'm dealing with: <unmanaged function signature>...
4
by: Duncan Smith | last post by:
I have a VS2005 C++ MFC project which #imports a type library. The goal is to introduce some managed code eventually, but for starters I just need to set the /clr compiler option and build the...
3
by: vijay.gandhi | last post by:
Hi, I am trying to convert some unmanaged code (C++) to managed code (using C++/CLI). 1) One of the functions used returns a void* which I need to cast into a handle of a managed object. Can...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.