dlldepend/cmd_opr.cpp
2024-03-08 11:35:52 +08:00

50 lines
1.2 KiB
C++

#include "cmd_opr.h"
#include <windows.h>
#include <stdio.h>
#include <iostream>
std::string CmdOpr::exec_cmd(const std::string& cmd)
{
std::string result{};
FILE* pipe = _popen(cmd.c_str(), "r");
if (!pipe) {
return result;
}
char buffer[128]{};
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != nullptr) {
result += buffer;
}
}
_pclose(pipe);
return result;
}
bool add_environment_path(const std::string& path)
{
// 获取当前的 PATH 变量需要动态调整缓冲区大小
DWORD bufferSize = GetEnvironmentVariable("PATH", nullptr, 0);
if (bufferSize == 0) {
std::cerr << "Failed to get PATH variable size!" << std::endl;
return false;
}
char* currentPath = new char[bufferSize];
GetEnvironmentVariable("PATH", currentPath, bufferSize);
// 构造新的 PATH 变量值(在原有路径后面添加新路径)
std::string newPath = std::string(currentPath) + ";" + path;
// 设置修改后的 PATH 变量到环境变量中
if (!SetEnvironmentVariable("PATH", newPath.c_str())) {
std::cerr << "Failed to set PATH variable!" << std::endl;
}
delete[] currentPath;
return true;
}