简体   繁体   English

将变量设置为cmd.exe

[英]set variable to cmd.exe

Greeting earthmen, 问候地球人,

Here is my question: 这是我的问题:

How can I create a program which sets variable to current session of cmd.exe eg 我如何创建一个程序,将变量设置为cmd.exe的当前会话,例如

c:\> set myvar
Environment variable myvar not defined
c:\>myexe.exe
c:>set myvar
myvar=myvalue

The only similar topic that I've found is this - 我发现的唯一类似主题是-

How can I change Windows shell (cmd.exe) environment variables from C++? 如何从C ++更改Windows Shell(cmd.exe)环境变量?

But I didn't get a single word from this: 但是我没有从中得到一个字:

There is a way... Just inject your code into parent process and call SetEnvironmentVariableA inside cmd's process memory. 有一种方法...只需将代码注入父进程,然后在cmd的进程内存中调用SetEnvironmentVariableA。 After injecting just free the allocated memory. 注入后仅释放分配的内存。

While C/C++ is not my "native" language I've felt myself completely lost when I've searched google with "c++ inject code" and etc... Is there an article where I can get more info about this. 虽然C / C ++不是我的“母语”语言,但是当我用“ c ++注入代码”等搜索google时,我感到自己完全迷失了。是否有文章可以让我了解更多信息。

BTW now I'm using one a little bit stupid workaround.As the setting a variable to 顺便说一句,我现在正在使用一些愚蠢的解决方法。将变量设置为

HKEY_CURRENT_USER\\Environment HKEY_CURRENT_USER \\环境

and

HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment HKEY_LOCAL_MACHINE \\ SYSTEM \\ CurrentControlSet \\ Control \\ Session Manager \\ Environment

is comparatively easy I'm just recording similar variable to the registry: 比较容易,我只是在注册表中记录类似的变量:

load.temp.vars=set myvar1=myval1&set myvar2=myval2& ....

and then just call %load.temp.vars% and it will be executed as a command: 然后只需调用%load.temp.vars%,它将作为命令执行:

c:/>%load.temp.vars%
c:/>set myvar1
myvar1=myval1

It works fine bu it's not good enough for me :) 它对我来说还不够好:)

Live long and prosper, \\\\//_ 健康长寿·繁荣昌盛, \\\\//_

Check out this article: Three Ways to Inject Your Code into Another Process . 查看本文: 将代码注入另一个进程的三种方法

Also you probably will need a handle to your parent process (to determine the target process whose environment to change). 另外,您可能需要父进程的句柄(以确定要更改其环境的目标进程)。 A way to get it is described here . 这里描述一种获取方法。

Just keep in mind that injection may not work, depending on user account privileges, and also that some especially paranoid antivirus solutions may frown upon it. 请记住,根据用户帐户权限的不同,注入可能不起作用,并且某些特别偏执的防病毒解决方案可能对此并不满意。

There are very easy way to do this without any tricks . 有很简单的方法可以做到这一点, 而无需任何技巧

You should write a small program myexe.exe which produce a simple output (console output) like following: 您应该编写一个小程序myexe.exe ,它会产生一个简单的输出(控制台输出),如下所示:

SET myvar=Some value

then you start your program with the following steps: 然后按以下步骤启动程序:

myexe.exe >%TEMP%\t.cmd
call %TEMP%\t.cmd
del %TEMP%\t.cmd

Now in the current cmd.exe which started myexe.exe exist the environment variable myvar and it has the value Some value . 现在,在启动myexe.exe的当前cmd.exe ,存在环境变量myvar ,其值是Some value Is it not exactly what you want? 这不是您想要的吗?

The way is extrem simple and it works on all versions of Windows (and not only Windows). 这种方法非常简单,并且可以在所有版本的Windows(不仅限于Windows)上运行。 So it is my recommendation for you. 因此,这是我对您的推荐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM