我写了一端代码,目的是将自己的程序做成开机启动,程序运行没有报错,但是就是没有吧程序的路径写入注册表,不知道为什么,程序路径通过我测试,获取的是正确的。我的代码如下:
//开机启动代码
long result;
HKEY hkey;
HMODULE hmodule;
hmodule=GetModuleHandle(NULL);
if(hmodule==NULL)
{
MessageBox("获取模块句柄失败");
return FALSE;
}
char path[60];
if(GetModuleFileName(hmodule,path,60)==0)
{
MessageBox("获取文件路径失败");
return FALSE;
}
    /*CString strPath;
strPath.Format("%s",path);
MessageBox(strPath);*/
result=RegCreateKey(HKEY_LOCAL_MACHINE,"\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",&hkey);
if(ERROR_SUCCESS==result)
{
MessageBox("创建键值失败");
return FALSE;
}
result=RegSetValueEx(hkey,"Mail",0,REG_SZ,(LPBYTE)"asdf",strlen("asdf")+1);
if(result=ERROR_SUCCESS)
{
MessageBox("写入键值失败");
return FALSE;
} RegCloseKey(hkey);

解决方案 »

  1.   

    result=RegSetValueEx(hkey,"Mail",0,REG_SZ,(LPBYTE)"asdf",strlen("asdf")); 
    if(result!=ERROR_SUCCESS) 

        // result 指出了失败原因,可以用FormatMessage 查看
        MessageBox("写入键值失败"); 
        return FALSE; 

      

  2.   

    RegCreateKeyEx(HKEY_LOCAL_MACHINE,
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
    0L,
    NULL,
    REG_OPTION_NON_VOLATILE,
    KEY_ALL_ACCESS,
    NULL,
    &hkey,
    &dwoption);
    RegSetValueEx(hkey,
          "Mail",
           NULL,
           REG_SZ,
           (const BYTE*)(LPCTSTR)strPath,
           strPath.GetLength()*sizeof(TCHAR));
      

  3.   


    if(result = ERROR_SUCCESS) 

       MessageBox("写入键值失败"); 
       return FALSE; 
    兄弟:看清你自己的代码,这样子能报错吗?if(result != ERROR_SUCCESS)
      

  4.   

    RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",&hkey); 
    RegSetValueEx(hkey,"Mail",0,REG_SZ,(unsigned char*)"asdf",strlen("asdf")+1); 
      

  5.   

    用fhg0451  的代码,报错,创建失败!
      

  6.   

    if(ERROR_SUCCESS==result) 

    MessageBox("创建键值失败"); 
    return FALSE; 

    晕,你前面还有段,逻辑错了,应该为“!=”。//下面这句也要改,把最前面的\\去掉:
    result=RegCreateKey 
    (HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",&hkey); 
      

  7.   

    void CZhaojunDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    long result; 
    HKEY hkey; 
    HMODULE hmodule; 
    hmodule=GetModuleHandle(NULL); 
    if(hmodule==NULL) 

    MessageBox("获取模块句柄失败"); 
    //return FALSE; 

    char path[60]; 
    if(GetModuleFileName(hmodule,path,60)==0) 

    MessageBox("获取文件路径失败"); 
    //return FALSE; 

        /*CString strPath; 
    strPath.Format("%s",path); 
    MessageBox(strPath);*/ 
    result=RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",&hkey);//是"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",不是,"\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"
    int n=GetLastError();
    CString str;
    str.Format("%d",n);
    MessageBox(str);
    if(result!=ERROR_SUCCESS) 

    MessageBox("创建键值失败"); 
    //return FALSE; 

    result=RegSetValueEx(hkey,"Mail",0,REG_SZ,(LPBYTE)"asdf",strlen("asdf")+1); 
    if(result!=ERROR_SUCCESS) 

    MessageBox("写入键值失败"); 
    //return FALSE; 

    RegCloseKey(hkey);
    }
      

  8.   


    void CZhaojunDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    long result; 
    HKEY hkey; 
    HMODULE hmodule; 
    hmodule=GetModuleHandle(NULL); 
    if(hmodule==NULL) 

    MessageBox("获取模块句柄失败"); 
    //return FALSE; 

    char path[60]; 
    if(GetModuleFileName(hmodule,path,60)==0) 

    MessageBox("获取文件路径失败"); 
    //return FALSE; 

        /*CString strPath; 
    strPath.Format("%s",path); 
    MessageBox(strPath);*/ 
    result=RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",&hkey);//是"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",不是,"\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"
    int n=GetLastError();
    CString str;
    str.Format("%d",n);
    MessageBox(str);
    if(result!=ERROR_SUCCESS) 

    MessageBox("创建键值失败"); 
    //return FALSE; 

    result=RegSetValueEx(hkey,"Mail",0,REG_SZ,(LPBYTE)"asdf",strlen("asdf")+1); 
    if(result!=ERROR_SUCCESS) 

    MessageBox("写入键值失败"); 
    //return FALSE; 

    RegCloseKey(hkey);
    }