Stack Exchange
createprocess returning 0 with getlasterror set to 2
I have to execute a class file by providing the jar file path.Below is my program, 
But createprocess is returning 0 with getlasterror set to 2.
Tried using sysnative instead of system32 still its not working.
Can anyone point me what is going wrong here:
char *command;
char *cmdname = "c:\\windows\\system32\\cmd.exe /C ";
    char *p = " java -cp ";
    char *p1 = " com.ge.med.terra.eaaa.server.EA3Server %*";
   command = (char *)malloc(50);

    env = getenv("GEHC_SECURITY_HOME");
    strcpy(command, cmdname);
    strcat(command, "\"");
    strcat(command, p);
    strcat(command, "\"");
    strcat(command,env);
    strcat(command,"\\eat\\jar\\eat.jar\";");
    strcat(command, p1);
    strcat(command, "\"");

    result = CreateProcessA( NULL,   // No module name (use command line)
        command,        // 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 ) ;  


How is command declared?

its char *command; and I am doing command = malloc(50);

what's the error you're getting? return value zero is indicating that the process could not be created, use FormatMessage with the dwMessageId set to the return value of GetLastError to get the error message

you're probably just formatting your command incorrectly, like a forgotten whitespace in strcat(command, "\""); before inserting env or something

You should print the command to the screen and paste it here--presumably the error is in there somewhere.

You tagged this question with c++. For this reason, I would suggest you use C++ std::string class. That way, you can easily concatenate the strings necessary to create you command. It is much more concise, easier to read, all memory management is done by the class in the background (no need for malloc), and you do not need to worry about terminating the string with (maybe not for this example but in general). Here is how you do it:
std::string command = "c:\\windows\\system32\\cmd.exe /C \" java -cp \"" + std::string(env)
    + "\\eat\\jar\\eat.jar\";\"";

result = CreateProcessA(NULL,   // No module name (use command line)
    command.c_str(),        // 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);


You're overflowing the buffer you allocated. You allocate space for 50 characters, but you write 105 plus whatever is in GEHC_SECURITY_HOME. When this happens, you write into some other allocated block, which will produce unspecified but undesirable behavior.
Using std::string will eliminate this, but the + operator is slow. Using std::stringstream is better if you care about the performance. But to test out whether this is the only problem, you can change your 50 to something like 500 and try again.
