ILoveYesterday
2015-07-14 00:43
采纳率: 75%
浏览 6.1k

RegOpenKeyEx调用失败返回5 ,管理员运行不管用 win7VS2013 请问怎么回事?

代码
LONG lRegRet;
HLOCAL hLocal;
HKEY hKeyUser;
PTSTR pszEnvironmentName = NULL;
PTSTR pszEnvironmentValue = NULL;

pszEnvironmentName = (PTSTR)malloc(MAX_PATH * sizeof(TCHAR));
GetDlgItemText(hDlg, IDD_EDIT_NAME, pszEnvironmentName, MAX_PATH);

pszEnvironmentValue = (PTSTR)malloc(1024 * sizeof(TCHAR) * 3);
GetDlgItemText(hDlg, IDD_EDIT_VALUE, pszEnvironmentValue,
1024 * sizeof(TCHAR) * 3);

//修改用户环境变量
lRegRet = RegOpenKeyEx(HKEY_CURRENT_USER, USER_ENVREG, KEY_ALL_ACCESS, 0, &hKeyUser);
if (lRegRet != ERROR_SUCCESS)
{
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, lRegRet,
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), (PTSTR)&hLocal, 0, NULL);

MessageBox(hDlg, (PCTSTR)LocalLock(hLocal), TEXT("错误"), MB_OK);
break;
}
RegSetValueEx(hKeyUser, pszEnvironmentName, 0, REG_SZ,
(const unsigned char *)pszEnvironmentValue,
wcslen(pszEnvironmentValue) + sizeof(TCHAR));
RegCloseKey(hKeyUser);
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)TEXT("Environment"));

//释放分配的内存
free(pszEnvironmentValue);
free(pszEnvironmentName);

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

相关推荐 更多相似问题