#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 );
}
RegCloseKey(RegEntry);
}