实现开机启动的方法有很多,但是在Win7中,修改注册表等操作需要高权限,这样的话,感觉就很麻烦。
我实现开机启动所用的方法是:为程序自己创建一个快捷方式,然后把快捷方式放入到开始菜单---程序---启动中,这个不需要用管理员权限。
可以学习的地方:
1、创建快捷方式
2、获取启动的文件夹地址(一个api搞定)
贴代码:
#include <ShlObj.h>
#include <Windows.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
TCHAR StartPath[MAX_PATH+1];
TCHAR FilePath[MAX_PATH+1];
ZeroMemory(FilePath,MAX_PATH+1);
GetModuleFileName(NULL,FilePath,MAX_PATH);
SHGetSpecialFolderPath(NULL,StartPath,7,FALSE);
printf("%S\n",StartPath);
::CoInitialize(NULL);
IShellLink *psl;
IPersistFile* ppf;
HRESULT hr=CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void**)&psl);
if(SUCCEEDED(hr))
{
psl->SetPath(FilePath);
hr=psl->QueryInterface(IID_IPersistFile,(void**)&ppf);
if(SUCCEEDED(hr))
{
lstrcat(StartPath,TEXT("\\AutoStart.lnk"));
ppf->Save(StartPath,STGM_READWRITE);
psl->Release();
ppf->Release();
}
}
cout<<"已加入开始---程序---启动,\n按任意键删除开机启动"<<endl;
system("pause");
DeleteFile(StartPath);
system("pause");
return 0;
}