A C++ desktop application demonstrating inter-window communication using the Windows API. This application creates two chat windows that can send text messages to each other bidirectionally using custom Windows messages.
- Two independent chat windows (Window A and Window B)
- Bidirectional messaging: Both windows can send and receive messages
- Text input and output areas in each window
- Send button to transmit messages between windows
- Real-time message display in the receiving window
- Unicode support for international text
- Educational approach with separate window procedures
- Two different window classes with distinct message handlers
- Good for learning Windows API fundamentals
- Clear separation of concerns
- Production-ready with modern C++ features
- Uses
std::optional, smart pointers, and auto type deduction - Better error handling and memory management
- Singleton pattern for window management
- Simplified architecture with one shared window procedure
- Single window class handles both windows
- Less code duplication and easier maintenance
- Demonstrates alternative design approach
WNDCLASSEX wc = {};
wc.lpfnWndProc = WindowProc; // Message handler function
wc.lpszClassName = L"MyWindowClass";
RegisterClassEx(&wc);- Window Class: A template that defines window properties and behavior
- Window Procedure: A callback function that handles messages sent to windows
HWND hwnd = CreateWindowEx(
0, // Extended window style
L"MyWindowClass", // Window class name
L"Window Title", // Window title
WS_OVERLAPPEDWINDOW, // Window style
x, y, width, height, // Position and size
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional data
);- WM_CREATE: Sent when a window is first created
- WM_COMMAND: Handles button clicks and control notifications
- WM_DESTROY: Sent when a window is being destroyed
- Custom Messages: User-defined messages for specific communication needs
The application uses a custom message (WM_SEND_TEXT) to send text between windows:
#define WM_SEND_TEXT (WM_USER + 1)
SendMessage(targetWindow, WM_SEND_TEXT, 0, (LPARAM)textData);# Run the build script and choose your preferred version
build.batThe script will prompt you to choose:
- Original version - Educational with separate window procedures
- Modern C++17 version - Production-ready with advanced features
- Single procedure version - Simplified architecture
# Original version (separate window procedures)
g++ -std=c++17 -DUNICODE -D_UNICODE -o MessageApp.exe main.cpp -lgdi32 -luser32 -lkernel32
# Modern C++17 version (advanced features)
g++ -std=c++17 -DUNICODE -D_UNICODE -o MessageApp_cpp17.exe main_cpp17.cpp -lgdi32 -luser32 -lkernel32
# Single procedure version (simplified)
g++ -std=c++17 -DUNICODE -D_UNICODE -o MessageApp_single.exe main_single_proc.cpp -lgdi32 -luser32 -lkernel32
# Or use the provided Makefile
make- Open
MessageApp.vcxprojin Visual Studio - Build the solution (Ctrl+Shift+B)
- Run the application (F5)
- Build and run any version using the build script or manual compilation
- Two chat windows will appear side by side
- Type a message in the text input area of either window
- Click "Send Message" to send it to the other window
- The message appears in the output area of the receiving window
- Both windows work identically - either can send to the other
main.cpp- Original version with separate window procedures (educational)main_cpp17.cpp- Modern C++17 version with advanced features (production)main_single_proc.cpp- Single procedure version with simplified architecturebuild.bat- Interactive build script with version selectionMakefile- Standard build systemMessageApp.vcxproj- Visual Studio project file
README.md- This file (main documentation)CPP17_FEATURES.md- C++17 features and modern C++ improvementsWINDOW_PROCEDURES.md- Comparison of single vs multiple window proceduresGUIDE.md- Detailed step-by-step Windows API tutorialPROJECT_SUMMARY.md- Complete project overview and learning outcomes
- HWND: Handle to a window (unique identifier)
- HINSTANCE: Handle to an application instance
- MSG: Structure containing message information
- WPARAM/LPARAM: Parameters passed with messages
The heart of any Windows application:
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg); // Keyboard input translation
DispatchMessage(&msg); // Send to window procedure
}- Dynamic allocation for message text:
new wchar_t[length] - Cleanup responsibility: receiving window deletes the memory
- Proper resource management to prevent memory leaks
You can enhance this basic application by:
- Adding message history/logging
- Implementing different message types
- Adding file transfer capabilities
- Creating a chat-like interface with timestamps
- Adding network communication between different computers
- Windows operating system
- C++ compiler (MinGW-w64, Visual Studio, or similar)
- Basic understanding of C++ and Windows API concepts
Build Errors:
- Ensure you have the Windows SDK installed
- Check that all required libraries are linked (
user32.lib,gdi32.lib) - Verify C++11 or later standard is enabled
Runtime Issues:
- Make sure both windows are created successfully
- Check that window handles are valid before sending messages
- Ensure proper message handling in window procedures