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

Many thanks to the Perl Monks for helping me get this far. I am forced to report however that I still need some help. I am stuck trying to call a function, getting an error I do not understand.

Here is my code, with lots of comments at to how it behaves:

use Win32::OLE;
use Win32::OLE::Const;
my $HALi;

# Create a new OLE object using the CLSID of HALiCnt.HALi
## This works, or at least does not return any errors
  $HALi = Win32::OLE->new('{0533EA34-1857-443E-8A9F-62399F881402}') ||
  die "Cannot create COM server";


print "Text String representing TYPE of COM server via CLSID.. ";
print Win32::OLE->QueryObjectType($HALi);
## Returns the string 'HALiCnt.HALi', so I guess the previous 'NEW' was
## Successful
print "\n";



## The VB examples I have suggest that the system must be initialized
## at the start of the program with the VB command 'HALi.Init()'
## So I translate this to::::
$HALi->Init();
## Fails with "This Key Already associated with an element of this collection
## I had tried many other combinations, but this is the only
## format that generates anything other than "invalid class string"
# However, if I do not make the Init call, but instead call destroy
$HALi->DESTROY(); # Call DESTROY to free memory and release the HALi Objects
# Then this appears to work just fine.

print "Last OLE Error detected.. ";
print Win32::OLE->LastError();
## Returns a '0' if I do not call Init() but returns the error posted 
So I am fairly sure that I am opening the correct COM objects, and the fact that I get the "already associated" error implies that the Init() function is present. But I am doing it incorrectly somehow.

Here is the result of the actual run:

C:\perl>testole.pl
Text String representing TYPE of COM server via CLSID.. HALiCnt_HALi
OLE exception from "HALiCnt":

This key is already associated with an element of this collection

Win32::OLE(0.1403) error 0x800a01c9
    in METHOD/PROPERTYGET "Init" at C:\perl\testole.pl line 26
Last OLE Error detected.. OLE exception from "HALiCnt":

This key is already associated with an element of this collection

Win32::OLE(0.1403) error 0x800a01c9
    in METHOD/PROPERTYGET "Init"
C:\perl>
I have spent hours studying docs and experimenting with different forms of the Init() function. I have used OLEView to look at the object libraries and the OLE Browser as well. Any format other than the one gives me "invalid Class String".

I am once again lost, and in need of enlightenment.

Thanks,

Nathan

  • Comment on Still More help needed with Win32::OLE project.

Replies are listed 'Best First'.
Re: Still More help needed with Win32::OLE project.
by Solo (Deacon) on Jun 15, 2004 at 15:06 UTC
    Is Init the only method you need to call on this object? What if you press forward and ignore that Init doesn't seem to work? (Perhaps it was called implicitly upon object creation.)

    --Solo
    --
    You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.
      Actually, I need to call several functions. The Init is just the first in the process. I have been working from the hypothesis that the manufacturers instructions (and their VB examples) that claim the call to Init() is necessary before calling any other functions are correct. Still, it's a good thought. I will try that.

      Nathan

      Update: Tried it. No dice.

      my $c = $HALi->{Modes}=Count();

      fails with undefined subroutine. Of course, I may be calling it wrongly too. I will keep experimenting.

      Nathan

        my $c = $HALi->{Modes}->Count(); # ^^
        is what you are looking for, I believe--or at least the right way to call the Count method on the Modes collection.

        --Solo
        --
        You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.