修改命令行参数 [英] Modify Command Line Arguments

查看:117
本文介绍了修改命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Win32 C ++应用程序,我需要在该应用程序中修改命令行参数。具体来说,我想以这样的方式编辑命令行参数: GetCommandLineW()返回我的新参数。

I have a Win32 C++ application and I need to modify the command line arguments in the application. Specifically, I want to edit the command line arguments in such a way that GetCommandLineW() returns my new arguments.

信不信由你,这行得通(因为我们有一个指向字符数组的非常量指针):

Believe it or not, this works (since we have a non-const pointer to the character array):

LPTSTR args = GetCommandLineW();
LPTSTR new_args = L"foo --bar=baz";
wmemcpy(args, new_args, lstrlenW(new_args));

// ...
LPTSTR args2 = GetGommentLineW(); // <- equals "foo --bar=baz"

但是我不知道如何Windows为 GetCommandLineW()提供的 LPTSTR 分配了长内存。

But I don't know how long much memory Windows allocates for the LPTSTR provided by GetCommandLineW().

还有另一种方法吗?还是有人知道命令行参数是否分配了可预测的内存?

Is there another way to do this? Or does anyone know if there is a predictable amount of memory allocated for the command line arguments?

推荐答案

GetCommandLineW()不分配任何内存。它只是返回一个指针,该指针指向操作系统在进程的 PEB 结构在创建过程时出现。该缓冲区存在于整个过程的生命周期中。

GetCommandLineW() does not allocate any memory. It simply returns a pointer to a buffer that is allocated by the OS in the process's PEB structure when the process is created. That buffer exists for the lifetime of the process.

这篇关于修改命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆