The method of passing argv [] to CreateProcess ()

My C Win32 application should allow the transfer of the full command line to run another program, for example.

myapp.exe /foo /bar "C:\Program Files\Some\App.exe" arg1 "arg 2"

myapp.exe may look something like

int main(int argc, char**argv)
{
  int i;

  for (i=1; i<argc; ++i) {
     if (!strcmp(argv[i], "/foo") {
        // handle /foo
     } else if (!strcmp(argv[i], "/bar") {
        // handle /bar
     } else {
        // not an option => start of a child command line
        break;
     }
  }

  // run the command
  STARTUPINFO si;
  PROCESS_INFORMATION pi;
  // customize the above...

  // I want this, but there is no such API! :(
  CreateProcessFromArgv(argv+i, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

  // use startup info si for some operations on a process
  // ...
}

I might think of some workarounds:

Both of them are long and reimplement the cumbersome logic of the Windows command line, which is already part of it CommandLineToArgvW().

Is there a "standard" solution for my problem? Standard (Win32, CRT, etc.) Implementing workarounds is considered a solution.

+3
5

, .

1) API, GetCommandLine(),

myapp.exe /foo /bar "C:\Program Files\Some\App.exe" arg1 "arg 2"

2) CreateProcess() ,

CreateProcess(NULL, "c:\\hello.exe arg1 arg2 etc", ....) 

, .

3) , , exe, CreateProcess().

char* cmd_pos = strstr(GetCommandLine(), argv[3]);

, : CreateProcess(NULL, strstr(GetCommandLine(), argv[i]), ...);

EDIT: , . , .

+5

. , , GetCommandLine(), .

Microsoft .

.

Solve, .

E.g. 
     "a b c" d e

First Part: "a b c"
Next Parameter Start: d e

Microsoft, . Solve, argv.

test.c

#include <stdio.h>

extern char* Solve(char* p);

void showString(char *str)
{
    char *end = Solve(str);

    char *p = str;

    printf("First Part: ");
    while(p < end){
        fputc(*p, stdout); 
        p++;
    }

    printf("\nNext Parameter Start: %s\n", p + 1);
}

int main(){
    char str[] = "\"a b c\" d e";
    char str2[] = "a\\\\b d\"e f\"g h";
    char str3[] = "a\\\\\\\"b c d";
    char str4[] = "a\\\\\\\\\"b c\" d e";

    showString(str);
    showString(str2);
    showString(str3);
    showString(str4);

    return 0;
}

:

First Part: "a b c"
Next Parameter Start: d e
First Part: a\\b
Next Parameter Start: d"e f"g h
First Part: a\\\"b
Next Parameter Start: c d
First Part: a\\\\"b c"
Next Parameter Start: d e

Solve, findarg.c

/**

This is a FSM for quote recognization.

Status will be 
    1. Quoted. (STATUS_QUOTE)
    2. Normal. (STATUS_NORMAL)
    3. End.    (STATUS_END)

    Quoted can be ended with a " or \0
    Normal can be ended with a " or space( ) or \0

    Slashes
*/

#ifndef TRUE
#define TRUE 1
#endif

#define STATUS_END    0
#define STATUS_NORMAL 1
#define STATUS_QUOTE  2

typedef char * Pointer;
typedef int STATUS;

static void MoveSlashes(Pointer *p){

    /*According to Microsoft note, http://msdn.microsoft.com/en-us/library/17w5ykft.aspx */
    /*Backslashes are interpreted literally, unless they immediately precede a double quotation mark.*/

    /*Here we skip every backslashes, and those linked with quotes. because we don't need to parse it.*/
    while (**p == '\\'){

        (*p)++;

        //You need always check the next element
        //Skip \" as well.
        if (**p == '\\' || **p == '"')
            (*p)++;

    }
}

/*    Quoted can be ended with a " or \0  */
static STATUS SolveQuote(Pointer *p){
    while (TRUE){
        MoveSlashes(p);
        if (**p == 0)
            return STATUS_END;

        if (**p == '"')
            return STATUS_NORMAL;

        (*p)++;
    }
}

/* Normal can be ended with a " or space( ) or \0 */
static STATUS SolveNormal(Pointer *p){
    while (TRUE){
        MoveSlashes(p);
        if (**p == 0)
            return STATUS_END;

        if (**p == '"')
            return STATUS_QUOTE;

        if (**p == ' ')
            return STATUS_END;

        (*p)++;
    }
}

/*
    Solve the problem and return the end pointer.

    @param p The start pointer

    @return The target pointer.
*/
Pointer Solve(Pointer p){

    STATUS status = STATUS_NORMAL;

    while (status != STATUS_END){
        switch (status)
        {
        case STATUS_NORMAL:
            status = SolveNormal(&p); break;

        case STATUS_QUOTE:
            status = SolveQuote(&p); break;

        case STATUS_END:
        default:
            break;
        }

        //Move pointer to the next place.
        if (status != STATUS_END)
            p++;
    }

    return p;
}
+1

, , .

. http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx

, , argv ( CommandLineToArgv (, , , ) - CRT, argv main()), , .

: , , / . , ..

0

: Visual Studio , C. , VC\crt\src\stdargv.c, wparse_cmdline, argc argv GetCommandLineW API. , "cmdv", , argv. argv, , "" CreateProcess, cmdv [i].

This solution has the benefits of using the same syntax code, still provides argv as usual, and allows you to pass the original without having to re-quote or re-exit it.

0
source

Source: https://habr.com/ru/post/1770620/


All Articles