Win32::OLE::Const can't find 64-bit MS Office constants

1.6k Views Asked by At

We have a legacy perl code base that includes the below line:

use Win32::OLE::Const 'Microsoft Excel';

This has worked traditionally but is not working on newer 64-bit installations, such as Windows 10.

The error appears to be in the OLE.xs source as below, but I have a limited understanding of Windows functions and XS in general.

err = RegQueryValueA(hKeyLangid, "win32", szFile, &cbFile);

If this query fails, it never calls Win32::OLE::Const::_Typelib which is the function that stores the result. Checking my registry, the keys are indeed Win64 and not win32. Other keys that worked either have just win32 or both.

Is there a way to resolve this issue without editing the legacy module? It is widely used and any changes would involve some risk so I'm looking at alternatives first.

I'm aware we can do the below, but it doesn't stop the other Win32::OLE::Const line causing an error if it's not removed.

my $Excel = Win32::OLE->new('Excel.Application', 'Quit');
my $xl = Win32::OLE::Const->Load($Excel);
1

There are 1 best solutions below

0
Nick P On

I'll provide a fix in case anyone wants to use it. It's not clear to me whether Win32::OLE is still being maintained, as the issues list at https://rt.cpan.org/Public/Bug/Display.html?id=48858 raised this exact item a number of months ago, so I'm not sure where to submit a patch.

As noted in that link there are a few ways to resolve this (such as giving preference to the win64 entry if both exist), however I've decided it would be best in our case to only query the win64 folder if win32 does not return anything useful.

To do this, in OLE.xs search for win32 and change as below.

err = RegQueryValueA(hKeyLangid, "win32", szFile, &cbFile);

// check win64 if win32 failed
if (err != ERROR_SUCCESS || cbFile <= 1)
    err = RegQueryValueA(hKeyLangid, "win64", szFile, &cbFile);