Managed Class Instantiating Unmanaged Class

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

    #1

    Managed Class Instantiating Unmanaged Class

    I am writing a .NET Windows application and it needs to
    perform DDE calls to a legacy system. I created a C++
    unmanaged class that performs the actual DDE connection
    and communication. However, I need .NET to instantiate
    this object and somehow allow the this object to callback
    to .NET. So, my question is:

    1. How can a managed class instantiate and unmanaged
    class?

    2. How can an unmanaged class call a function or delegate
    in the managed class?

    I would like both classes to reside in the same project,
    but it doesn't have to.

    Thanks!
  • Ted Miller

    #2
    Re: Managed Class Instantiating Unmanaged Class

    Creating a C++ object involves calling operator new to get storage for it
    and then invoking a constructor. Conversely, destroying a C++ object
    involves calling the destructor and then invoking operator delete to release
    its storage. Even if you could somehow hack these steps into working
    directly from managed code, you would still have to maually maintain and
    pass the object's 'this' pointer to any methods.

    Basically, you have 2 options I can see.

    (1) Make your class COM. Then you can use existing interop tools to access
    your class.

    (2) Add global (or even better, static) methods to your class to mirror its
    existing methods. The first parameter would be the this pointer. You also
    need factory methods. Then p/invoke to these.

    class MyClass {
    public:
    MyClass();
    ~MyClass();
    int Method1(int i);
    //...

    // Add these
    static MyClass *MyClass_Ctor() { return new MyClass(); }
    static MyClass *MyClass_Dtor(M yClass *_this) { delete (MyClass
    *)_this; }
    static int MyClass_Method1 (MyClass *_this,int i) { return ((MyClass
    *)_this)->Method1(i); }
    };

    Then you delcare these for p/invoke as

    IntPtr MyClass_Ctor();
    void MyClass_Dtor(In tPtr _this);
    int MyClass_Method1 (IntPtr _this,int i);

    Dunno, maybe making them COM is easier. Or could you make then plain old
    non-OO C or C++ to begin with?

    "Chris Kiechel" <chris.kiechel@ applicationcraf tsmen.com> wrote in message
    news:057001c3a3 1d$f3d7efa0$a10 1280a@phx.gbl.. .[color=blue]
    > I am writing a .NET Windows application and it needs to
    > perform DDE calls to a legacy system. I created a C++
    > unmanaged class that performs the actual DDE connection
    > and communication. However, I need .NET to instantiate
    > this object and somehow allow the this object to callback
    > to .NET. So, my question is:
    >
    > 1. How can a managed class instantiate and unmanaged
    > class?
    >
    > 2. How can an unmanaged class call a function or delegate
    > in the managed class?
    >
    > I would like both classes to reside in the same project,
    > but it doesn't have to.
    >
    > Thanks![/color]


    Comment

    • Chris Kiechel

      #3
      Re: Managed Class Instantiating Unmanaged Class

      Hmmm...you're challenging my assumptions. Is "unmanaged
      code" and regular C++ the same thing, or are they two
      different beasts?

      They must be different, which would explain why the
      unmanaged code examples that I've reviewed looked so
      foreign.

      So, I need to "get smart" on the differences between
      unmanaged code and C++!

      Thanks, Ted! That was helpful.

      [color=blue]
      >-----Original Message-----
      >Creating a C++ object involves calling operator new to[/color]
      get storage for it[color=blue]
      >and then invoking a constructor. Conversely, destroying a[/color]
      C++ object[color=blue]
      >involves calling the destructor and then invoking[/color]
      operator delete to release[color=blue]
      >its storage. Even if you could somehow hack these steps[/color]
      into working[color=blue]
      >directly from managed code, you would still have to[/color]
      maually maintain and[color=blue]
      >pass the object's 'this' pointer to any methods.
      >
      >Basically, you have 2 options I can see.
      >
      >(1) Make your class COM. Then you can use existing[/color]
      interop tools to access[color=blue]
      >your class.
      >
      >(2) Add global (or even better, static) methods to your[/color]
      class to mirror its[color=blue]
      >existing methods. The first parameter would be the this[/color]
      pointer. You also[color=blue]
      >need factory methods. Then p/invoke to these.
      >
      >class MyClass {
      >public:
      > MyClass();
      > ~MyClass();
      > int Method1(int i);
      > //...
      >
      > // Add these
      > static MyClass *MyClass_Ctor() { return new MyClass[/color]
      (); }[color=blue]
      > static MyClass *MyClass_Dtor(M yClass *_this) { delete[/color]
      (MyClass[color=blue]
      >*)_this; }
      > static int MyClass_Method1 (MyClass *_this,int i) {[/color]
      return ((MyClass[color=blue]
      >*)_this)->Method1(i); }
      >};
      >
      >Then you delcare these for p/invoke as
      >
      > IntPtr MyClass_Ctor();
      > void MyClass_Dtor(In tPtr _this);
      > int MyClass_Method1 (IntPtr _this,int i);
      >
      >Dunno, maybe making them COM is easier. Or could you make[/color]
      then plain old[color=blue]
      >non-OO C or C++ to begin with?
      >
      >"Chris Kiechel" <chris.kiechel@ applicationcraf tsmen.com>[/color]
      wrote in message[color=blue]
      >news:057001c3a 31d$f3d7efa0$a1 01280a@phx.gbl. ..[color=green]
      >> I am writing a .NET Windows application and it needs to
      >> perform DDE calls to a legacy system. I created a C++
      >> unmanaged class that performs the actual DDE connection
      >> and communication. However, I need .NET to instantiate
      >> this object and somehow allow the this object to[/color][/color]
      callback[color=blue][color=green]
      >> to .NET. So, my question is:
      >>
      >> 1. How can a managed class instantiate and unmanaged
      >> class?
      >>
      >> 2. How can an unmanaged class call a function or[/color][/color]
      delegate[color=blue][color=green]
      >> in the managed class?
      >>
      >> I would like both classes to reside in the same project,
      >> but it doesn't have to.
      >>
      >> Thanks![/color]
      >
      >
      >.
      >[/color]

      Comment

      • Ted Miller

        #4
        Re: Managed Class Instantiating Unmanaged Class

        Unmanaged == "regular C++"

        "Chris Kiechel" <chris.kiechel@ applicationcraf tsmen.com> wrote in message
        news:082301c3a3 4e$c150cb20$a30 1280a@phx.gbl.. .[color=blue]
        > Hmmm...you're challenging my assumptions. Is "unmanaged
        > code" and regular C++ the same thing, or are they two
        > different beasts?
        >
        > They must be different, which would explain why the
        > unmanaged code examples that I've reviewed looked so
        > foreign.
        >
        > So, I need to "get smart" on the differences between
        > unmanaged code and C++!
        >
        > Thanks, Ted! That was helpful.
        >
        >[color=green]
        > >-----Original Message-----
        > >Creating a C++ object involves calling operator new to[/color]
        > get storage for it[color=green]
        > >and then invoking a constructor. Conversely, destroying a[/color]
        > C++ object[color=green]
        > >involves calling the destructor and then invoking[/color]
        > operator delete to release[color=green]
        > >its storage. Even if you could somehow hack these steps[/color]
        > into working[color=green]
        > >directly from managed code, you would still have to[/color]
        > maually maintain and[color=green]
        > >pass the object's 'this' pointer to any methods.
        > >
        > >Basically, you have 2 options I can see.
        > >
        > >(1) Make your class COM. Then you can use existing[/color]
        > interop tools to access[color=green]
        > >your class.
        > >
        > >(2) Add global (or even better, static) methods to your[/color]
        > class to mirror its[color=green]
        > >existing methods. The first parameter would be the this[/color]
        > pointer. You also[color=green]
        > >need factory methods. Then p/invoke to these.
        > >
        > >class MyClass {
        > >public:
        > > MyClass();
        > > ~MyClass();
        > > int Method1(int i);
        > > //...
        > >
        > > // Add these
        > > static MyClass *MyClass_Ctor() { return new MyClass[/color]
        > (); }[color=green]
        > > static MyClass *MyClass_Dtor(M yClass *_this) { delete[/color]
        > (MyClass[color=green]
        > >*)_this; }
        > > static int MyClass_Method1 (MyClass *_this,int i) {[/color]
        > return ((MyClass[color=green]
        > >*)_this)->Method1(i); }
        > >};
        > >
        > >Then you delcare these for p/invoke as
        > >
        > > IntPtr MyClass_Ctor();
        > > void MyClass_Dtor(In tPtr _this);
        > > int MyClass_Method1 (IntPtr _this,int i);
        > >
        > >Dunno, maybe making them COM is easier. Or could you make[/color]
        > then plain old[color=green]
        > >non-OO C or C++ to begin with?
        > >
        > >"Chris Kiechel" <chris.kiechel@ applicationcraf tsmen.com>[/color]
        > wrote in message[color=green]
        > >news:057001c3a 31d$f3d7efa0$a1 01280a@phx.gbl. ..[color=darkred]
        > >> I am writing a .NET Windows application and it needs to
        > >> perform DDE calls to a legacy system. I created a C++
        > >> unmanaged class that performs the actual DDE connection
        > >> and communication. However, I need .NET to instantiate
        > >> this object and somehow allow the this object to[/color][/color]
        > callback[color=green][color=darkred]
        > >> to .NET. So, my question is:
        > >>
        > >> 1. How can a managed class instantiate and unmanaged
        > >> class?
        > >>
        > >> 2. How can an unmanaged class call a function or[/color][/color]
        > delegate[color=green][color=darkred]
        > >> in the managed class?
        > >>
        > >> I would like both classes to reside in the same project,
        > >> but it doesn't have to.
        > >>
        > >> Thanks![/color]
        > >
        > >
        > >.
        > >[/color][/color]


        Comment

        • Pent

          #5
          Re: Managed Class Instantiating Unmanaged Class

          1. ---------------------------------

          struct Unmanaged
          {
          void func(){}
          };
          __gc class Managed /*: public IDispose*/
          {
          Unmanaged* m_pUnMan;
          public:
          Managed() { m_pUnMan = new Unmanaged(); }
          ~Managed() { delete m_pUnMan, m_pUnMan=0; }
          void func()
          {
          Managed __pin* pThis = this;
          m_pUnMan->func(pThis);
          }
          };

          2. ---------------------------------

          //.h
          struct Unmanaged
          {
          void func(Managed* pMan);
          };

          __gc class Managed /*: public IDispose*/
          {
          Unmanaged* m_p;
          public:
          Managed() { m_p = new Unmanaged(); }
          ~Managed() { delete m_p, m_p=0; }
          void func()
          {
          Managed __pin* pThis = this;
          m_p->func(pThis);
          }
          };
          //.cpp
          void Unmanaged::func (Managed* p)
          {
          Console::Writel ine(p->ToString());
          }

          "Chris Kiechel" <chris.kiechel@ applicationcraf tsmen.com> wrote in message
          news:057001c3a3 1d$f3d7efa0$a10 1280a@phx.gbl.. .[color=blue]
          > I am writing a .NET Windows application and it needs to
          > perform DDE calls to a legacy system. I created a C++
          > unmanaged class that performs the actual DDE connection
          > and communication. However, I need .NET to instantiate
          > this object and somehow allow the this object to callback
          > to .NET. So, my question is:
          >
          > 1. How can a managed class instantiate and unmanaged
          > class?
          >
          > 2. How can an unmanaged class call a function or delegate
          > in the managed class?
          >
          > I would like both classes to reside in the same project,
          > but it doesn't have to.
          >
          > Thanks![/color]


          Comment

          • Chris Kiechel

            #6
            Re: Managed Class Instantiating Unmanaged Class

            Bingo! Thanks!
            [color=blue]
            >-----Original Message-----
            >1. ---------------------------------
            >
            >struct Unmanaged
            >{
            > void func(){}
            >};
            >__gc class Managed /*: public IDispose*/
            >{
            > Unmanaged* m_pUnMan;
            >public:
            > Managed() { m_pUnMan = new Unmanaged(); }
            > ~Managed() { delete m_pUnMan, m_pUnMan=0; }
            > void func()
            > {
            > Managed __pin* pThis = this;
            > m_pUnMan->func(pThis);
            > }
            >};
            >
            >2. ---------------------------------
            >
            >//.h
            >struct Unmanaged
            >{
            > void func(Managed* pMan);
            >};
            >
            >__gc class Managed /*: public IDispose*/
            >{
            > Unmanaged* m_p;
            >public:
            > Managed() { m_p = new Unmanaged(); }
            > ~Managed() { delete m_p, m_p=0; }
            > void func()
            > {
            > Managed __pin* pThis = this;
            > m_p->func(pThis);
            > }
            >};
            >//.cpp
            >void Unmanaged::func (Managed* p)
            >{
            > Console::Writel ine(p->ToString());
            >}
            >
            >"Chris Kiechel" <chris.kiechel@ applicationcraf tsmen.com>[/color]
            wrote in message[color=blue]
            >news:057001c3a 31d$f3d7efa0$a1 01280a@phx.gbl. ..[color=green]
            >> I am writing a .NET Windows application and it needs to
            >> perform DDE calls to a legacy system. I created a C++
            >> unmanaged class that performs the actual DDE connection
            >> and communication. However, I need .NET to instantiate
            >> this object and somehow allow the this object to[/color][/color]
            callback[color=blue][color=green]
            >> to .NET. So, my question is:
            >>
            >> 1. How can a managed class instantiate and unmanaged
            >> class?
            >>
            >> 2. How can an unmanaged class call a function or[/color][/color]
            delegate[color=blue][color=green]
            >> in the managed class?
            >>
            >> I would like both classes to reside in the same project,
            >> but it doesn't have to.
            >>
            >> Thanks![/color]
            >
            >
            >.
            >[/color]

            Comment

            Working...