Conversion to MC++???
-
I am going thru some code conversion and having bad time with MC++. Can anyyou help me with this? There is an activeX control which is to be converted for .net. __gc class Rinex{ } class CRinex_ControlCtrl : public COleControl { Rinex *x; // This is an error as CRinex_controlctl is unmanaged class. } I thought all comp generated classes are managed. But if i make it managed with __gc, all the code (comp generated) has to be changed. Do you know how to avert this problem? One other problem is converting from a variable from char [] to string? How do you convert? There is a sample code below. char x __gc[]; x[]=new x __gc[10]; int i= strlen(x); // This gives an error. Even a type-cast does not work.Thanks for you help.
-
I am going thru some code conversion and having bad time with MC++. Can anyyou help me with this? There is an activeX control which is to be converted for .net. __gc class Rinex{ } class CRinex_ControlCtrl : public COleControl { Rinex *x; // This is an error as CRinex_controlctl is unmanaged class. } I thought all comp generated classes are managed. But if i make it managed with __gc, all the code (comp generated) has to be changed. Do you know how to avert this problem? One other problem is converting from a variable from char [] to string? How do you convert? There is a sample code below. char x __gc[]; x[]=new x __gc[10]; int i= strlen(x); // This gives an error. Even a type-cast does not work.Thanks for you help.
If you want to contain an instance of a managed object inside an unmanaged class (per your example) you need to use gcroot<>. gcroot is a template class (#include either vcclr.h or gcroot.h) that wraps the functionality of GCHandle. GCHandle is a .NET Framework class that tells the garbage collecter there is an instance of a class pointed to by memory outside its control (likely the C++ heap, in this example).
__gc class Rinex
{
};class CRinex_ControlCtrl
{
gcroot x;
};When you compile with /clr the compiler will generate all code as MSIL, but no datatypes are magically made into managed types. When you pass managed data to an unmanaged function (as you are attempting to do with your strlen() problem), you must first pin the data so that the garbage collector doesn't move it during the call:
char x __gc[];
x = new char __gc[10];char __pin* pinned_x = &x[0];
int i= strlen(pinned_x);You can read more about pinning in an article I wrote, here. Cheers, Nick This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.
-
If you want to contain an instance of a managed object inside an unmanaged class (per your example) you need to use gcroot<>. gcroot is a template class (#include either vcclr.h or gcroot.h) that wraps the functionality of GCHandle. GCHandle is a .NET Framework class that tells the garbage collecter there is an instance of a class pointed to by memory outside its control (likely the C++ heap, in this example).
__gc class Rinex
{
};class CRinex_ControlCtrl
{
gcroot x;
};When you compile with /clr the compiler will generate all code as MSIL, but no datatypes are magically made into managed types. When you pass managed data to an unmanaged function (as you are attempting to do with your strlen() problem), you must first pin the data so that the garbage collector doesn't move it during the call:
char x __gc[];
x = new char __gc[10];char __pin* pinned_x = &x[0];
int i= strlen(pinned_x);You can read more about pinning in an article I wrote, here. Cheers, Nick This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.
-
But Isn't it true that all the default classes in MFC (I mean, the computer generated classes) are managed. If not, how does garbage collection occur for such classes? Thanks Yogi
No; MFC classes are not garbage collected. Depending on the usage, MFC has built in behavior to manage the lifetime of some classes, e.g. views, using standard C++ technique (new/delete). This posting is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use. © 2001 Microsoft Corporation. All rights reserved.