Solved

Getting IDispatch from VT_UNKNOWN

Posted on 2006-06-15
9
1,755 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 16913523

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
ID: 16913594
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
ID: 16919222
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 16920263
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
 
LVL 27

Author Comment

by:BigRat
ID: 16920394
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
ID: 16920455
>>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
ID: 16920614
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
ID: 16921278
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
ID: 16923820
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…

734 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