【问题标题】:Batch variables to C variables [closed]批量变量到 C 变量 [关闭]
【发布时间】:2022-01-14 14:35:58
【问题描述】:

这里的一般问题,想知道我是否能够从批处理中获取变量并将它们转换为实际的 C 变量,例如:

%ComputerName%

然后将其放入 C 中的 var 中

char = computername thing

想知道有没有可能

【问题讨论】:

  • 检查getenv()函数
  • yourexe.exe %computerName% ...并且,在你的程序中使用argcargv
  • 我去看看

标签: c batch-file variables


【解决方案1】:

“批处理变量”是指环境变量,它们在命令会话和批处理文件运行的生命周期之外继续存在。

在 Windows 上,您可以使用 GetEnvironmentVariable API 函数:

wchar_t computerName[100];
GetEnvironmentVariableW(L"ComputerName", computerName, 100);
// Now: computerName contains the %ComputerName% variable

在 POSIX(以及 Windows)上,您可以使用 getenv,但是,由于它返回一个指向内存的只读指针,因此如果您想在自己的程序中对其进行变异,则必须先将其复制到其他地方:

char *computerName = getenv("ComputerName");

【讨论】:

  • 感谢伙计,这正是我所需要的。 :)
猜你喜欢
  • 2022-01-21
  • 2018-02-11
  • 1970-01-01
  • 2022-06-10
  • 2022-07-06
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多