Thread: CreateProcess variable definition

  1. #16
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    Where does the documentation say that it reads the ProcessInformation struct? The documentation says:

    lpProcessInformation [out]
    A pointer to a PROCESS_INFORMATION structure that receives identification information about the new process.
    This gives me the impression that there will only be information written to the ProcessInformation struct about a newly created process. Thus leaving garbage values in the ProcessInformation struct will result in the same behaviour as setting everything to zero before passing it to CreateProcess.
    This parameter is reserved

  2. #17
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Ktulu View Post
    Where does the documentation say that it reads the ProcessInformation struct?
    You asked me about StartupInfo, not ProcessInformation.

    But even so, I would still zero out ProcessInformation. That way if CreateProcess fails I can still use an handle out of the struct. Like Codeplug demonstrated, it's not exactly hard to do it.

  3. #18
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    I should have been more specific when I referred to "an uninitialized output argument".

    Quote Originally Posted by Yarin View Post
    But even so, I would still zero out ProcessInformation. That way if CreateProcess fails I can still use an handle out of the struct. Like Codeplug demonstrated, it's not exactly hard to do it.
    That depends a lot on the situation how CreateProcess is being used. When CreateProcess fails it might not have altered the ProcessInformation struct. If you then decide to call CloseHandle with the hProcess and hThread members of ProcessInformation (which makes no sense to me) it might result in undefined behaviour, assuming ProcessInformation is left with garbage values.

    But if in some situation the parent process relies on the successful creation of the new process, meaning it would terminate otherwise, there is absolutely no need to pass the ProcessInformation struct initialized with zero values.
    This parameter is reserved

  4. #19
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by Ktulu View Post
    Question 1: If you're absolutely certain that the process being created will not do anything with the StartupInfo struct, is it safe to pass it to CreateProcess while containing garbage data?
    No, as it contains input / init values and has different size versions (which is why you have to set the CB element to the sizeof the struct).


    Quote Originally Posted by Ktulu View Post
    Question 2: Why zero the ProcessInformation struct before calling CreateProcess when the system will always fill this struct with the appropriate information? Shouldn't it be perfectly fine to pass this struct while it contains garbage data in any case?
    Yes, because it receives output values and does not have different size versions.

    But you will have to check each different structs use by the API to ensure that it is safe.

    So it is easier to just init the structs to zero as it only requres 5 key strokes.

    Quote Originally Posted by Ktulu View Post
    Question 3: In the situation described above is it correct to define/declare these structs as such before calling CreateProcess?
    No.



    Your coding style is up to you. The coding habits you form are up to you.

    If you only write trivial non commercial applications then you will probably be OK taking these type of short cuts.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaration and definition of an external variable.
    By Mr.Lnx in forum C Programming
    Replies: 3
    Last Post: 01-12-2014, 03:02 AM
  2. Declaration and definition of a variable
    By raghu_1991 in forum C Programming
    Replies: 2
    Last Post: 05-11-2013, 02:21 AM
  3. Dynamic variable type definition
    By baxy in forum C++ Programming
    Replies: 3
    Last Post: 12-12-2012, 11:52 PM
  4. Mutual header inclusion breaks variable definition
    By Alexandre in forum C++ Programming
    Replies: 4
    Last Post: 08-22-2011, 10:03 AM
  5. Variable declaration Vs definition
    By callkalpa in forum C Programming
    Replies: 11
    Last Post: 12-17-2009, 12:21 PM