Esempio n. 1
0
        /// <summary>
        /// Parses a command line string into an argv array according to the Windows rules
        /// </summary>
        /// <param name="lpCommandLine">The command line to parse</param>
        /// <returns>An array of arguments interpreted by Windows</returns>
        public static string[] ParseCommandLine(string lpCommandLine)
        {
            int numArgs;

            using (SafeMemoryBuffer buf = NativeMethods.CommandLineToArgvW(lpCommandLine, out numArgs))
            {
                if (buf.IsInvalid)
                {
                    throw new Win32Exception("Error parsing command line");
                }
                IntPtr[] strptrs = new IntPtr[numArgs];
                Marshal.Copy(buf.DangerousGetHandle(), strptrs, 0, numArgs);
                return(strptrs.Select(s => Marshal.PtrToStringUni(s)).ToArray());
            }
        }