仅适用于同一台电脑的两个进程聊天,对于不同电脑之前的聊天需要依靠tcp/ip协议。
两个进程是通过发送WM_COPYDATA
消息来传输字节的。
代码如下:
Server.cpp
#include <Windows.h> #include <stdio.h> #include <iostream> #include <thread> using namespace std; DWORD dwWritten; DWORD num; DWORD dwRead; char strMessage[256]; HWND h_client; void td1(); COORD coord; SHORT i = 0; LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE); if (message == WM_DESTROY) { PostQuitMessage(0); } if (message == WM_COPYDATA) { COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam; if (pcds->dwData == 1) { CHAR* lpszString = (CHAR*)(pcds->lpData); WriteConsoleOutputCharacterA(hStdOut, lpszString, strlen(lpszString)-2, { 0,i++ }, &dwWritten); } if (i > 24) { system("cls"); i = 0; coord.X = 0; coord.Y = 25; SetConsoleCursorPosition(hStdOut, coord); printf("---------------------------------------------------------------------------------------------------------- "); } } return DefWindowProc(hwnd, message, wParam, lParam); }; HINSTANCE hinst; int main() { HWND hwnd; hinst = GetModuleHandle(NULL); // create a window class: WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hinst; wc.lpszClassName = L"win32"; // register class with operating system: RegisterClass(&wc); // create and show window: hwnd = CreateWindow(L"win32", L"Server", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL); std::thread t1(td1); if (hwnd == NULL) { return 0; } ShowWindow(hwnd, SW_SHOW); MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } void td1() { HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE); HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); while (1) { h_client = FindWindow(L"win32", L"Client"); if (h_client) { break; } } coord.X = 0; coord.Y = 25; SetConsoleCursorPosition(hStdOut, coord); printf("----------------------------------------------------------------------------------------------------------"); while (1) { coord.X = 0; coord.Y = 26; SetConsoleCursorPosition(hStdOut, coord); memset(strMessage, 0, 256); ReadFile(hStdIn, strMessage, 256, &dwRead, NULL); SetConsoleCursorPosition(hStdOut, coord); for (int i = strlen(strMessage); i > 0; i--) { putchar(32); } COPYDATASTRUCT cds; cds.dwData = 1; // can be anything cds.cbData = sizeof(CHAR) * strlen(strMessage); cds.lpData = strMessage; SendMessage(h_client, WM_COPYDATA, (WPARAM)h_client, (LPARAM)(LPVOID)&cds); } }
Client.cpp
#include <Windows.h> #include <stdio.h> #include <iostream> #include <thread> using namespace std; DWORD dwWritten; DWORD dwRead; char strMessage[256]; HWND h_server; SHORT i = 0; COORD coord; void td1(); LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE); if (message == WM_DESTROY) { PostQuitMessage(0); } if (message == WM_COPYDATA) { COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam; if (pcds->dwData == 1) { CHAR* lpszString = (CHAR*)(pcds->lpData); int len = pcds->cbData; WriteConsoleOutputCharacterA(hStdOut, lpszString, len - 2, { 0,i++ }, &dwWritten); } if (i > 24) { system("cls"); i = 0; coord.X = 0; coord.Y = 25; SetConsoleCursorPosition(hStdOut, coord); printf("---------------------------------------------------------------------------------------------------------- "); } } return DefWindowProc(hwnd, message, wParam, lParam); } HINSTANCE hinst; int main() { HWND hwnd; hinst = GetModuleHandle(NULL); // create a window class: WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hinst; wc.lpszClassName = L"win32"; // register class with operating system: RegisterClass(&wc); // create and show window: hwnd = CreateWindow(L"win32", L"Client", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL); std::thread t1(td1); if (hwnd == NULL) { return 0; } ShowWindow(hwnd, SW_SHOW); MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } void td1() { HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE); HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); while (1) { h_server = FindWindow(L"win32", L"Server"); if (h_server) { break; } } coord.X = 0; coord.Y = 25; SetConsoleCursorPosition(hStdOut, coord); printf("----------------------------------------------------------------------------------------------------------"); while (1) { coord.X = 0; coord.Y = 26; SetConsoleCursorPosition(hStdOut, coord); memset(strMessage, 0, 256); ReadFile(hStdIn, strMessage, 256, &dwRead, NULL); SetConsoleCursorPosition(hStdOut, coord); for (int i = strlen(strMessage); i > 0; i--) { putchar(32); } COPYDATASTRUCT cds; cds.dwData = 1; // can be anything cds.cbData = sizeof(CHAR) * (strlen(strMessage) + 1); cds.lpData = strMessage; SendMessage(h_server, WM_COPYDATA, (WPARAM)h_server, (LPARAM)(LPVOID)&cds); } }
实现效果: