Click to Rate and Give Feedback
MSDN
MSDN Library
User Interface
Windows Shell
Shell Reference
Shell Functions
 CommandLineToArgvW Function
CommandLineToArgvW Function

Parses a Unicode command line string and returns an array of pointers to the command line arguments, along with a count of such arguments, in a way that is similar to the standard C run-time argv and argc values.

Syntax

LPWSTR *CommandLineToArgvW(      
    LPCWSTR lpCmdLine,     int *pNumArgs );

Parameters

lpCmdLine
[in] 

Pointer to a null-terminated Unicode string that contains the full command line. If this parameter is an empty string the function returns the path to the current executable file.

pNumArgs
[out] Pointer to an int that receives the number of array elements returned, similar to argc.

Return Value

A pointer to an array of LPWSTR values, similar to argv.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

The address returned by CommandLineToArgvW is the address of the first element in an array of LPWSTR values; the number of pointers in this array is indicated by pNumArgs. Each pointer to a null-terminated Unicode string represents an individual argument found on the command line.

CommandLineToArgvW allocates a block of contiguous memory for pointers to the argument strings, and for the argument strings themselves; the calling application must free the memory used by the argument list when it is no longer needed. To free the memory, use a single call to the LocalFree function.

For more information about the argv and argc argument convention, see Argument Definitions and Parsing C++ Command-Line Arguments.

The GetCommandLineW function can be used to get a command line string that is suitable for use as the lpCmdLine parameter.

This function accepts command lines that contain a program name; the program name can be enclosed in quotation marks or not.

CommandLineToArgvW has a special interpretation of backslash characters when they are followed by a quotation mark character ("), as follows:

  • 2n backslashes followed by a quotation mark produce n backslashes followed by a quotation mark.
  • (2n) + 1 backslashes followed by a quotation mark again produce n backslashes followed by a quotation mark.
  • n backslashes not followed by a quotation mark simply produce n backslashes.

Example

The following example demonstrates how to parse a Unicode command-line string. The code frees the memory for the argument list at exit.

#include <windows.h>
#include <stdio.h>
#include <shellapi.h>

int __cdecl main()
{
   LPWSTR *szArglist;
   int nArgs;
   int i;

   szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
   if( NULL == szArglist )
   {
      wprintf(L"CommandLineToArgvW failed\n");
      return 0;
   }
   else for( i=0; i<nArgs; i++) printf("%d: %ws\n", i, szArglist[i]);

// Free memory allocated for CommandLineToArgvW arguments.

   LocalFree(szArglist);

   return(1);
}

Function Information

Minimum DLL VersionShell32.dll version 6.0 or later
Custom ImplementationNo
HeaderShellapi.h
Import libraryShell32.lib
Minimum operating systems Windows NT Workstation 3.5, Windows NT Server 3.5, Windows 2000 Professional, Windows 2000 Server, Windows XP, Windows Server 2003
UnicodeImplemented as Unicode version.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Bug in the example code      Roland Illig   |   Edit   |   Show History
main() usually returns 0 on success and non-zero on failure. The example does the opposite.
No extra NULL element      Myria   |   Edit   |   Show History
Unlike main and wmain, CommandLineToArgvW does not have an extra element of argv[argc] == NULL. Trying to do this will result in reading past the end of the pointer list.
Tags What's this?: Add a tag
Flag as ContentBug
Bug with CommandLineToArgvW/GetCommandLineW combination      Goatspawn   |   Edit   |   Show History
When a program is launched directory with no arguments, GetCommandLineW() can return an unquoted path with spaces in it. In this case, CommandLineToArgvW() will not handle the string properly. Ran into this issue on Vista 64 with a 32-bit app, not sure how widespread it is.

The simplest way to test for this case seems to be to use GetModuleFileNameW() with 0 as the first parameter to get the exe path and then compare it to the command line.


Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker