-1

I have the following in my CPP code which adds the current program into startup. I'm trying to modify the code to add a different program to startup, say I want to add a key so that "C:\mytime.exe" runs on startup. Could you please help me modify the code?

TCHAR szPath[MAX_PATH];
DWORD pathLen = 0;

pathLen = GetModuleFileName(NULL, szPath, MAX_PATH);
if (pathLen == 0)
{
    return -1;
}
HKEY newValue;
if (RegOpenKey(HKEY_CURRENT_USER,
    TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
    &newValue) != ERROR_SUCCESS)
{
    return -1;
}
DWORD pathLenInBytes = pathLen * sizeof(*szPath);
if (RegSetValueEx(newValue,
    TEXT("My Program"),
    0,
    REG_SZ,
    (LPBYTE)szPath,
    pathLenInBytes) != ERROR_SUCCESS)
{
    RegCloseKey(newValue);
    return -1;
}
RegCloseKey(newValue);
return TRUE;
4

1 回答 1

1

只需替换这段代码:

pathLen = GetModuleFileName(NULL, szPath, MAX_PATH);
if (pathLen == 0)
{
    return -1;
}

有了这个:

/* of course, use your own executable - make sure to not overflow the buffer! */
_tcscpy(szPath, _T("C:\\stackoverflow.exe"));
pathLen = _tcslen(szPath);
于 2013-02-21T18:33:07.580 回答