command line options in winmain with __argv

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eric dexter
    New Member
    • Sep 2006
    • 46

    #1

    command line options in winmain with __argv

    I want to compare what I get on the command line in winmain with an if statement.. I am getting a beginers error but I don't have an example to use to fix it.. cannot convert from 'const int' to 'char *' in this line if (__argv[1] = 'file'){};
    I am very lost with this error and any help would be apreaceated

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, //handle to current instance
    				   HINSTANCE hPrevInstance, //pointer to the previous instance
    				   LPSTR lpCmdLine,  //pointer to the command file
    				   int nCmdShow)     //show state of the window
    {
    	//
    
        HWAVEOUT hWaveOut;  //handle to sound card output
        WAVEFORMATEX WaveFormat;  //The sound format
    	WAVEHDR WaveHeader;       //Wave header for our sound data
    	
    	char Data[BUFFERSIZE];  //sound data buffer
    
    	HANDLE Done;  // Event handle that tells us the sound has finished being played.
    				  // This is a real efficient way to put the program to sleep
    					//The sound card is processing the sound buffer
    
    	double x;
    	int ii;
    	int i;
    	int FREQUENCY;
    	FILE * pFile; //this is set up to read from a file
        char string [100]; //this is the file we are reading
    	struct wordd line[20]; //create an array of 20 words
    	char * pch; //from strtok example
    
    
    	//if command line is file
        if (__argv[1] == "file")
    	{
    		  
    	      pFile = fopen (__argv[2] , "r"); //second argument is filename
    		  if (pFile == NULL) perror ("Error opening file");
    		  else {
                  fgets (string , 100 , pFile); //get a line from a file
    			  pch = strtok (string," "); //split string into token seperated by a space
    			  i = 0; // iterator starts at 0
    			  while (pch != NULL) // while there is something to split 
    			  {
    				  i++; //this indicates where you are in the loop not used here but for more advanced synths
    			//	  line[i].word = pch; //put pch in place in the line for this synth freq is first word
    				  ii = atoi(pch);
    			      pch = strtok (NULL, " "); //grab next word
    				  //I will need to have an array of pch to do more paremeters in a synth..  redo the line maybe..
    				  playsound(ii);
    			  }
    			  //ii = atoi(pch); //convert line to a number using atoi
    			 // playsound(ii);      //play the sound..
    
    
                  //puts (string); prints line I think
                  fclose (pFile);
    
       }
    
    }
    	if (__argv[1] = 'file'){};
    	     //playsound(int freq)}
    	if (isdigit(__argv[1][1])) //if this is a number play the frequency
    	{
    	     i = atoi(__argv[1]);
    		 playsound(i);
    	}
    	//if 
    	//string  = __argv[1];
    	//if (__argv[1] = 'file'){};
    	
    //** Release our event handle **
     
     return FALSE;
    
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    WinMain() is not main().

    There is no command line in a Windows program and the arguments on the command line have to be fetched using GetCommandLine( ).

    All that argc argv stuff is for native language console applications only.

    Comment

    • eric dexter
      New Member
      • Sep 2006
      • 46

      #3
      It seems to compile fine and one of the if's is working it is when I look for a string instead of a charecter it complains.. is there an example of the other somewhere where I compare what I get with your command??? I saw that but didn't understand how to use it (no good examples)

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You may need to post your revised code.

        GetCommandLine( ) returns an LPTSTR, which is a char* if you are using ASCII and a wchar_t* if you are using Unicode.

        Various C functions have been rewritten for Unicode. When you have your peoject set for ASCII you want the ANSI C version and for Unicode you want the ANSI C wide character version. You switch between these two by using the TCHAR mappings (Please read up on this).

        The function mappings are http://msdn2.microsoft.com/en-us/library/ms860358.aspx.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by eric dexter
          I want to compare what I get on the command line in winmain with an if statement.. I am getting a beginers error but I don't have an example to use to fix it.. cannot convert from 'const int' to 'char *' in this line if (__argv[1] = 'file'){};
          I am very lost with this error and any help would be apreaceated

          Code:
          int WINAPI WinMain(HINSTANCE hInstance, //handle to current instance
          				   HINSTANCE hPrevInstance, //pointer to the previous instance
          				   LPSTR lpCmdLine,  //pointer to the command file
          				   int nCmdShow)     //show state of the window
          {
          <snipped>
           return FALSE;
          
          }
          The command line is accessable directly in WinMain via the lpCmdLine function argument. I do not think using __argc and __argp is a good idea, I do not believe these are documented/supported features. Thatis you have no guarantee that they will continue to exist from 1 version of the SDK to the next.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You can also create a Windows Console application and that will generate an _tmain(int argc, _TCHAR* argv[]).

            Also note that using lpCmdLine does not retrieve the entire command line but only the first argv* portion. The type of lpCmdLine is a LPSTR so you can have only one argument. Usually, this is a file name to be read in that contains the other arguments.

            If you are using Unicode, then you have to use GetCommandLine( ). There is a CommandLineToAr gvW() that you can use to recreate the argv string array.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by weaknessforcats
              Also note that using lpCmdLine does not retrieve the entire command line but only the first argv* portion. The type of lpCmdLine is a LPSTR so you can have only one argument. Usually, this is a file name to be read in that contains the other arguments.
              This is not true, the lpCmdLine is the entire command line concatinated into a single string excluding what would be in argv[0] (i.e. the program name).

              Try this program with multiple command line arguments

              [code=cpp]
              #include "windows.h"

              int APIENTRY WinMain(HINSTAN CE hInstance,
              HINSTANCE hPrevInstance,
              LPSTR lpCmdLine,
              int nCmdShow)
              {
              MessageBox(NULL , lpCmdLine, "Command Line", MB_OK);

              return 0;
              }
              [/code]

              The difference between lpCmdLine and GetCommandLine is that lpCmdLine is type LPSTR (for historical reasons) where as lpCmdLine returns LPTSTR. This means that lpCmdLine is always a string of ASCII characters, but GetCommandLine can return a unicode string in a unicode enabled application.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                I stand corrected. But:
                Originally posted by Banfa
                #include "windows.h"

                int APIENTRY WinMain(HINSTAN CE hInstance,
                HINSTANCE hPrevInstance,
                LPSTR lpCmdLine,
                int nCmdShow)
                {
                MessageBox(NULL , lpCmdLine, "Command Line", MB_OK);

                return 0;
                }


                The difference between lpCmdLine and GetCommandLine is that lpCmdLine is type LPSTR (for historical reasons) where as lpCmdLine returns LPTSTR. This means that lpCmdLine is always a string of ASCII characters, but GetCommandLine can return a unicode string in a unicode enabled application.
                The MessageBox line only works with 16-bit Windows. MessageBox today is a macro that switches between MessageBoxA (ASCII) and MessageBoxW (Unicode). By default Windows programs are Unicode. That means you need the TCHAR mapping for this to compile. The code should be:
                [code=cpp]
                MessageBox(NULL , GetCommandLine( ), TEXT("Command Line"), MB_OK);
                [/code]

                You can't use lpCmdLine since this is always ASCII. For Unicode you would need to convert this to a Unicode string before passing it to MessageBox. Therefore, the current advice is to always use GetCommandLine( ).

                Also, I think there is a typo in your reply:
                Originally posted by Banfa
                where as lpCmdLine returns LPTSTR.
                I think you mean to say:

                whereas GetCommandLine( ) returns a LPTSTR.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Originally posted by weaknessforcats
                  Also, I think there is a typo in your reply:

                  I think you mean to say:

                  whereas GetCommandLine( ) returns a LPTSTR.
                  Yes I did, my sloppy writing and coding aside I was just making the point that lpCmdLine points to the entire command line, but is a pointer to an ASCII string.

                  Comment

                  • eric dexter
                    New Member
                    • Sep 2006
                    • 46

                    #10
                    I saw this as an example. I haven't figured out how to split the line to get an array of what is on the command line (or if it is doing that how it is being done)

                    Code:
                    void CCommandLineDlg::OnBtnCmdLine() 
                    {
                    	// TODO: Add your control notification handler code here
                    	char CmdLine[80];
                    	char CmdResult[80];
                    
                    	strcpy(CmdLine, GetCommandLine());
                    	sprintf(CmdResult, "%s", CmdLine);
                    	m_CommandLine.Format("%s", CmdResult);
                    
                    	UpdateData(FALSE);
                    }
                    this is from
                    http://www.functionx.com/visualc/Lesson06.htm

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      Originally posted by Eric Dexter
                      void CCommandLineDlg ::OnBtnCmdLine( )
                      {
                      // TODO: Add your control notification handler code here
                      char CmdLine[80];
                      char CmdResult[80];

                      strcpy(CmdLine, GetCommandLine( ));
                      sprintf(CmdResu lt, "%s", CmdLine);
                      m_CommandLine.F ormat("%s", CmdResult);

                      UpdateData(FALS E);
                      }
                      I hope you understand that this is ASCII code and most Windows code is Unicode. You are not using the TCHAR mappings.
                      [code=c]
                      void CCommandLineDlg ::OnBtnCmdLine( )
                      {
                      // TODO: Add your control notification handler code here
                      TCHAR CmdLine[80];
                      TCHAR CmdResult[80];

                      _tcsncpy(CmdLin e, GetCommandLine( ));
                      _stprintf(CmdRe sult, TEXT("%s"), CmdLine);
                      m_CommandLine.F ormat(TEXT("%s" ), CmdResult);

                      UpdateData(FALS E);
                      }
                      [/code]

                      Plus, strcpy, and sprintf are deprecated by Microsoft. You are supposed to use the newer safe _s versions.

                      Plus you have a hard-coded 80 that is a crash waiting to happen.

                      Comment

                      • eric dexter
                        New Member
                        • Sep 2006
                        • 46

                        #12
                        Those are good things to know certainly but I am using a compiler that was made by microsoft circa 2001 (and a borland c compiler from the same era).. It also doesn't realy answer the question as far as how to make that an array of strings from the command line. I am having a hard time finding an example on the internet.. The code I am trying to add a command line is here (if that is helpfull and it may not be)

                        http://dexrowem.blogsp ot.com/2007/11/command-line-synth.html

                        Comment

                        • weaknessforcats
                          Recognized Expert Expert
                          • Mar 2007
                          • 9214

                          #13
                          You will have to post your code. I don't navigate to to other sites.

                          Remember, GetCommandLine fetches the entire command line as one string.
                          You will have to parse that string looking for the data that constitues your strings. Then you:
                          1) aallocate memory for the string as a char array
                          2) copy data from the command line to your allocation
                          3) add the address of the string to an array of char* that is you array of strings.

                          Comment

                          Working...