[C++] Command Line Argument

Results 1 to 5 of 5
  1. #1
    Sorcerer Supreme Kudo^ is offline
    Member +Rank
    Jul 2007 Join Date
    PortugalLocation
    425Posts

    [C++] Command Line Argument

    PHP Code:
        int nArgsCnt;
        
    bool btStatus false;
        
    char szTemp[200] = {0};
        
    char szPatch[200] = {0};
        
    LPWSTR   lpCommandLine CommandLineToArgvW(GetCommandLineW(),&nArgsCnt);
        
    sscanf((char*)lpCommandLine,"%s",szTemp);
        
    MessageBox(NULLszPatch szTemp MB_OK|MB_APPLMODAL|MB_ICONERROR);
        
    // i want see run it
        
    if(!strcmpi(szTemp,"start"))
        {
            
    MessageBoxA(NULLszPatch szTemp MB_OK|MB_APPLMODAL|MB_ICONERROR);
            
    ExitProcess(1);
        } 
    This is the code i have now

    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.


  2. #2
    Elite Member LegacyCode is offline
    Member +Rank
    Aug 2009 Join Date
    HyruleLocation
    129Posts

    Re: [C++] Command Line Argument

    A small guide on arguments in C++ > http://www.site.uottawa.ca/~lucia/co...Arguments.html

  3. #3
    Ginger by design. jMerliN is offline
    Grand MasterRank
    Feb 2007 Join Date
    2,500Posts

    Re: [C++] Command Line Argument

    getopt_long

  4. #4
    Sorcerer Supreme Kudo^ is offline
    Member +Rank
    Jul 2007 Join Date
    PortugalLocation
    425Posts

    Re: [C++] Command Line Argument

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

  5. #5
    Ginger by design. jMerliN is offline
    Grand MasterRank
    Feb 2007 Join Date
    2,500Posts

    Re: [C++] Command Line Argument

    Quote Originally Posted by Kudo^ View Post
    Solution:
    PHP Code:
        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/libr...59(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.



Advertisement