VC实现程序的自动运行

来源:互联网 发布:弹道软件汉化版 编辑:程序博客网 时间:2024/04/30 08:28

 很容易实现程序的开机自动运行,在注册表中写入相关信息即可.
在HKEY_LOCAL_MACHINE分支下的Software/Microsoft/Windows/CurrentVersion/Run下写入
字符串键值.

HKEY hKEY;
char CurrentPath[MAX_PATH];
char SysPath[MAX_PATH];
long ret;
LPSTR FileNewName;
LPSTR FileCurrentName;
DWORD type=REG_SZ;
DWORD size=MAX_PATH;
LPCSTR Rgspath="Software//Microsoft//Windows//CurrentVersion//Run";
GetSystemDirectory(SysPath,size);//攻取系统目录
GetModuleFileName(NULL,CurrentPath,size);//攻取程序路径
FileCurrentName=CurrentPath;
FileNewName=istrcat(SysPath,"//Surveillant.exe");//加系统目录的全路径名
ret=CopyFile(FileCurrentName,FileNewName,TRUE);//复制程序到系统目录
if(!ret)
 return;
//打开注册表
ret=RegOpenKeyEx(HKEY_LOCAL_MACHINE,Rgspath,0,KEY_WRITE,&hKEY);
if(ret!=ERROR_SUCCESS)
{
 RegCloseKey(hKEY);
 return;
}
//写入注册表
ret=RegSetValueEx(hKEY,"Surveillant",NULL,type,FileNewName,size);
if(ret!=ERROR_SUCCESS)
{
 RegCloseKey(hKEY);
 return;
}
//关闭注册表
RegCloseKey(hKEY);

原创粉丝点击