Usuwanie z rejestru autostartu

0

Witam, próbuję dodać program do rejestru autostartu, testowałem kilka kodów ale nic się w rejestrze nie zmienia. Teraz mam taki kod:

#include <tchar.h>
#include <stdio.h>
#include <windows.h>
#include <iostream>
 
int main(void)
{
    TCHAR szPath[MAX_PATH];
    DWORD pathLen = 0;
    // GetModuleFileName returns the number of characters
    // written to the array.
    pathLen = GetModuleFileName(NULL, szPath, MAX_PATH);
    if (pathLen == 0)
    {
		_tprintf(TEXT("Unable to get module file name; last error = %lu\n"), GetLastError());
		std::cin.get();
		return -1;
    }
    HKEY newValue;
    if (RegOpenKey(HKEY_LOCAL_MACHINE,
		TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
		&newValue) != ERROR_SUCCESS)
		{
		_tprintf(TEXT("Unable to open registry key; last error = %lu\n"), GetLastError());
		std::cin.get();
		return -1;
    }
    // Need to pass the length of the path string in bytes,
    // which may not equal the number of characters due to
    // character set.
    DWORD pathLenInBytes = pathLen * sizeof(*szPath);
    if (RegSetValueEx(newValue,
		TEXT("Registry"),
		0,
		REG_SZ,
		(LPBYTE)szPath,
		pathLenInBytes) != ERROR_SUCCESS)
		{
		RegCloseKey(newValue);
		_tprintf(TEXT("Unable to set registry value; last error = %lu\n"), GetLastError());
		std::cin.get();
		return -1;
    }
    RegCloseKey(newValue);
    return 0;
}
 

Wypisuje: Unable to set registry value; last error = 0, ale nic do rejestru nie dodaje. Co robię źle?

0

Odpal w trybie administratora.

2

_tprintf(TEXT("Unable to set registry value; last error = %lu\n"), GetLastError());

nie rób tak. zrób tak:

 if (RegSetValueEx(...) != ERROR_SUCCESS)
{
    DWORD err = GetLastError();
    RegCloseKey(newValue);
    _tprintf(TEXT("Unable to set registry value; last error = %u\n"), err);
}

GetLastError nie powinno być parametrem wywołania funkcji, bo możesz w ten sposób stracić wartość i pokaże ci zero.
powinno być też bezpośrednio po funkcji która się nie udaje, bo teraz to wyświetlasz że RegCloseKey udaje się bez błędu.

0

Ok wielkie dzięki, wszystko działa :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1