跳到正文

剪贴板

剪贴板 y09zo5i4

原文
剪贴板信息

剪贴板信息

收录
更新

#include <bits/stdc++.h> #include <winsock2.h> #include <Windows.h> #include <shellapi.h> using namespace std; #define BUF_SIZE 32768 #define TIMEOUT 1919 #define PORT 1145

#define kd(vk) (GetAsyncKeyState(vk) & 0x8000 ? 1 : 0) #define KEY_DOWN kd

#define perr(b, str) if(b) { cout << str << endl; Sleep(10000); exit(GetLastError()); } string pname = ”\\.\PIPE\GAME”;

void send(HANDLE hWPipe, string s) { DWORD dwReturn; WriteFile(hWPipe, s.data(), s.length(), &dwReturn, NULL); cout << “Send message "" << s << "" to child process.” << endl; } string rece(HANDLE hRPipe) { string s; char szBuffer[BUF_SIZE] = { 0 }; DWORD dwReturn; if (ReadFile(hRPipe, szBuffer, BUF_SIZE, &dwReturn, NULL)) { szBuffer[dwReturn] = ‘\0’; cout << “Receive message "" << szBuffer << "" from child process.” << endl; return (s = szBuffer); } return ""; }

#include “socket.cpp”

string ip;

HANDLE hWPipe, hRPipe; SocketTCP s; string serv; int n; void Process(); int main(int argc, char *argv[]) { if (argc == 2) { pname += argv[1]; } else { pname += to_string(time(NULL)); }

C++
WSADATA wsd;
WSAStartup(MAKEWORD(2, 2), &wsd);
vector<string> vip = GetLocalIP();
WSACleanup();
cout << "IP address:" << endl;
for(int i = 0; i < (int)vip.size(); i++)
{
	cout << i + 1 << ":\t" << vip[i] << endl;
}
if(vip.size() == 1)
{
	ip = vip[0];
}
else
{
	cout << "Choose an IP address to game: ";
	int x;
	cin >> x;
	while(x < 1 || x > (int)vip.size())
	{
		cout << "Error input number must bigger than 0 and smaller than " << vip.size() + 1 << endl;
		cout << "Reinput IP address number: ";
		cin >> x;
	}
	ip = vip[x - 1];
}
do
{
	cout << "Input the last number of server IP address: ";
	cin >> serv;
	s.cSetTarget(ip.substr(0, ip.find_last_of('.') + 1) + serv, PORT);
}while(s.cConnect());

cout << "PIPE NAME:" << pname << "(R/W)" << endl;
cout << "create r: ";
hRPipe = CreateNamedPipe((pname + "R").data(), PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, BUF_SIZE, BUF_SIZE, 0, NULL);
perr(hRPipe == INVALID_HANDLE_VALUE, "Create Read Pipe Error");
cout << hRPipe << endl;

cout << "create w: ";
hWPipe = CreateNamedPipe((pname + "W").data(), PIPE_ACCESS_OUTBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, BUF_SIZE, BUF_SIZE, 0, NULL);
perr(hWPipe == INVALID_HANDLE_VALUE, "Create Write Pipe Error");
cout << hWPipe << endl;

cout << "execute child timeout 1" << '\r';
Sleep(1000);
cout << "execute child program right now" << endl;

ShellExecuteA(NULL, "open", "gchild.exe", pname.data(), NULL, SW_SHOWDEFAULT);

perr(!ConnectNamedPipe(hRPipe, NULL) && GetLastError() != ERROR_PIPE_CONNECTED, "Connect R");
perr(!ConnectNamedPipe(hWPipe, NULL) && GetLastError() != ERROR_PIPE_CONNECTED, "Connect W");

SetConsoleTitle("Logger");

//////////////////////////////////

Process();

DisconnectNamedPipe(hRPipe);
DisconnectNamedPipe(hWPipe);
CloseHandle(hRPipe);
CloseHandle(hWPipe);

system("pause");
system("pause");

} queue<string> mq;

DWORD WINAPI Listen(LPVOID lpvIgnore) { while(1) { string s = rece(hRPipe); if(s != "") mq.push(s); } } void Process() { s.aSend(“ONLINE”); string receive; receive = s.aRecv(); sscanf(receive.data(), “%d”, &n); cout << n << endl; SetConsoleTitleA((“Game logger - n” + to_string(n)).data()); do { s.aSend(“FIND”); }while((receive = s.aRecv()) == “NOPE”); cout << “Find result: ” << receive << endl; SetConsoleTitleA((“Game logger - n” + to_string(n) + ” ” + receive).data()); // s.aSetRecvTime(1); int peer, player; sscanf(receive.data(), “%d%d”, &peer, &player); send(hWPipe, to_string(player));

代码
CreateThread(NULL, 0, Listen, NULL, 0, NULL);
while(1)
{
	s.BlockTarg(0);
	receive = s.aRecv();
	s.BlockTarg(1);
	if(receive.substr(0, 4) == "PEER") // Peer messages
	{
		string ms = receive.substr(5);
		if(ms == "LEAVE")
		{

// send(hWPipe, “peer_leave”); // if(rece(hRPipe) == “refind”) // { // do // { // s.aSend(“REFIND”); // refind // }while((receive = s.aRecv()) == “NOPE”); // cout << “Find result: ” << receive << endl; // SetConsoleTitleA((“Game logger - n” + to_string(n) + ” ” + receive).data()); // continue; // } // else { return; } } if(ms.substr(0, 3) == “KEY”) { send(hWPipe, ms); } } // DWORD dwst = PIPE_NOWAIT; // SetNamedPipeHandleState(hRPipe, &dwst, NULL, NULL); if(mq.size()) { receive = mq.front(); mq.pop(); if(receive == “exit”) { shutdown(s.aGetSocket(), SD_BOTH); shutdown(s.aGetTargetSocket(), SD_BOTH); return; } else s.aSend(receive); } // dwst = PIPE_WAIT; // SetNamedPipeHandleState(hRPipe, &dwst, NULL, NULL); } }