【问题标题】:Replacing process arguments for a C# application替换 C# 应用程序的进程参数
【发布时间】:2011-10-03 22:00:45
【问题描述】:

有没有办法像 here 描述的本机程序那样替换托管进程的参数值?

这是我编写的一个程序,试图做同样的事情,但它不像其他情况那样工作:

        static void Main(string[] args)
        {
            if (args == null) 
            { 
                return; 
            }

            foreach (String arg in args)
            {
                Console.WriteLine("Before: " + arg); 

                unsafe
                {
                    fixed (char* ptr = arg)
                    {
                        for (int i = 0; i < arg.Length; i++)
                        {
                            *(ptr + i) = 'x';
                        }
                    }
                }

                Console.WriteLine("After: " + arg); 
            }

            Console.WriteLine("Environment.GetCommandLineArgs(): ");

            foreach (String arg in Environment.GetCommandLineArgs())
            {
                Console.WriteLine(arg);
            }
        }

结果是:

Before: asdf
After: xxxx
Before: lkjasdf
After: xxxxxxx
Before: ;lkajsdf
After: xxxxxxxx
Environment.GetCommandLineArgs():
ConsoleApplication2.exe
asdf
lkjasdf
;lkajsdf

提前谢谢你。

【问题讨论】:

  • 你想达到什么目的?找不到你?
  • 我会非常非常犹豫是否要这样做。您正在改变应该是不可变字符串的内容,并且您不知道您最初是否只有一个副本传递给您。也许如果我们知道为什么这是可取的?
  • 我正在查看,当敏感信息通过命令行参数传递时,是否有一种方法可以隐藏它们,以免被 Process Explorer 或任何其他可以查看命令的工具查看进程的行参数。
  • @Duat:你可以生成一个分离的子进程,通过命名管道传递参数,然后让父进程退出。
  • @Duat:您可以使用AnonymousPipeServerStream 文档中给出的方法将参数写入客户端(可能使用 BinaryWriter)。如果您无法尝试这种方法,请告诉我。

标签: c# .net arguments


【解决方案1】:

Environment.GetCommandLineArgs() 可能会在 Windows 上使用本机 GetCommandLine()。但是,返回值将被编组为 .Net 字符串,并且不会影响 GetCommandLine() 实际返回的本机指针。

您应该假设这是只读,并且您不能保证写入指针不会导致错误。我确定您可以使用 GetCommandLine()IntPtr 的 P/Invoke 定义来覆盖命令行,但这是未定义的行为,并且不能保证在未来有效(这些假设是 Raymond经常在他的文章中指出)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    相关资源
    最近更新 更多