Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Very nice and Very handy package.
I'm having a problem, however... :-)

In short, I'm trying to use Win32::API to call CreateProcess.

All is well except for the LPSTARTUPINFO and LPPROCESS_INFORMATION parms I pass into the call. If I create these using pack(...) for the correct number of bytes, it works fine and I can get the PID back out of the PROCESS_INFORMATION object (my ultimate goal).

If, however, I create these parms using the Win32::API::Struct->new( 'STARTUPINFO' ) approach, the call fails. I'm even setting the .cb field to the size of the structure. The error I get back (using Win32::GetLastError()) is "Invalid access to memory location". Here's the function call I'm making (same in both cases):

<-----------snip------------> # version 1 - succeeds my $si = pack( "L17", ((0) x 17) ); $si = pack( "LL16", length( $si ), ((0) x 16 ) ); my $pi = pack( "L4", ((0) x 4 ) ); # version 2 - fails my $si = Win32::API::Struct->new( "STARTUPINFO" ); $si->{cb} = $si->sizeof(); my $pi = Win32::API::Struct->new( "PROCESS_INFORMATION" ); my $retval = CreateProcess( "c:\\winnt\\notepad.exe", 0, # unused - cmdline ptr, unused 0, # unused - proc attributes ptr 0, # unused - thread attributes ptr 0, # unused - inherit handles 0, # unused - creation flags 0, # unused - env pointer 0, # unused - current dir ptr $si, $pi ); <-----------snip------------>
Should CreateProcess be called the same way in both cases? When a ->new('STARTUPINFO') is called, is the memory zeroed-out upon creation? Any idea why the pack version succeeds and the Struct verion fails?

D. Smith

In reply to Re: Re: Win32::API (finally) by neauva
in thread Win32::API by Guildenstern

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (8)
    As of 2018-04-20 13:32 GMT
    Find Nodes?
      Voting Booth?