BOOL   SetAutoRun(CString   strPath)//开机自动运行   
{   
    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   
    {   
// strPath.GetBuffer(0);
_wsplitpath(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),   
 78 )   !=   ERROR_SUCCESS)   
                  bResult=FALSE;
            else
                  bResult=TRUE;
            strPath.ReleaseBuffer();

    }   
    return   bResult;   
}   ::RegSetValueEx(   hRegKey,    str,   
 0,   
  REG_SZ,   
  (CONST   BYTE   *)strPath.GetBuffer(0),   
 78 )   
最后一个参数到底什么样的,我的strPath是d:\nproject\autorun1\Debug\autorun1.exe 一共39个字节,但是我这里打39的话他插进注册表的值只是d:\nproject\autorun1,如果我设置成78即39的2倍,他就可以把完整的地址插进注册表里。 我觉得很奇怪,所以问一下。

解决方案 »

  1.   

    Specifies the size of the information pointed to by the lpData parameter, in bytes. 
    If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character or characters. 
      

  2.   

    估计楼主是unicode版本的,
    改成这样吧,
    (strPath.GetLength()+1)*sizeof(TCHAR)
      

  3.   

    “一共39个字节”
    楼主,确切的说是39个字符,如果是Ansi版的话,是39个字节,
    如果是Unicode版的话,是78个字节。
      

  4.   

    是unicode编码的问题,一个字符等于两个字节.
    CString 中GetBuffer返回的就是一个TCHAR*.
    这是个宽字符的字符串,最后一个参数是上一个参数的大小(MSDN: Size, in bytes, of the information pointed to by the lpData parameter)
    所以最后一个参数要乘以2,就是为什么不是39而是78了.