环境如题,网上搜索资料基本一致,代码如下:
BOOL CGetTimeDlg::SetAutoRun(CString strPath)//开机自动运行,strPath为exe路径
{
CString str;
HKEY hRegKey;
BOOL bResult;
str=_T("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
if(RegOpenKey(HKEY_LOCAL_MACHINE, str, &hRegKey) != ERROR_SUCCESS) 
bResult=FALSE;
else
{
_splitpath(strPath.GetBuffer(0),NULL,NULL,str.GetBufferSetLength(MAX_PATH+1),NULL);
strPath.ReleaseBuffer();
str.ReleaseBuffer();
if(::RegSetValueEx( hRegKey,str,0,REG_SZ,(CONST BYTE *)strPath.GetBuffer(0),strPath.GetLength() )!=ERROR_SUCCESS)
bResult=FALSE;
else
bResult=TRUE;
strPath.ReleaseBuffer();
} return bResult;
}RegOpenKey(HKEY_LOCAL_MACHINE, str, &hRegKey)返回为0,表示成功,而
(::RegSetValueEx( hRegKey,str,0,REG_SZ,(CONST BYTE *)strPath.GetBuffer(0),strPath.GetLength() )返回为5,郁闷一下午,没找到究竟,希望高手解答.刚注册,没多少分,希望大虾们别介意

解决方案 »

  1.   

    MSDN上如是说
    RegSetValueEx(HKEY hKey,.....);
    hKey
        A handle to an open registry key. The key must have been opened with the KEY_SET_VALUE access right.
      

  2.   

    strPath.ReleaseBuffer();之后你再RegSetValueEx?
      

  3.   

    5=Access Denied开机的时候访问UAC保护的注册表会被拒绝。
      

  4.   

    5:没有权限!WIN7和VISTA一样,受限于UAC。
      

  5.   

    知道问题所在了,F5调试时,由于win7在该情况下没有权限,所以RegSetValueEx始终返回值为5.但是程序本身并没有问题,在debug文件夹以管理员身份运行该exe即可。还发现一个问题,如果编译环境为Unicode,则
    (CONST BYTE *)strPath.GetBuffer(0),strPath.GetLength()无法得到正确的exe路径长度,导致exe路径无法得到,需要
        TCHAR tCharPath[MAX_PATH];
        wcscpy_s(tCharPath, MAX_PATH, strPath);
        if (::RegSetValueEx (hRegKey, _T("LedPlayerManage"), 0, REG_SZ, (LPBYTE)    tCharPath, (DWORD) (_tcslen(tCharPath)*sizeof(TCHAR)+1))!=ERROR_SUCCESS)
    利用TCHAR才能得到正确exe路径
      

  6.   

    遇到同样问题,建议用RegSetValue
      

  7.   

    遇到同样问题,建议用RegOpenKey
      

  8.   

    win7下怎么样才可以越过UAC读写注册表