C++/CLI mixing managed/unmanaged

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jraul

    #1

    C++/CLI mixing managed/unmanaged

    Hi,

    This is probably a noobie question but:

    I just created a new C++/CLI project in VS 2005. It created an empty
    class:

    public ref class Class1
    {
    // TODO: Add your methods for this class here.

    };

    I added a method and its implementation. I also added an unmanaged
    instance (from a native class library I need to use) as a data member
    and the compiler complained:

    error C4368: cannot define 'mTest' as a member of managed
    'TestProj::Clas s1': mixed types are not supported

    Can I not have unmanaged data members in a managed class? If not, how
    am I supposed to interop between managed and unmanaged code?

    I took out the "public ref" prefix, and the code compiled.

  • David Ching

    #2
    Re: C++/CLI mixing managed/unmanaged

    "jraul" <jraulinth@yaho o.comwrote in message
    news:1183344380 .054367.175940@ a26g2000pre.goo glegroups.com.. .
    Hi,
    >
    This is probably a noobie question but:
    >
    I just created a new C++/CLI project in VS 2005. It created an empty
    class:
    >
    public ref class Class1
    {
    // TODO: Add your methods for this class here.
    >
    };
    >
    I added a method and its implementation. I also added an unmanaged
    instance (from a native class library I need to use) as a data member
    and the compiler complained:
    >
    error C4368: cannot define 'mTest' as a member of managed
    'TestProj::Clas s1': mixed types are not supported
    >
    Can I not have unmanaged data members in a managed class? If not, how
    am I supposed to interop between managed and unmanaged code?
    >
    I took out the "public ref" prefix, and the code compiled.
    >
    No, unfortunately you cannot have a managed C++/CLI class that contains a
    native member. Sorry. The implementation of your managed C++/CLI class
    (i.e. when you define its methods) can use native local variables, etc.

    -- David


    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: C++/CLI mixing managed/unmanaged


      "jraul" <jraulinth@yaho o.comwrote in message
      news:1183344380 .054367.175940@ a26g2000pre.goo glegroups.com.. .
      Hi,
      >
      This is probably a noobie question but:
      >
      I just created a new C++/CLI project in VS 2005. It created an empty
      class:
      >
      public ref class Class1
      {
      // TODO: Add your methods for this class here.
      >
      };
      >
      I added a method and its implementation. I also added an unmanaged
      instance (from a native class library I need to use) as a data member
      and the compiler complained:
      >
      error C4368: cannot define 'mTest' as a member of managed
      'TestProj::Clas s1': mixed types are not supported
      >
      Can I not have unmanaged data members in a managed class? If not, how
      am I supposed to interop between managed and unmanaged code?
      You have to instead use a pointer to the unmanaged data, and allocate it
      with "new" in your constructor and "delete" in your destructor and
      finalizer. That way the unmanaged data is stored on the native heap, where
      the garbage collector won't try to move it (having the gc move unmanaged
      data around would be a total disaster, because unmanaged code might be using
      it).
      >
      I took out the "public ref" prefix, and the code compiled.
      >

      Comment

      Working...