为了实现开机自动启动,需要操作注册表信息。
1.添加注册表信息
添加注册表
#define KEY_AUTO_RUN _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run") //自动开机启动的注册表的位置
#define KEY_PROGRAM _T("your program name");
HKEY RegEntry;
int open = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KEY_AUTO_RUN, 0, KEY_SET_VALUE, &RegEntry);
if(ERROR_SUCCESS == open)
{
TCHAR fileName[MAX_PATH+1];
GetModuleFileName(GetModuleHandle(AfxGetApp()->m_pszAppName), fileName, MAX_PATH);
DWORD dwRet = RegSetValueEx(RegEntry, KEY_PROGRAM, 0, REG_SZ, (LPBYTE)fileName, strlen(fileName));
if(dwRet != ERROR_SUCCESS)
{
//打印注册表失败操作信息提示
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwRet,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
::MessageBox( this->m_hWnd, (LPCTSTR)lpMsgBuf, "注册表操作提示", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
#define KEY_PROGRAM _T("your program name");
HKEY RegEntry;
int open = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KEY_AUTO_RUN, 0, KEY_SET_VALUE, &RegEntry);
if(ERROR_SUCCESS == open)
{
TCHAR fileName[MAX_PATH+1];
GetModuleFileName(GetModuleHandle(AfxGetApp()->m_pszAppName), fileName, MAX_PATH);
DWORD dwRet = RegSetValueEx(RegEntry, KEY_PROGRAM, 0, REG_SZ, (LPBYTE)fileName, strlen(fileName));
if(dwRet != ERROR_SUCCESS)
{
//打印注册表失败操作信息提示
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwRet,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
::MessageBox( this->m_hWnd, (LPCTSTR)lpMsgBuf, "注册表操作提示", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
RegCloseKey(RegEntry);
}
}
2.删除开机自动启动注册表信息
删除开机启动注册表信息
#define KEY_AUTO_RUN _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run") //自动开机启动的注册表的位置
HKEY RegEntry;
int open = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KEY_AUTO_RUN, 0, KEY_SET_VALUE, &RegEntry);
if(ERROR_SUCCESS == open)
{
DWORD dwRet = RegDeleteValue(RegEntry, KEY_PROGRAM);
if(dwRet != ERROR_SUCCESS)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwRet,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
::MessageBox( this->m_hWnd, (LPCTSTR)lpMsgBuf, "注册表操作提示", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
RegCloseKey(RegEntry);
}
HKEY RegEntry;
int open = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KEY_AUTO_RUN, 0, KEY_SET_VALUE, &RegEntry);
if(ERROR_SUCCESS == open)
{
DWORD dwRet = RegDeleteValue(RegEntry, KEY_PROGRAM);
if(dwRet != ERROR_SUCCESS)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwRet,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
::MessageBox( this->m_hWnd, (LPCTSTR)lpMsgBuf, "注册表操作提示", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
RegCloseKey(RegEntry);
}