<code class="lang-">#include <windows.h> #include <tchar.h> #include <stdio.h> #include <userenv.h> #pragma comment(lib, "userenv.lib") int _tmain(int argc, TCHAR **argv) { // 设置(删除)用户级(系统级)环境变量,并通知explorer.exe读取注册表环境变量 TCHAR envstr[] = _T("My Environment String"); RegSetKeyValue(HKEY_CURRENT_USER, _T("Environment"), _T("myenv"), REG_SZ, envstr, lstrlen(envstr) * sizeof (TCHAR)); //RegDeleteKeyValue(HKEY_CURRENT_USER, _T("Environment"), _T("myenv")); RegSetKeyValue(HKEY_LOCAL_MACHINE, _T("System\\CurrentControlSet\\Control\\Session Manager\\Environment"), _T("mysysenv"), REG_SZ, envstr, lstrlen(envstr) * sizeof (TCHAR)); SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)_T("Environment")); // 根据NT系统的userenv.dll生成Unicode环境块 HANDLE htoken = NULL; OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &htoken); LPVOID envblock = NULL; CreateEnvironmentBlock(&envblock, htoken, FALSE); CloseHandle(htoken); // 以这个环境块运行cmd /k set以查看结果 TCHAR cmdline[1024] = _T("cmd /k set"); STARTUPINFO si = { sizeof si }; PROCESS_INFORMATION pi = { 0 }; CreateProcess(NULL, cmdline, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT, envblock, NULL, &si, &pi); CloseHandle(pi.hThread); // 等待程序结束 WaitForSingleObject(pi.hProcess, INFINITE); CloseHandle(pi.hProcess); // 销毁环境块 DestroyEnvironmentBlock(envblock); return 0; }</userenv.h></stdio.h></tchar.h></windows.h></code>
[修改于 7年4个月前 - 2017/09/07 21:40:25]
时段 | 个数 |
---|---|
{{f.startingTime}}点 - {{f.endTime}}点 | {{f.fileCount}} |