在Windows系统上以C++打印出当前活动用户的环境变量
2017-08-06 23:49
645 查看
在Windows系统上以C++打印出当前活动用户的环境变量,代码如下(QT环境):
[cpp] view plain copy
void getEnvironmentVariables()
{
DWORD sessionId = WTSGetActiveConsoleSessionId();
qInfo() << "Session ID = " << sessionId;
HANDLE token;
if (!WTSQueryUserToken(sessionId, &token))
{
qCritical() << "Failed to get the user token of session " << sessionId;
}
wchar_t* pEnv = NULL;
if (CreateEnvironmentBlock((void**)&pEnv, token, TRUE))
{
while (*pEnv) {
// printf("%ls\n", pEnv);
qInfo() << QString::fromWCharArray(pEnv);
pEnv += wcslen(pEnv) + 1;
}
}
}
以上是打印当前活动用户(active user)的。
如果仅仅是是打印当前进程所处环境的环境变量,见Visual Studio的代码如下:
[cpp] view plain copy
#include <Windows.h>
#include <UserEnv.h>
#include <assert.h>
#include <stdio.h>
#pragma comment(lib, "userenv.lib")
int main()
{
HANDLE hToken = NULL;
BOOL ok = OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hToken);
assert(ok);
wchar_t* penv = L"";
ok = CreateEnvironmentBlock((void**)&penv, hToken, TRUE);
assert(ok);
while (*penv) {
printf("%ls\n", penv);
penv += wcslen(penv) + 1;
}
return 0;
}
http://blog.csdn.net/nirendao/article/details/52040232
[cpp] view plain copy
void getEnvironmentVariables()
{
DWORD sessionId = WTSGetActiveConsoleSessionId();
qInfo() << "Session ID = " << sessionId;
HANDLE token;
if (!WTSQueryUserToken(sessionId, &token))
{
qCritical() << "Failed to get the user token of session " << sessionId;
}
wchar_t* pEnv = NULL;
if (CreateEnvironmentBlock((void**)&pEnv, token, TRUE))
{
while (*pEnv) {
// printf("%ls\n", pEnv);
qInfo() << QString::fromWCharArray(pEnv);
pEnv += wcslen(pEnv) + 1;
}
}
}
以上是打印当前活动用户(active user)的。
如果仅仅是是打印当前进程所处环境的环境变量,见Visual Studio的代码如下:
[cpp] view plain copy
#include <Windows.h>
#include <UserEnv.h>
#include <assert.h>
#include <stdio.h>
#pragma comment(lib, "userenv.lib")
int main()
{
HANDLE hToken = NULL;
BOOL ok = OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hToken);
assert(ok);
wchar_t* penv = L"";
ok = CreateEnvironmentBlock((void**)&penv, hToken, TRUE);
assert(ok);
while (*penv) {
printf("%ls\n", penv);
penv += wcslen(penv) + 1;
}
return 0;
}
http://blog.csdn.net/nirendao/article/details/52040232
相关文章推荐
- 在Windows系统上以C++打印出当前活动用户的环境变量
- 以Windows系统服务得到活动用户的用户名、UserProfile与环境变量
- 从Windows系统服务获取活动用户的注册表信息(当前活动用户的sessionId. 当前活动用户的 hUserToken)
- Windows命令行获取当前bat文件所在目录,添加永久系统环境变量的方法
- windows下批处理更新当前目录到系统环境变量中
- Windows 10 系统设置系统环境变量和用户环境变量
- Windows命令行获取当前bat文件所在目录,添加永久系统环境变量的方法
- Windows命令行获取当前bat文件所在目录,添加永久系统环境变量的方法
- Windows命令行获取当前bat文件所在目录,添加永久系统环境变量的方法
- Windows系统 环境变量
- Windows 系统环境变量
- Linux及Windows系统配置JDK环境变量
- c++打印环境变量
- WINDOWS 7 系统环境下配置JDK环境变量
- Linux命令之env:显示当前用户的环境变量
- vs2008如何新建自己工程的环境变量(局部)和 Windows系统(全局). .
- 在Windows系统下设置Python的环境变量,以及安装BeautifulSoup
- ,如%USERPROFILE% 指当前用户的配置文件目录 系统路径%ProgramFiles% %windir% %windows% %Temp% 等的解释
- Bash玩转脚本5之Windows上修改环境变量的脚本系统
- Windows 10 系统中关于 JDK1.9 下载以及环境变量配置