1. TCHAR szPath[MAX_PATH];
  2. GetModuleFileName(NULL,szPath,MAX_PATH);
  3. HKEY newValue;
  4. HKEY resKey;
  5. subValue = "MyProgram";
  6. long key = RegQueryValueExA(resKey, subValue, NULL, NULL, NULL, NULL);
  7. if(key == ERROR_FILE_NOT_FOUND)
  8. {
  9. return false;
  10. }
  11. else
  12. {
  13. const DWORD cbData = sizeof(TCHAR) * (lstrlen(szPath) + 1);
  14. RegOpenKey(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&newValue);
  15. RegSetValueEx(newValue, subValue ,0,REG_SZ,(LPBYTE)szPath,cbData);
  16. RegCloseKey(newValue);
  17. }