RATNESH TIWARI
RATNESH TIWARI

Reputation: 87

How to pass an argument in the function CreateProcess in Win32

I have an application hello.exe which takes input number by command-line argument and generates Fibonacci number. I want to execute this process by function CreateProcess in Win32.

Here is my file hello.c:

    #include<stdio.h>
    #include<stdlib.h>

    int fib(int n) 
    { 
        if (n <= 1) 
            return n; 
        return fib(n-1) + fib(n-2); 
    } 

    int main(int argc, char *argv[])
    {
        int number = atoi(argv[1]);     // Command Line Argument For Input
        int res = fib(number); 
        printf("\nFibonacci no of %d is: %d\n",number ,res);
        return 0;
    } 

Compiled the above program by : gcc hello.c -o hello

Here is my program for Create Process:

#include<stdio.h>
#include<Windows.h>
#include<string.h>    
int main()
{
    STARTUPINFO si[1];
    PROCESS_INFORMATION pi[1];
    ZeroMemory(&si, sizeof(si));
    ZeroMemory(&pi, sizeof(pi));

    char getApplicationName[][200] = { "C:\\Users\\xyz\\Documents\\Visual Studio 2015\\Projects\\process_CLI\\hello.exe" };
    const int NoOfApplication = sizeof(getApplicationName) / sizeof(getApplicationName[0]);

    //char *fibNumber = "10";
    char *fibNumber = "C:\\Users\\xyz\\Documents\\Visual Studio 2015\\Projects\\process_CLI\\hello.exe  10";

    for (int i = 0; i < NoOfApplication; i++)
    {
        BOOL bCreateProcess = FALSE;
        bCreateProcess = CreateProcess(
            getApplicationName[i],
            fibNumber,
            NULL,
            NULL,
            FALSE,
            0,
            NULL,
            NULL,
            &si[i],
            &pi[i]
        );
        if (bCreateProcess == FALSE)
        {
            printf("\nProcess %d Creation Failed . Its Error Number: %d\n", i, GetLastError());
        }
        else {
            printf("\nProcess Creation Successful\n");
            //printf("\nProcessId: %d\n", GetProcessId(pi[i].hProcess));
            //printf("\nThreadId: %d\n", GetThreadId(pi[i].hThread));
        }
        WaitForSingleObject(pi[i].hProcess, INFINITE);
    }
    for (int i = 0; i < NoOfApplication; i++)
    {
        CloseHandle(pi[i].hProcess);
        CloseHandle(pi[i].hThread);
    }   
    system("PAUSE");
    return 0;
}

I tried diffrent method but unable to pass the argument through the function CreateProcess.

Expected Output:

Process Creation Successful    
Fibonacci no of 10 is: 55

Actual Output:

Process Creation Successful
Fibonacci no of 0 is: 0

Please suggest me correct approach.

Upvotes: 0

Views: 1320

Answers (1)

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

Because you specified a module name as first parameter, the second parameter will be the command line, excluding the module name. So just 10.

lpApplicationName The name of the module to be executed. The lpApplicationName parameter can be NULL. In that case, the module name must be the first white space-delimited token in the lpCommandLine string.

lpCommandLine The command line to be executed. The lpCommandLine parameter can be NULL. In that case, the function uses the string pointed to by lpApplicationName as the command line.

If both lpApplicationName and lpCommandLine are non-NULL, the null-terminated string pointed to by lpApplicationName specifies the module to execute, and the null-terminated string pointed to by lpCommandLine specifies the command line.

So either provide a module name plus command line as lpApplicationName or as lpCommandLine, but not both, or provide the module name as first parameter and the command line as the second.


EDIT See the comment of Eryk Sun; however, the documentation does not require in this case that a path name with spaces be contained in quotes:

The lpApplicationName parameter can be NULL. In that case, the module name must be the first white space-delimited token in the lpCommandLine string. If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin; otherwise, the file name is ambiguous. For example, consider the string "c:\program files\sub dir\program name". This string can be interpreted in a number of ways. The system tries to interpret the possibilities in the following order:

c:\program.exe files\sub dir\program name

c:\program files\sub.exe dir\program name

c:\program files\sub dir\program.exe name

c:\program files\sub dir\program name.exe

However, since lpApplicationName in OP's code is not NULL and since lpApplicationName cannot contain command line arguments, lpApplicationName does not need to be a quoted string if this name contains spaces.

Upvotes: 3

Related Questions