GetCommandLine

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.
What exactly is your problem with it?
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
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
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
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
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.
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.
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.
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;
}
LocalFree()? Where's that documented?
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.