wide char和win32 :: api

时间:2015-09-06 16:48:34

标签: perl winapi

在perl中,我使用此代码使用win32 :: api调用shellabout对话框:

<iframe width="560" height="315" src="https://www.youtube.com/embed/2EIeUlvHAiM" frameborder="0" allowfullscreen></iframe>

以上是按预期工作的,但是当我尝试使用shellabout的unicode版本时:

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);

不显示字符串。我声明了以下内容:

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);

任何想法?

1 个答案:

答案 0 :(得分:3)

对于W次调用,LPCTSTR必须是使用UTF-16le编码的以NUL结尾的字符串。

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);