// find out the file name from given path CString FindFileName(LPSTR path) { CString filename(path); int nIndex = filename.ReverseFind('\\'); filename = filename.Mid(nIndex + 1); return filename; }// Query if the application will automatically start up when starting windows BOOL IsAppAutoRunEnabled() { HKEY hKey; if(RegCreateKey(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\" "Windows\\CurrentVersion\\Run"), &hKey) == ERROR_SUCCESS) { BOOL bEnabled;
CString FindFileName(LPSTR path)
{
CString filename(path);
int nIndex = filename.ReverseFind('\\');
filename = filename.Mid(nIndex + 1);
return filename;
}// Query if the application will automatically start up when starting windows
BOOL IsAppAutoRunEnabled()
{
HKEY hKey;
if(RegCreateKey(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\"
"Windows\\CurrentVersion\\Run"), &hKey) == ERROR_SUCCESS)
{
BOOL bEnabled;
LPTSTR lpCurrentPath = NULL;
lpCurrentPath = new CHAR [MAX_PATH];
GetModuleFileName(NULL,lpCurrentPath,MAX_PATH);
CString filename = FindFileName(lpCurrentPath);
bEnabled = (ERROR_SUCCESS == RegQueryValueEx(hKey,
filename.GetBuffer(0), 0, NULL, NULL, NULL));
RegCloseKey(hKey);
delete [] lpCurrentPath;
return bEnabled;
} return FALSE;
}// Modify the registry to let the application run at window starting up.
BOOL EnableAppAutoRun()
{
HKEY hKey; if(RegCreateKey(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\"
"Windows\\CurrentVersion\\Run"), &hKey) == ERROR_SUCCESS)
{
LPTSTR lpCurrentPath = NULL;
lpCurrentPath = new CHAR [MAX_PATH];
GetModuleFileName(NULL,lpCurrentPath,MAX_PATH); CString filename = FindFileName(lpCurrentPath);
RegSetValueEx(hKey, filename.GetBuffer(0), 0, REG_SZ,
(BYTE *)lpCurrentPath, strlen(lpCurrentPath));
RegCloseKey(hKey);
delete [] lpCurrentPath;
return TRUE;
}
return FALSE;
}这样使用:
BOOL bRetVal = IsAppAutoRunEnabled();
if (!bRetVal)
EnableAppAutoRun();