Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Conversion to MC++???

Conversion to MC++???

Scheduled Pinned Locked Moved Managed C++/CLI
helpcsharpc++comtutorial
4 Posts 3 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    ydasari
    wrote on last edited by
    #1

    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.

    N 1 Reply Last reply
    0
    • Y ydasari

      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.

      N Offline
      N Offline
      Nick Hodapp
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • N Nick Hodapp

        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.

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        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

        N 1 Reply Last reply
        0
        • A Anonymous

          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

          N Offline
          N Offline
          Nick Hodapp
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups