2 Replies - 223 Views - Last Post: 26 November 2012 - 12:08 PM Rate Topic: -----

#1 KiddKoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-October 10

Process creation in Windows using "if" statement

Posted 25 November 2012 - 08:37 PM

This is really bothering me. I thought if statements only checked for a condition. However, this if statement actually creates the process or prints a failure if it doesn't work. Am I missing something about the capabilities of if statements?

if( !CreateProcess( NULL,   // No module name (use command line)
        argv[1],        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    } 

This post has been edited by KiddKoder: 25 November 2012 - 08:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Process creation in Windows using "if" statement

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Process creation in Windows using "if" statement

Posted 25 November 2012 - 09:17 PM

Quote

I thought if statements only checked for a condition.

That is correct, but the if statement is not creating a process. The CreateProcess function doing all the work. The function is evaluated before the if statement checks the condition. CreateProcess returns a value after it is finished executing. The NOT of that return value is considered the condition which the if statement checks.

i.e.
#include <stdio.h>

int printStr(char *str) {
	printf("In printStr(): %s\n", str);
	return 1; //return true
}
int main() {

	if(printStr("Hello"))
		printf("In main(): %s\n", "Hello");
	return 0;

}


Notice how execution jumps to printStr before evaluating the if statement. This should not be considered odd behavior, take basic arithmetic for instance.

For example, look at the following expression

2 * (2 * (2 - 2))

Remember that the most inner parenthesis are evaluated first

This post has been edited by jjl: 25 November 2012 - 09:22 PM

Was This Post Helpful? 0
  • +
  • -

#3 KiddKoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-October 10

Re: Process creation in Windows using "if" statement

Posted 26 November 2012 - 12:08 PM

Excellent explanation. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1