Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In addition to the <a href="https://stackoverflow.com/questions/298830/split-string-containing-command-line-parameters-into-string-in-c/298990#298990">good and pure managed solution</a> by <a href="https://stackoverflow.com/users/27423/earwicker">Earwicker</a>, it may be worth mentioning, for sake of completeness, that Windows also provides the <a href="http://msdn.microsoft.com/en-us/library/bb776391.aspx" rel="nofollow noreferrer"><code>CommandLineToArgvW</code></a> function for breaking up a string into an array of strings:</p> <blockquote> <pre><code>LPWSTR *CommandLineToArgvW( LPCWSTR lpCmdLine, int *pNumArgs); </code></pre> <p>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.</p> </blockquote> <p>An example of calling this API from C# and unpacking the resulting string array in managed code can be found at, “<a href="http://intellitect.com/converting-command-line-string-to-args-using-commandlinetoargvw-api/" rel="nofollow noreferrer">Converting Command Line String to Args[] using CommandLineToArgvW() API</a>.” Below is a slightly simpler version of the same code:</p> <pre><code>[DllImport("shell32.dll", SetLastError = true)] static extern IntPtr CommandLineToArgvW( [MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine, out int pNumArgs); public static string[] CommandLineToArgs(string commandLine) { int argc; var argv = CommandLineToArgvW(commandLine, out argc); if (argv == IntPtr.Zero) throw new System.ComponentModel.Win32Exception(); try { var args = new string[argc]; for (var i = 0; i &lt; args.Length; i++) { var p = Marshal.ReadIntPtr(argv, i * IntPtr.Size); args[i] = Marshal.PtrToStringUni(p); } return args; } finally { Marshal.FreeHGlobal(argv); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload