O
O
Olegofr3n772020-09-01 16:43:45
C++ / C#
Olegofr3n77, 2020-09-01 16:43:45

How to include a program in C++ startup?

I add the program to autoload code:

void AutoRun()
{
    char arr[MAX_PATH] = { };
    GetModuleFileName(NULL, (LPWSTR)arr, MAX_PATH);

    HKEY hKey;

    if (RegCreateKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL) == ERROR_SUCCESS)
    {
        if (RegSetValueEx(hKey, L"My program", NULL, REG_SZ, (LPBYTE)arr, sizeof(arr)) == ERROR_SUCCESS)
        {
            RegCloseKey(hKey);
        }
        return;
    }
}

Everything is fine, the program is prescribed, but the problem is that the "Status" is disabled, how to make it so that it becomes "on"?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2020-09-01
@res2001

The last parameter in RegSetValueEx for REG_SZ must be the length of the string + 1 to the terminating null character (strlen(arr)+1) ( https://docs.microsoft.com/en-us/windows/win32/api... )
Do not use MAX_PATH is an ancient, obsolete option. Read until enlightened: https://docs.microsoft.com/en-us/windows/win32/fil...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question