views:

107

answers:

1

Hi guys.

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.

+2  A: 

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)

Stijn Sanders
Thanks a lot Stijn! It works perfectly. You could also cast it without the *as* like this: _Connection((IUnknown(NativeConnection)). The mystery for me is why VarToIntf() is giving me an AV...
Wodzu