最近无聊开始看mfc程序的编写。。做了个查询星座运势的东西。。内容摘自女巫店。。要用到启动项注册。。记录一下。。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
写入到注册表启动项
1
BOOL SetAutoRun(CString filePath)//开机自动运行
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
HKEY hRegKey;
4
BOOL bResult;
5![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
CString regStr = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
7![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
if(RegOpenKey(HKEY_LOCAL_MACHINE , regStr , &hRegKey) != ERROR_SUCCESS)
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
bResult = FALSE;
11
}
12
else
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
_splitpath(filePath.GetBuffer(0) , NULL , NULL , regStr.GetBufferSetLength(MAX_PATH + 1) , NULL);
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
filePath.ReleaseBuffer();
17
regStr.ReleaseBuffer();
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
if(::RegSetValueEx(hRegKey , regStr , 0 , REG_SZ , (CONST BYTE *)filePath.GetBuffer(0) , filePath.GetLength()) != ERROR_SUCCESS)
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
bResult = FALSE;
22
}
23
else
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
bResult = TRUE;
26
}
27
28
filePath.ReleaseBuffer();
29
}
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
return bResult;
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
}
34![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
#pragma endregion 写入到注册表启动项其中的filePath可以通过GetModuleFileName取得。。
1![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
TCHAR lpFileName[MAX_PATH];
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
GetModuleFileName(NULL , lpFileName , MAX_PATH);