@dawn has asked for the wisdom of the Perl Monks concerning the following question:

Hello Great Ones

I have a problem understanding passing variants by reference and returning the changed variant value by the method the object is calling!

Example.....
use strict; use Win32::OLE; use Win32::OLE::Variant; my $obj = Win32::OLE->new('Server.SpellCheck') or die "Can not start +server\n"; my $word = 'testt'; my $lang = 24941; my $dic = ''; my $opt = 303872; my $dpt = 70; # start the server by calling the REG TREE # it is using for this spell check session $obj->Start('SOFTWARE\abc spell\\' . $lang); # create the place holder for the suggestions # list returned by the Suggest() method below my $sugg = Variant(VT_BSTR|VT_BYREF, ""); # call the Suggest() method to get the suggestions # for the misspelled word, held in $word value! $obj->Suggest($word, $dpt, $lang, $dic, $opt, $sugg); print $sugg . "\n";

$sugg, should return a list of suggestions by reference but it does not! So can someone show me what I am doing, because I thought I followed the Doc....

OLE DOCS... Variants by reference Some OLE servers expect parameters passed by reference so that they ca +n be changed in the method call. This allows methods to easily return multi +ple values. There is preliminary support for this in the Win32::OLE::Varia +nt module: my $x = Variant(VT_I4|VT_BYREF, 0); my $y = Variant(VT_I4|VT_BYREF, 0); $Corel->GetSize($x, $y); print "Size is $x by $y\n";

Thank You

Dawn

Edited by Chady -- fixed formatting.

Replies are listed 'Best First'.
Re: Win32::OLE (Variants by reference)
by maa (Pilgrim) on Mar 30, 2004 at 06:49 UTC

    Hi, @dawn

    I'm going to guess that the reason you've not have much response to your post is that no-one is familiar with the spellcheck application you're using.

    A couple of things you could check, though:

    1. Does the Server.Spellcheck object actually support OLE automation?
    2. Can you automate it in VB(A|S) using the code you've written?

    Our (my) ignorance of your spellchecker aside, there doesn't seem to be anything wrong with your code which would also account for the lack of replies. My advice would be: "try it in VBS and see if it works".

    Oh, and Win32::OLE isn't perfect either. Some stuff that works in VBS and "runs" perfectly in perl doesn't actually "do" anything - but I've only seen that happen when trying to automate Outlook, so far.

    HTH - Mark

    PS: if the reference is a list doesn't that mean OLE will cast is as an Variant Array() or worse, a Collection?

      Hi maa

      I was thinking about what you said PS: and in part I think you were correct! It is not documented but I have found out by testing and finally by the author note that any "string variant -> Variant(VT_BSTR, '');" regardless if it is passed by reference "VT_BYREF" or not, can not be used if that "string variant" will be used in a method -> $object->Method(); because the $object->Method(); will assign the "variant" to a "string variant value"!

      So in short, by doing this....

      my $sugg = Variant(VT_BSTR|VT_BYREF, "");

      I was assigning $sugg to an absolute value = "", that could not be changed by the method -> $object->Method(); that I was using it in...

      The correct way to pass a "string variant" to a method -> $object->Method(); that will change or assign a value to the "string variant" is listed below...

      my $sugg = Variant(VT_VARIANT|VT_BYREF, "");

      Authors Note...
      After the method call $sugg will hold the returned value for the $sugg variant. It will still be variant but the overloading converts it to string representation automatically.

      A side note (to help others you might run into this problem)...

      Any "OLE String -> VT_BSTR or OLE Boolean -> VT_BOOL" that are placed in a "object Method" and they will return a value by the "method" that they are placed in should be called "VT_VARIANT"

      Thanks Mark

      @dawn