GetCommandLine

Mar 25, 2011 at 1:35pm
hi, I am a new C++ user. I need help in using GetCommandLine Function, I want for example a simple program that copy a file using GetCommandLine Function.
Mar 25, 2011 at 2:14pm
What exactly is your problem with it?
Mar 25, 2011 at 2:22pm
closed account (zb0S216C)
What exactly is your problem with it?

I don't think he has a problem, Hanst. I think he just want's to know how to use a particular method.
Last edited on Mar 25, 2011 at 2:22pm
Mar 25, 2011 at 2:31pm
http://msdn.microsoft.com/en-us/library/ms683156%28v=vs.85%29.aspx

There, reference. Example:
EDIT: forget the example, it doesn't work quite as I thought it would.
Last edited on Mar 25, 2011 at 2:38pm
Mar 25, 2011 at 3:12pm
Thank you all. It's true Hanst as Framework said I don't know how to use it. I want a simple example to learn from it. I am sorry but I am as a said a new C++ user
Mar 25, 2011 at 3:50pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <windows.h>
#include <sstream>
int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR szCmdLine,
				   int iCmdShow)
{
     std::stringstream ss;
     ss<<GetCommandLineA();
     std::string str;
     ss>>str;
     std::cout<<str;
     return 0;
}


This will print the name of the program to cout. I tried this with MinGW, and somehow I couldn't get it to output it on the command line, but outputting on a file works.
Last edited on Mar 25, 2011 at 3:51pm
Mar 25, 2011 at 4:36pm
GetCommandLine gives you the entire command line as passed to the program.

If you want to use the arguments, you're better off looking at argc and argv as passed to main. If you're using a Windows app with WinMain, they're still available as global variables __argc, __argv.
Mar 25, 2011 at 4:44pm
I am not very sure about __argc and __argv. I don't think those are standard.

Then again, it doesn't even matter - you only need GetCommandLine when working with Unicode parameters, otherwise you can just use the szCmdLine parameter.
Mar 25, 2011 at 5:15pm
I am not very sure about __argc and __argv. I don't think those are standard.
They are on Windows.

I believe Borland followed the spec and implemented _argc and _argv, that aside it's been there since Windows 3.0.
Mar 25, 2011 at 6:51pm
Use this to get argc/argv style when building a windows GUI application (this must work with every compiler):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <shellapi.h>
int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR szCmdLine,
				   int iCmdShow)
{

LPWSTR *szArglist;
	int nArgs = 0;
	szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);



// do your work here


// Free memory allocated for CommandLineToArgvW arguments.
	LocalFree(szArglist);
return 0;
}
Mar 25, 2011 at 9:41pm
LocalFree()? Where's that documented?
Mar 25, 2011 at 10:01pm
Besides, why would you need to free memory allocated by the system right before the program quits? That memory would be freed either way?
Topic archived. No new replies allowed.