in reply to Re^4: Win32::API Memory Exception with GetCommandLine() (which returns a static string)
in thread Win32::API Memory Exception with GetCommandLine() (which returns a static string)

I've just noticed that if I take the OP's code out of the batch file, it works ok (no faults) though it still returns the wrong string.

Also, if I replace the OP's code in the batch file with:
use Win32::API; $function = new Win32::API("kernel32", "GetCommandLine", '', 'P'); $string = $function->Call(); print "string[".length($string)."] = '$string'\n";
then the batch file runs without error and $string no longer contains the additional trailing space - ie Win32::API and Inline are in complete agreement.

A problem with the Import method perhaps ? ... I've never trusted it.

Cheers,
Rob
Update: As BrowserUk has subsequently demonstrated, it's the pack('Z*', ...) that leads to the extra trailing space - nothing to do with Win32::API->Import() at all.
  • Comment on Re^5: Win32::API Memory Exception with GetCommandLine() (which returns a static string)
  • Select or Download Code

Replies are listed 'Best First'.
Re^6: Win32::API Memory Exception with GetCommandLine() (which returns a static string)
by BrowserUk (Patriarch) on Jul 06, 2007 at 11:29 UTC

    The extra trailing space is explainable to the weird use the OP is making of pack:

    #!/usr/bin/perl -w use strict; use Win32::API; Win32::API->Import("kernel32", "LPTSTR GetCommandLine()"); my $cl = GetCommandLine(); printf "%d : '%s'\n", length( $cl ), $cl; my $cl2 = pack 'Z*', GetCommandLine(); printf "%d : '%s'\n", length( $cl2 ), $cl2; __END__ C:\test>junk3 46 : '"c:\perl\bin\perl.exe" -sw "C:\test\junk3.pl" ' 47 : '"c:\perl\bin\perl.exe" -sw "C:\test\junk3.pl" '

    I'm lost as to why the combination of pack, mingw built perl and a batch file would cause a segfault?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I'm lost as to why the combination of pack, mingw built perl and a batch file would cause a segfault?

      I'm still at a loss on how to fix this as well. But you've all given me a couple of other avenues to take in order to tackle the issue.

      Thanks.