开发者

How to cast Variant to TADOConnection.ConnectionObject?

开发者 https://www.devze.com 2022-12-15 10:56 出处:网络
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 inval

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号