468,457 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

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

Expand|Select|Wrap|Line Numbers
  1. int WINAPI WinMain(HINSTANCE hInstance, //handle to current instance
  2.                    HINSTANCE hPrevInstance, //pointer to the previous instance
  3.                    LPSTR lpCmdLine,  //pointer to the command file
  4.                    int nCmdShow)     //show state of the window
  5. {
  6.     //
  7.  
  8.     HWAVEOUT hWaveOut;  //handle to sound card output
  9.     WAVEFORMATEX WaveFormat;  //The sound format
  10.     WAVEHDR WaveHeader;       //Wave header for our sound data
  11.  
  12.     char Data[BUFFERSIZE];  //sound data buffer
  13.  
  14.     HANDLE Done;  // Event handle that tells us the sound has finished being played.
  15.                   // This is a real efficient way to put the program to sleep
  16.                     //The sound card is processing the sound buffer
  17.  
  18.     double x;
  19.     int ii;
  20.     int i;
  21.     int FREQUENCY;
  22.     FILE * pFile; //this is set up to read from a file
  23.     char string [100]; //this is the file we are reading
  24.     struct wordd line[20]; //create an array of 20 words
  25.     char * pch; //from strtok example
  26.  
  27.  
  28.     //if command line is file
  29.     if (__argv[1] == "file")
  30.     {
  31.  
  32.           pFile = fopen (__argv[2] , "r"); //second argument is filename
  33.           if (pFile == NULL) perror ("Error opening file");
  34.           else {
  35.               fgets (string , 100 , pFile); //get a line from a file
  36.               pch = strtok (string," "); //split string into token seperated by a space
  37.               i = 0; // iterator starts at 0
  38.               while (pch != NULL) // while there is something to split 
  39.               {
  40.                   i++; //this indicates where you are in the loop not used here but for more advanced synths
  41.             //      line[i].word = pch; //put pch in place in the line for this synth freq is first word
  42.                   ii = atoi(pch);
  43.                   pch = strtok (NULL, " "); //grab next word
  44.                   //I will need to have an array of pch to do more paremeters in a synth..  redo the line maybe..
  45.                   playsound(ii);
  46.               }
  47.               //ii = atoi(pch); //convert line to a number using atoi
  48.              // playsound(ii);      //play the sound..
  49.  
  50.  
  51.               //puts (string); prints line I think
  52.               fclose (pFile);
  53.  
  54.    }
  55.  
  56. }
  57.     if (__argv[1] = 'file'){};
  58.          //playsound(int freq)}
  59.     if (isdigit(__argv[1][1])) //if this is a number play the frequency
  60.     {
  61.          i = atoi(__argv[1]);
  62.          playsound(i);
  63.     }
  64.     //if 
  65.     //string  = __argv[1];
  66.     //if (__argv[1] = 'file'){};
  67.  
  68. //** Release our event handle **
  69.  
  70.  return FALSE;
  71.  
  72. }
  73.  
  74.  
Nov 22 '07 #1
12 16521
weaknessforcats
9,207 Expert Mod 8TB
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.
Nov 22 '07 #2
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)
Nov 23 '07 #3
weaknessforcats
9,207 Expert Mod 8TB
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.
Nov 24 '07 #4
Banfa
9,051 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. int WINAPI WinMain(HINSTANCE hInstance, //handle to current instance
  2.                    HINSTANCE hPrevInstance, //pointer to the previous instance
  3.                    LPSTR lpCmdLine,  //pointer to the command file
  4.                    int nCmdShow)     //show state of the window
  5. {
  6. <snipped>
  7.  return FALSE;
  8.  
  9. }
  10.  
  11.  
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.
Nov 24 '07 #5
weaknessforcats
9,207 Expert Mod 8TB
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 CommandLineToArgvW() that you can use to recreate the argv string array.
Nov 25 '07 #6
Banfa
9,051 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. #include "windows.h"
  2.  
  3. int APIENTRY WinMain(HINSTANCE hInstance,
  4.                      HINSTANCE hPrevInstance,
  5.                      LPSTR     lpCmdLine,
  6.                      int       nCmdShow)
  7. {
  8.     MessageBox(NULL, lpCmdLine, "Command Line", MB_OK);
  9.  
  10.     return 0;
  11. }
  12.  
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.
Nov 26 '07 #7
weaknessforcats
9,207 Expert Mod 8TB
I stand corrected. But:
#include "windows.h"

int APIENTRY WinMain(HINSTANCE 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:
Expand|Select|Wrap|Line Numbers
  1. MessageBox(NULL, GetCommandLine(), TEXT("Command Line"), MB_OK);
  2.  
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:
where as lpCmdLine returns LPTSTR.
I think you mean to say:

whereas GetCommandLine() returns a LPTSTR.
Nov 26 '07 #8
Banfa
9,051 Expert Mod 8TB
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.
Nov 26 '07 #9
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)

Expand|Select|Wrap|Line Numbers
  1. void CCommandLineDlg::OnBtnCmdLine() 
  2. {
  3.     // TODO: Add your control notification handler code here
  4.     char CmdLine[80];
  5.     char CmdResult[80];
  6.  
  7.     strcpy(CmdLine, GetCommandLine());
  8.     sprintf(CmdResult, "%s", CmdLine);
  9.     m_CommandLine.Format("%s", CmdResult);
  10.  
  11.     UpdateData(FALSE);
  12. }
  13.  
this is from
http://www.functionx.com/visualc/Lesson06.htm
Nov 27 '07 #10
weaknessforcats
9,207 Expert Mod 8TB
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);
}
I hope you understand that this is ASCII code and most Windows code is Unicode. You are not using the TCHAR mappings.
Expand|Select|Wrap|Line Numbers
  1. void CCommandLineDlg::OnBtnCmdLine() 
  2. {
  3.     // TODO: Add your control notification handler code here
  4.     TCHAR CmdLine[80];
  5.     TCHAR CmdResult[80];
  6.  
  7.     _tcsncpy(CmdLine, GetCommandLine());
  8.     _stprintf(CmdResult, TEXT("%s"), CmdLine);
  9.     m_CommandLine.Format(TEXT("%s"), CmdResult);
  10.  
  11.     UpdateData(FALSE);
  12. }
  13.  
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.
Nov 27 '07 #11
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.blogspot.com/2007/11/command-line-synth.html
Nov 29 '07 #12
weaknessforcats
9,207 Expert Mod 8TB
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.
Nov 29 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Edvard Majakari | last post: by
7 posts views Thread by Ken Innes | last post: by
2 posts views Thread by Dr. Laurence Leff | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.