ActiveX and Late binding in C++/CLI

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
 
W

Willy Denoyette [MVP]

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.
 
N

None

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
 
M

Marcus Heege

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top