[C++] Command Line Argument

Junior Spellweaver
Joined
Jul 5, 2007
Messages
181
Reaction score
9
PHP:
    int nArgsCnt;
    bool btStatus = false;
    char szTemp[200] = {0};
    char szPatch[200] = {0};
    LPWSTR   * lpCommandLine = CommandLineToArgvW(GetCommandLineW(),&nArgsCnt);
    sscanf((char*)lpCommandLine,"%s",szTemp);
    MessageBox(NULL, szPatch , szTemp , MB_OK|MB_APPLMODAL|MB_ICONERROR);
    // i want see run it
    if(!strcmpi(szTemp,"start"))
    {
        MessageBoxA(NULL, szPatch , szTemp , MB_OK|MB_APPLMODAL|MB_ICONERROR);
        ExitProcess(1);
    }
This is the code i have now :blush:

What i want to do is add a command line argument check, if dont have the argument "start" when launching the programm it will simply throw you a messagebox saying you are not allowed to run.

What is the argument i am talking about, it's this:
C:\Folder\Application.exe start

If you have any idea on how to solve this, please reply here. Thanks in advance.
 
Solution:
PHP:
    char * lpCommandLine = GetCommandLineA();
    if(strstr(lpCommandLine,"start"))
    return;
    else
    ExitProcess(1);

Thread can be closed ;)
 
Solution:
PHP:
    char * lpCommandLine = GetCommandLineA();
    if(strstr(lpCommandLine,"start"))
    return;
    else
    ExitProcess(1);

Thread can be closed ;)

Not sure why you're using GetCommandLineA, considering the command line is passed to your main function.

Also, strstr won't check for the existence of the command line argument 'start', instead, it searches of that string exists anywhere within the command line, whether it be an argument or not. For instance, if I rename your program to start.exe, then just run it, the above code would execute -- not the intended effect.


http://msdn.microsoft.com/en-us/library/ms633559(VS.85).aspx (for WinMain), and main intrinsically has argc/argv.

ANSI console processes written in C can use the argc and argv arguments of the main function to access the command-line arguments. ANSI GUI applications can use the lpCmdLine parameter of the WinMain function to access the command-line string, excluding the program name. The main and WinMain functions cannot return Unicode strings.
 
Back