Solved

Getting IDispatch from VT_UNKNOWN

Posted on 2006-06-15
9
1,720 Views
Last Modified: 2011-08-18
I've got a function which returns an IUnknown in an OleVariant and I need to get the interface pointer out and call QueryInterface to see if it has an IDispatch interface. Using Delphi 5 I get "Invalid typecast" on various attemps :-

procedure win_getreturntype(var RetVal  : OleVariant;
                            var rettype : Integer;
                            var bqlVal  : TActiveXReturn);
var
   hr      : HRESULT;
   pdisp   : IDispatch;
   punkVal : IUnknown;
   locDisp : PChar;
  begin
    .....................................
//         hr := IUnknown(RetVal.punkVal).QueryInterface(IID_IDispatch, Addr(pdisp));
         punkval:=RetVal;
         hr := IUnknown(RetVal).QueryInterface(IID_IDispatch, pdisp);
         if FAILED(hr) then
           ................


Any ideas????
0
Comment
Question by:BigRat
  • 5
  • 4
9 Comments
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility

Done in D5

Regards,
Russell

---

  // Check type
  if (VarType(RetVal) = varUnknown) then
  begin
     // Get interface
     punkVal:=RetVal;
     // Resource protection
     try
        // Check interface
        if Assigned(punkVal) then
           // QI for IDispatch
           hr:=IUnknown(RetVal).QueryInterface(IDispatch, pdisp)
        else
           // No interface
           hr:=E_NOINTERFACE;
     finally
        // Release interface
        punkVal:=nil;
     end;
  end;

0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility
Or optionally:

var
   hr      : HRESULT;
   pdisp   : IDispatch;
   punkVal : IUnknown;
   locDisp : PChar;
begin

  // Check type
  if (VarType(RetVal) = varUnknown) then
  begin
     // Get interface
     punkVal:=RetVal;
     // Resource protection
     try
        // Check interface
        if Assigned(punkVal) then
           // QI for IDispatch
           hr:=punkVal.QueryInterface(IDispatch, pdisp)
        else
           // No interface
           hr:=E_NOINTERFACE;
     finally
        // Release interface
        punkVal:=nil;
     end;
  end;

end;

Delphi 5 (not sure about later versions) allows you to directly cast a variant/olevariant as either IDispatch or IUnknown. No need to use .punkVal or dispVal. An alternative (longer way of doing it) would be to cast the OleVariant as TVariantArg, eg:

  TVariantArg(RetVal).punkVal.QueryInterface(IDispatch, pdisp);

Regardless of what you do, you should also ensure the interface is non nil, as an ole variant CAN be marked as holding an interface type, and have the interface pointer = nil

Russell


0
 
LVL 27

Author Comment

by:BigRat
Comment Utility
I have Delphi5 Standard and I've just updated it, but I still get the error

   Incompatible types: 'IUnknown' and 'Variant'

on the following piece of code :-

procedure win_getreturntype(var RetVal  : Variant;
                            var rettype : Integer;
                            var bqlVal  : TActiveXReturn);
var
   hr      : HRESULT;
   pdisp   : IDispatch;
   punkVal : IUnknown;
  begin
   (* return the variant type and the variant value *)
   case RetVal.vt of
   VT_EMPTY :
      rettype := 0;
   VT_I4 :
      begin
      rettype := 1;
      bqlVal.data_int4 := RetVal.lVal;
      end;
   VT_BOOL :
      begin
      rettype := 5;
      bqlVal.data_bool:=RetVal.bool;
      end;
   VT_I2      :
      begin
      rettype := 1;
      bqlVal.data_int4 := RetVal.iVal;
      end;
   VT_UI1 :
      begin
      rettype := 1;
      bqlVal.data_int4 := RetVal.bVal;
      end;
   VT_BSTR    :
      begin
      rettype := 2;
      bqlVal.str_len := Length(RetVal.bstrVal);
      end;
   VT_R8      :
      begin
      rettype := 3;
      bqlVal.data_real := RetVal.dblVal;
      end;
   VT_UNKNOWN : (* object returned, we need dispatch *)
      begin
      rettype := 4;
      punkVal:=RetVal;      (* <======== ERROR!!!!   *)
      if Assigned(RetVal.punkVal) then
         begin
         hr := punkVal.QueryInterface(IDispatch,pdisp);
         if FAILED(hr) then
            rettype := -1
         else
            balVal. := pdisp;
         end
       else
         retval := 0;
      end;
  VT_DISPATCH :
      begin
      rettype := 4;
      if RetVal.pdispVal<>0 then
         begin// object returned - reference it
         RetVal.pdispVal->AddRef();
         bqlVal := RetVal.pdispVal;
         end
      else
         bqlVal. := 0;
      end;
   else (* unknown return type *)
      begin
      rettype := -2;
      bqlVal. := RetVal.vt;
      end;
   end;
  end;

(I have marked the line with ERROR). I have an interpreter which also uses a Variant type (my own) but does not support all the MS Variant entries, only IDispatch for example, so I switch on them and pick out the data.

Changing Variant to OLEVariant makes no difference.

I have the feeling that when I write Variant.fred Delphi does NOT access the field in the variant but assumes some form of invocation. I previously had this code in C. I don't want ANY automatic handling by Delphi.
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
Comment Utility
One thing with your above code. If you understand how variants work in Delphi, then you would realize that the following is WRONG:

>> case RetVal.vt of

When dealing with variants, if the compiler runs into the form VARIANT.NAME then it assumes that the variant is holding a dispatch interface, and that you are attempting to access a property/method of the interface. It in turn generates code to call GetIDsOfNames (to resolve the name)and Invoke (to get the result of the prop/method). I know that this was NOT what your intentions were, as you wanted to get the .vt field of the variant data (by treating the variant as a structure). As to the incompatible types, I can't say. I'm running D5 enterprise, and am not running into an error on the assignment. This compiles (and works) for me:

var  v        :  Variant;
     pdisp    :  IDispatch;
     punkVal  :  IUnknown;

begin

  pdisp:=v;
  punkVal:=v;

end;

Perhaps you could check the code clip to see if it compiles. I would hate to think that there is such an incompatibility on the same ver of Delphi, regardless of std/pro/ent.

Anyways, to hopefully resolve both your issues, I offer the following code as an optional way of accessing the data, basically a cast of the variant to its true form of tagVariant (the same as in C). I have compiled this (I mocked up a struct to handle the TActiveXReturn) and had no problems either in compiling or running. I will also throw this out there, just so you don't run into problems later:

The TVariantArg structure has unkVal, punkVal (Pointer to pointer, byref ver) dispVal, and pdispVal (Pointer to pointer, byref ver) defined as Pointer. The implications of this are that NO smart pointer referencing (auto addref/release) is done. You can cast them accordingly to perform QI's, etc, eg:

  hr:=IUnknown(vaVal.unkVal).QueryInterface(IDispatch, pdisp);

But if you CAST them to return a result, then an AddRef will be peformed, eg:

 pdisp:=IDispatch(vaVal.dispVal);

But peforming this:

 pdisp:=vaVal.dispVal

Will NOT peform an addref on the interface. It will all work well and good until the last ref count goes out of scope and the code bombs due to reference counting issues. Just something you should be aware of (if you aren't already).


Russell

------

procedure win_getreturntype(var RetVal: Variant; var rettype: Integer; var bqlVal: TActiveXReturn);
var  hr      :   HRESULT;
     vaVal   :   TVariantArg;
     pdisp   :   IDispatch;
     punkVal :   IUnknown;
begin

  // Cast the variant to tagVARIANT structure
  vaVal:=TVariantArg(RetVal);

  // Handle the variant type
  case vaVal.vt of
     VT_EMPTY    :
     begin
        //
     end;
     VT_I4       :
     begin
        //
     end;
     VT_BOOL     :
     begin
        //
     end;
     VT_I2       :
     begin
        //
     end;
     VT_UI1      :
     begin
        //
     end;
     VT_BSTR     :
     begin
        //
     end;
     VT_R8       :
     begin
        //
     end;
     VT_UNKNOWN  :
     begin
        rettype:=4;
        if Assigned(vaVal.unkVal) then
        begin
           hr:=IUnknown(vaVal.unkVal).QueryInterface(IDispatch, pdisp);
           if FAILED(hr) then
              rettype:=-1
           else
              // ...
        end
        else
           retval:=0;
     end;
     VT_DISPATCH :
     begin
        rettype:=4;
        if Assigned(vaVal.dispVal) then
        begin
           // Do whatever with the interface. At its current holding of vaVal.dispVal,
           // Delphi will treat this as a pointer, meaning no auto addref/release.
        end
        else
           retval:=0;
     end;
  else (* unknown return type *)
     // ...
  end;

end;
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 27

Author Comment

by:BigRat
Comment Utility
Code added to my code :-

procedure rlibby;
var  v        :  Variant;
     pdisp    :  IDispatch;
     punkVal  :  IUnknown;

begin

  pdisp:=v;
  punkVal:=v;

end;

Error: Incompatible types IDispatch and Variant on the first assignment.
Delphi 5 Standard (Build 6.18) Update Pack 1
Windows 2000 Service Pack 1 Build 2600
0
 
LVL 27

Author Comment

by:BigRat
Comment Utility
>>If you understand how variants work in Delphi, then you would realize that the following is WRONG:

Which I don't. THe original interpreter was written some 10 years ago with Norsk Data Pascal and I added all the COM handling by linking in C code written for MS VC 4.0. All no problem. BUT the compiler for Unix was ported to SCO and Windows but nowhere since. So for Linux I decided to use Delphi because of Kylix availability and I didn't need to rewrite everything. That's turned out wrong - Kylix is no more and Delphi's going where? So I recompile everything using Free Pascal and use Delphi for my IDE and as an IDE for development.

>>Will NOT peform an addref on the interface. It will all work well and good until the last ref count goes out of scope and the code bombs due to reference counting issues. Just something you should be aware of (if you aren't already).

My own object system uses reference counting, so I handle the addref there. This is absolutely no problem.

>>Cast the variant to tagVARIANT structure

Ah! That's how one does it. I'm beavering away right now. Let you know in a few minutes!
0
 
LVL 27

Author Comment

by:BigRat
Comment Utility
Yes, that did it.

A question: Is TVariantArgs EXACTLY the C VARIANT type, so that I could use it instead of the Delphi Variant. I have :-

 win_Dispatch:=IDISPATCH(iDisp).Invoke(funcId,IID_NULL,LOCALE_SYSTEM_DEFAULT,wFlags,
                                          DispParams,Addr(RetVal),nil,nil);

I don't think that Addr(RetVal) = Addr(Variant) is a good idea?

And of course I don't want any Delphi default handling.

According to www.howtodothings.com/viewarticle.aspx?article=405 (out of Google cache) it seems that TVariantArg is the correct way?

0
 
LVL 27

Author Comment

by:BigRat
Comment Utility
Additional comment from rlibby :-

As to the final questions, yes, it is *EXACTLY* the same as it is in C/C++. In
fact, all of the following are the same:

Variant = OleVariant = TVariantArg = tagVARIANT

Its just that the compiler peforms special conversion handling for (Ole)Variant
variables.  (eg, initialization to unassigned, auto cleanup, etc).

And this:

win_Dispatch:=IDISPATCH(iDisp).Invoke(funcId,IID_NULL,LOCALE_SYSTEM_DEFAULT,wFlags,
                                          DispParams,Addr(RetVal),nil,nil);

is fine (correct), though I personally tend to use @RetVal. Same-same though.
You could also pass the @ of TVariantArg if you wanted to, it just depends on
what you plan on doing with it afterwards.

Points increased for that and PAQed.

Thanks for the info, I'll start testing on Monday!
0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility
BigRat,

Glad you got the last bit of info. It seems I (and others from reading CS support) are getting this "Question Not Found" ID(0) error when attempting to post to Q's. I have found that hitting the browser's refresh button will cause the repost to actually go through... not pretty, but it works.

Anyways, let me know if you have any further problems / issues / questions with the COM stuff, as it is an area I am fairly well versed in. And thanks for the points ;-), always appreciated.

Regards,
Russell
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now