Click to Rate and Give Feedback
MSDN
MSDN Library
Windows Development
System Services
 GetCommandLine function
GetCommandLine function

Applies to: desktop apps only

Retrieves the command-line string for the current process.

Syntax

LPTSTR WINAPI GetCommandLine(void);

Parameters

This function has no parameters.

Return value

The return value is a pointer to the command-line string for the current process.

Remarks

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.

Unicode console process written in C can use the wmain or _tmain function to access the command-line arguments. Unicode GUI applications must use the GetCommandLineW function to access Unicode strings.

To convert the command line to an argv style array of strings, call the CommandLineToArgvW function.

Note  The name of the executable in the command line that the operating system provides to a process is not necessarily identical to that in the command line that the calling process gives to the CreateProcess function. The operating system may prepend a fully qualified path to an executable name that is provided without a fully qualified path.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

Unicode and ANSI names

GetCommandLineW (Unicode) and GetCommandLineA (ANSI)

See also

CreateProcess
Process and Thread Functions

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
free memory      mezdej   |   Edit   |   Show History

@webJose memory automatically released when app is closed.

Theoretically you can free that memory with LocalFree( pointer ); but it's bad idea because GetCommandLine always return the same ptr.

Tags What's this?: Add a tag
Flag as ContentBug
Key Information is Missing      webJose   |   Edit   |   Show History
This is an unusual API in the sense that it doesn't request the buffer to write the string into. Instead, it returns the string. This document, however, fails to document how to free the returned buffer, if such action is at all needed.
Tags What's this?: Add a tag
Flag as ContentBug
Another way to get the Unicode command line      Yuhong Bao   |   Edit   |   Show History
Another way to get the Unicode command line is to use the wWinMain or wmain function instead of WinMain or main. For ANSI/Unicode portablity use _tWinMain or _tmain.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker