2

In perl I'm using this code to call the shellabout dialog using win32::api:

my $shellAbout = Win32::API->new('Shell32', 'int ShellAboutA(HWND hWnd, LPCTSTR szApp, LPCTSTR szOtherStuff, HICON hIcon)');  
    $shellAbout->Call (0, 'perl-reguser', 'Editor del registro de usuario', 0);

The above works as expected, but when I try to use the unicode version of shellabout:

my $shellAbout = Win32::API->new('Shell32', 'int ShellAboutW(HWND hWnd, LPCTSTR szApp, LPCTSTR szOtherStuff, HICON hIcon)');  
    $shellAbout->Call (0, 'perl-reguser', 'Editor del registro de usuario', 0);

The strings are not displayed. I have the following declared:

use utf8;
use Encode;
# ..
binmode (STDOUT, ":encoding(utf8)");

any ideas?

Joel
  • 1,805
  • 1
  • 22
  • 22

1 Answers1

3

For W calls, LPCTSTR must be a NUL-terminated string encoded using UTF-16le.

use strict;
use warnings;

use utf8;   # Source code is encoded using UTF-8.

use Encode     qw( encode );
use Win32::API qw( );

my $shellAbout;

sub ShellAbout {
   my ($hWnd, $szApp, $szOtherStuff, $hIcon) = @_;

   $shellAbout ||= Win32::API->new('Shell32', 'int ShellAboutW(HWND hWnd, LPCTSTR szApp, LPCTSTR szOtherStuff, HICON hIcon)');

   $szApp = encode('UTF-16le', $szApp . "\0");
   $szOtherStuff = encode('UTF-16le', $szOtherStuff . "\0") if defined($szOtherStuff);

   $hWnd  //= 0;
   $hIcon //= 0;   

   return $shellAbout->Call($hWnd, $szApp, $szOtherStuff, $hIcon);
}


ShellAbout(undef, 'perl-reguser', 'Editor del registro de usuario', undef)
   or die($^E);
ikegami
  • 367,544
  • 15
  • 269
  • 518