问题是:在什么地方写修改注册表得代码比较好另:只要修改一次就可以了,怎样保证程序第二次运行时不再修改注册表

解决方案 »

  1.   

    放在 local_machine下的run子键下面。创建的时候先判断一下,如果不存在就新建,否则就什么也不做。
      

  2.   

    1。我以前是在CWinApp::InitInstance()中创建窗口(框架)之前,修改注册表的。
       地方应该不重要。
       写在"Software\\Microsoft\\Windows\\CurrentVersion\\Run"中。
    2。在第一次运行程序时,在注册表中建立一个键,如标志为Used;
       在CWinApp::InitInstance()中读取上述键,若为Used,跳过1
      

  3.   

    BOOL Register()
    {
    //Define Varible
    HKEY  hKEY;
    char  CurrentPath[MAX_PATH];
    char  SysPath[MAX_PATH];
    long  ret;
    LPSTR FileNewName;
    LPSTR FileCurrentName;
    DWORD type=REG_SZ;
    DWORD size=MAX_PATH;
    LPCTSTR Rgspath="Software\\Microsoft\\Windows\\CurrentVersion\\Run" ; //Get System Path
    GetSystemDirectory(SysPath,size);
    GetModuleFileName(NULL,CurrentPath,size);

    //Copy File
    FileCurrentName = CurrentPath;
    FileNewName = lstrcat(SysPath,"\\XXXX.exe");
    ret = CopyFile(FileCurrentName,FileNewName,TRUE);
    if (!ret)
    {
    return TRUE;
    } //Open key
    ret=RegOpenKeyEx(HKEY_LOCAL_MACHINE,Rgspath,0,KEY_WRITE, &hKEY);
    if(ret!=ERROR_SUCCESS)

    RegCloseKey(hKEY);
    return FALSE;
    } //Set Key
    ret=RegSetValueEx(hKEY,"shit",NULL,type,(const unsigned char*)FileNewName,size);
    if(ret!=ERROR_SUCCESS)

    RegCloseKey(hKEY);
    return FALSE;
    }
    RegCloseKey(hKEY); return TRUE;
    }