ActiveX and Late binding in C++/CLI

  • Thread starter Thread starter none
  • Start date Start date
N

none

Hello All

My app requires runtime creation of an ActiveX control on demand. I
attempted to fullfil this requirment with the following code snippet:

..
..
..
System::Type ^t =
System::Type::GetTypeFromProgID("CONTINUUMX.TRecordSetCtrl.1");

AxCONTINUUMXLib::AxTRecordSet ^rs =
(AxCONTINUUMXLib::AxTRecordSet^)System::Activator::CreateInstance(t);

rs->DataChange += gcnew
AxCONTINUUMXLib::_DTRecordSetEvents_DataChangeEventHandler(this,&Form1::OnDataChange);

rs->Open(textBox1->Text + "," + "Daily");
rs->Init(100, 0);
rs->GetRecDouble(0,0, val);

Unfortunately the above results in the following runtime error:

Unable to cast COM object of type 'AxCONTINUUMXLib.AxTRecordSet' to
class type ''. Instances of types that represent COM components cannot
be cast to types that do not represent COM components; however they can
be cast to interfaces as long as the underlying COM component supports
QueryInterface calls for the IID of the interface.

Is reflection and the use of System::Type::GetMethod the only (and
tedious) way of doing this?

Any hint or help is greatly appreciated.

Regards
 
none said:
Hello All

My app requires runtime creation of an ActiveX control on demand. I
attempted to fullfil this requirment with the following code snippet:

.
.
.
System::Type ^t =
System::Type::GetTypeFromProgID("CONTINUUMX.TRecordSetCtrl.1");

AxCONTINUUMXLib::AxTRecordSet ^rs =
(AxCONTINUUMXLib::AxTRecordSet^)System::Activator::CreateInstance(t);

rs->DataChange += gcnew
AxCONTINUUMXLib::_DTRecordSetEvents_DataChangeEventHandler(this,&Form1::OnDataChange);

rs->Open(textBox1->Text + "," + "Daily");
rs->Init(100, 0);
rs->GetRecDouble(0,0, val);

Unfortunately the above results in the following runtime error:

Unable to cast COM object of type 'AxCONTINUUMXLib.AxTRecordSet' to class
type ''. Instances of types that represent COM components cannot be cast
to types that do not represent COM components; however they can be cast to
interfaces as long as the underlying COM component supports QueryInterface
calls for the IID of the interface.

Is reflection and the use of System::Type::GetMethod the only (and
tedious) way of doing this?

Yes, reflection only. If you want late binding support for COM at the
language level you should use VB.NET, C++/CLI and C# aren't well suited for
this.

Willy.
 
Yes, reflection only. If you want late binding support for COM at the
language level you should use VB.NET, C++/CLI and C# aren't well
suited for this.

Willy.


Thank you, Willy. I appreciate your expedient response.

Regards
 
System::Type ^t =
System::Type::GetTypeFromProgID("CONTINUUMX.TRecordSetCtrl.1");

AxCONTINUUMXLib::AxTRecordSet ^rs =
(AxCONTINUUMXLib::AxTRecordSet^)System::Activator::CreateInstance(t);

What you get from Activator create instance is a Runtime Callable Wrapper
(RCW). If you cast an RCW to a .NET interface with the Guid attribute, it
calls QueryInterface under the hood.

Often, the easiest way to get an interface with the correct attribute
settings etc., is the TLBIMP.EXE tool.

Marcus Heege
 
Back
Top