Activator::CreateInstance( unmanaged type )

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

    Activator::CreateInstance( unmanaged type )

    Hi,

    VC++ 2008 /clr:

    I have an unmanaged class: "class U" and managed one: "class ref M{ M(
    const U* ); }". In other words, the constructor of the managed class M
    takes the pointer to the unmanaged class U. I can call the constructor
    directly no problem. I want to instantiate M via reflection.

    Activator::Crea teInstance() takes an array of parameters to be passed to the
    constructor:

    array< System::Object^ >^ pParams = gcnew array< System::Object^ >( 1 );
    const U* pu = new U;
    pParams[ 0 ] = pu;

    The last line gives error C2440: '=' : cannot convert from 'const U *' to
    'System::Object ^

    My question is, how do I pass a pointer to an unmanaged class as a
    constructor parameter to Activator::Crea teInstance()?

    Thank you,

    Andrew



  • Jochen Kalmbach [MVP]

    #2
    Re: Activator::Crea teInstance( unmanaged type )

    Hi muriwai!
    array< System::Object^ >^ pParams = gcnew array< System::Object^ >( 1 );
    const U* pu = new U;
    pParams[ 0 ] = pu;
    For "pointers" you should use "System::IntPtr "

    Greetings
    Jochen

    Comment

    • muriwai

      #3
      Re: Activator::Crea teInstance( unmanaged type )

      Thanks Jochen. I tried the following:

      const U* pu = new U;
      pParams[ 0 ] = gcnew System::IntPtr( ( PVOID) pu );
      Activator.Creat eInstance( type, pParams );

      but the last line throws an exception because it can't find the constructor
      (the constructor is "internal" by the way, and I am calling from the same
      DLL).

      I ended up with type->GetConstructor s() and then explicitly invoking the
      constructor with IntPtr as a parameter.

      Cheers


      "Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach @holzma.dewrote in message
      news:uYtonvcAJH A.524@TK2MSFTNG P06.phx.gbl...
      Hi muriwai!
      >
      >array< System::Object^ >^ pParams = gcnew array< System::Object^ >( 1 );
      >const U* pu = new U;
      >pParams[ 0 ] = pu;
      >
      For "pointers" you should use "System::IntPtr "
      >
      Greetings
      Jochen

      Comment

      Working...