http://www.perlmonks.org?node_id=625151

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

Monks,

I'm trying to reparse a Win32 command line using the Win32::API to retrieve it. I've composed the function to get the command line from the examples within Win32::API, but, depending on padding of the code itself, I get an occasional GPF. I believe that Perl is trying to garbage collect the returned "static" string. Any thoughts on this or how to get around it?

I can't find any CPAN modules that retrieve the Win32 command line. So, I'm trying to roll my own. Would like to avoid going all the way to XS as I've never delved into that realm (although I have a working compilation environment with mingw).

Thanks, in advance.

Note: save the following code as "t-getCL.bat" and try "t-getCL a". For me, $string prints and then I get a GPF just before program completion. The GPF is sensitive to code size and less padding at the end of the script can make the problem go away.

Note#2: It's a .bat file to deal correctly with I/O redirection on XP.

@rem = '--*-Perl-*-- @echo off if "%OS%" == "Windows_NT" goto WinNT perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl :WinNT perl -x -S %0 %* if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl if %errorlevel% == 9009 echo You do not have Perl in your PATH. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul goto endofperl @rem '; #!/usr/bin/perl -w #line 15 use Win32::API; Win32::API->Import("kernel32", "LPTSTR GetCommandLine()"); my $string = pack("Z*", GetCommandLine()); print "string[".length($string)."] = '$string'\n"; # ------ padding ----------------------------------------------------- +--------------------------------- __END__ :endofperl