Esempio n. 1
0
        public static string[] CommandLineToArgs(string commandLine)
        {
            if (string.IsNullOrEmpty(commandLine))
            {
                return(Array.Empty <string>());
            }

            var argv = Shell32.CommandLineToArgvW(commandLine, out int argc);

            if (argv == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            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);
            }
            finally
            {
                Marshal.FreeHGlobal(argv);
            }
        }
Esempio n. 2
0
        public static string[] CommandLineToArgvW(string cmdLine)
        {
            IntPtr argv = IntPtr.Zero;

            try
            {
                int numArgs = 0;

                argv = Shell32.CommandLineToArgvW(cmdLine, out numArgs);
                if (argv == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }
                var result = new string[numArgs];

                for (int i = 0; i < numArgs; i++)
                {
                    IntPtr currArg = Marshal.ReadIntPtr(argv, i * Marshal.SizeOf(typeof(IntPtr)));
                    result[i] = Marshal.PtrToStringUni(currArg);
                }

                return(result);
            }
            finally
            {
                IntPtr p = Kernel32.LocalFree(argv);
                // Otherwise LocalFree failed.
                // Assert.AreEqual(IntPtr.Zero, p);
            }
        }
Esempio n. 3
0
    public unsafe void CommandLineToArgvW_Test()
    {
        char **value = Shell32.CommandLineToArgvW("dotnet build /p:SomeValue=\"With A Space\"", out int length);

        Assert.Equal(3, length);
        Assert.Equal("dotnet", new string(value[0]));
        Assert.Equal("build", new string(value[1]));
        Assert.Equal("/p:SomeValue=With A Space", new string(value[2]));

        Kernel32.LocalFree(value);
    }
Esempio n. 4
0
        internal void Run(string[] args)
        {
            var kernel32CommandLine = Kernel32.GetCommandLine();
            var dotnetCommandLine   = Environment.CommandLine;
            var shell32ArgsPtr      = Shell32.CommandLineToArgvW(kernel32CommandLine, out var pNumArgs);
            var shell32Args         = IntPtrToStringArray(shell32ArgsPtr, pNumArgs);
            var dotnetMainArgs      = args;

            if (string.Equals(kernel32CommandLine.ToString(), dotnetCommandLine, StringComparison.Ordinal))
            {
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.WriteLine(LocalizableStrings.CommandLineHeader);
                Console.ResetColor();
                Console.WriteLine($"  {kernel32CommandLine}");
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.WriteLine(LocalizableStrings.CommandLineKernel32Header);
                Console.ResetColor();
                Console.WriteLine($"  {kernel32CommandLine}");
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.WriteLine(LocalizableStrings.CommandLineDotNetHeader);
                Console.ResetColor();
                Console.WriteLine($"  {dotnetCommandLine}");
            }

            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine(LocalizableStrings.CommandLineArgsHeader);
            Console.ResetColor();
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine($"[{i}] {args[i]}");
            }

            if (NoExit)
            {
                Console.WriteLine(LocalizableStrings.PressAnyKeyToExit);
                Console.ReadKey();
            }
        }