How to cast Variant to TADOConnection.ConnectionObject?
I've received a native COM ADOConnection which is stored in Variant. I would like to pass interface of this connection to the VCL wrapper TADOConnection. The problem is that either I am getting invalid typecast compiler message or acces violation.
For example:
procedure AssignNativeConnection(VCLConnection: TADOConnection; var NativeConnection: Variant);
var
VariantManager: TVariantManager;
AInterface: IInterface;
begin
AInterface := VCLConnection.ConnectionObject;
VariantManager.VarToIntf(AInterface, NativeConnection); //oops AV here!
end;
Any ideas how to solve that problem? I am using Delphi 2007.
Thanks in adv开发者_运维问答ance.
I have made this work several times by using a plain IUnknown cast from a variant first and then the as operator, much like this:
VCLConnection.ConnectionObject:=(IUnknown(NativeConnection) as _Connection);
(I notice the ConnectionObject property is of type _Connection and that it is defined in the ADOInt unit.) Ofcourse, you will still get an AV on an invalid pointer or nil pointer or anything that is not responding properly to the basic interface calls (QueryInterface and the like)
精彩评论