Wodzu
Wodzu

Reputation: 6989

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

Upvotes: 2

Views: 737

Answers (1)

Stijn Sanders
Stijn Sanders

Reputation: 36850

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)

Upvotes: 2

Related Questions