window下系统环境变量修改

通过注册表方式修改系统换变量

#include <windows.h>  
#include <iostream>  
#include <thread>

int main() {
	
	HKEY hKey;
	const char* en_name = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
	const char* key_name = "third";
	std::string third_value = "D:\\third_dll";
	const char* path_name = "Path";
	DWORD type = REG_SZ;
	DWORD size = strlen(third_value.c_str()) + 1;


	// 打开注册表键  
	if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, en_name, 0, KEY_WRITE, &hKey) != ERROR_SUCCESS) {
		std::cout << "Failed to open registry key." << std::endl;
		return 1;
	}

	//新建系统环境变量key_name  
	if (RegSetValueExA(hKey, key_name, 0, type, (LPBYTE)third_value.c_str(), size) != ERROR_SUCCESS) {
		std::cout << "Failed to set registry value." << std::endl;
		return 1;
	}

	std::string path_value = getenv("Path");

	path_value = path_value + ";%" + key_name + "%";
	DWORD path_size = strlen(path_value.c_str()) + 1;

	// 修改Path环境变量
	if (RegSetValueExA(hKey, path_name, 0, type, (LPBYTE)path_value.c_str(), path_size) != ERROR_SUCCESS) {
		std::cout << "Failed to read path value." << std::endl;
		return 1;
	}
	std::cout << "Path: " << path_value << std::endl;
	// 关闭注册表键  
	if (RegCloseKey(hKey) != ERROR_SUCCESS) {
		std::cout << "Failed to close registry key." << std::endl;
		return 1;
	}
	std::this_thread::sleep_for(std::chrono::seconds(1)); //等1s差不多才生效
	std::cout << "Environment variable set successfully." << std::endl;

	return 0;
}

对exe进行环境变量修改

#include <windows.h>  
#include <iostream>  
#define BUFSIZE 4096
int main(int argc, char *argv[])
{
	LPTSTR path_value;
    LPTSTR newValue[MAX_PATH];

    path_value = (LPTSTR)malloc(BUFSIZE * sizeof(TCHAR));

    //std::shared_ptr<LPTSTR> path_value = std::make_shared<LPWSTR>(BUFSIZE);

	const char* key_name = "third";
    std::string third_value = "D:\\third_dll";
	std::string proc_name = "project.exe";//当下应用程序,如果通过别的程序调是不可以的

	DWORD dwRet = GetEnvironmentVariableW(L"Path", path_value, BUFSIZE);
    
    std::string new_path = ConvertLPCTSTRToString(path_value) + std::string("%") + key_name + std::string("%");
    
	SetEnvironmentVariable(L"Path", ConvertToLPWSTR(new_path));
	dwRet = GetEnvironmentVariable(L"Path", path_value, BUFSIZE);
	
}

通过批处理方式修改

std::string batch_value = std::string("D:\\") + "setting.bat";
std::string value_1 = std::string("D:\\") + "third_dll";
value_1 = "setx Third_Dll \"" + value_1 + "\" -m";
std::string value_2 = "setx Path \"" + std::string("%%Third_Dll%%;%Path%") + "\" /m";
batchFile.open(batch_value); // 创建一个名为"mybatchfile.bat"的批处理文件  
if (batchFile.is_open()) {
	batchFile << "@echo on" << std::endl; // 写入批处理命令  
	batchFile << value_1 << std::endl;
	batchFile << value_2 << std::endl;
	batchFile << "pause" << std::endl;
	batchFile.close(); // 关闭文件  
}
else {
	std::cout << "Failed to create batch file." << std::endl;
}
system(batch_value.c_str());

cmd黑框隐藏、显示

#include <windows.h>  
  
int main() {  
    // 隐藏cmd窗口  
    FreeConsole();  
  
    // 这里是你的代码  
  
    // 显示cmd窗口  
    AllocConsole();  
  
    // 等待用户输入,以便看到cmd窗口  
    getchar();  
  
    return 0;  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳光开朗男孩

你的鼓励是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值