Exemple #1
0
        public static IReadOnlyList <string> CommandLineToArgs(string commandLine)
        {
            if (string.IsNullOrEmpty(commandLine))
            {
                return(Array.Empty <string>());
            }

            var argv = Win32Native.CommandLineToArgv(commandLine, out var argc);

            if (argv == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            try
            {
                var args = new string[argc];
                for (var i = 0; i < args.Length; ++i)
                {
                    var p = Marshal.ReadIntPtr(argv, i * IntPtr.Size);
                    args[i] = Marshal.PtrToStringUni(p);
                }
                return(args.ToList().AsReadOnly());
            }
            finally
            {
                Marshal.FreeHGlobal(argv);
            }
        }