Skip to content

pietrolc/win-msg-communication-1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Windows Message Communication App

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.

Features

  • 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

Available Versions

1. Original Version (main.cpp)

  • Educational approach with separate window procedures
  • Two different window classes with distinct message handlers
  • Good for learning Windows API fundamentals
  • Clear separation of concerns

2. Modern C++17 Version (main_cpp17.cpp)

  • 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

3. Single Procedure Version (main_single_proc.cpp)

  • Simplified architecture with one shared window procedure
  • Single window class handles both windows
  • Less code duplication and easier maintenance
  • Demonstrates alternative design approach

Key Concepts Demonstrated

1. Window Class Registration

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

2. Window Creation with CreateWindowEx

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
);

3. Message Handling

  • 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

4. Inter-Window Communication

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);

Building the Application

Option 1: Using the Interactive Build Script (Recommended)

# Run the build script and choose your preferred version
build.bat

The script will prompt you to choose:

  1. Original version - Educational with separate window procedures
  2. Modern C++17 version - Production-ready with advanced features
  3. Single procedure version - Simplified architecture

Option 2: Manual Compilation

# 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

Option 3: Using Visual Studio

  1. Open MessageApp.vcxproj in Visual Studio
  2. Build the solution (Ctrl+Shift+B)
  3. Run the application (F5)

How to Use

  1. Build and run any version using the build script or manual compilation
  2. Two chat windows will appear side by side
  3. Type a message in the text input area of either window
  4. Click "Send Message" to send it to the other window
  5. The message appears in the output area of the receiving window
  6. Both windows work identically - either can send to the other

Project Architecture

File Structure

  • 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 architecture
  • build.bat - Interactive build script with version selection
  • Makefile - Standard build system
  • MessageApp.vcxproj - Visual Studio project file

Documentation Files

  • README.md - This file (main documentation)
  • CPP17_FEATURES.md - C++17 features and modern C++ improvements
  • WINDOW_PROCEDURES.md - Comparison of single vs multiple window procedures
  • GUIDE.md - Detailed step-by-step Windows API tutorial
  • PROJECT_SUMMARY.md - Complete project overview and learning outcomes

Learning Points

Windows API Fundamentals

  • HWND: Handle to a window (unique identifier)
  • HINSTANCE: Handle to an application instance
  • MSG: Structure containing message information
  • WPARAM/LPARAM: Parameters passed with messages

Message Loop

The heart of any Windows application:

while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);  // Keyboard input translation
    DispatchMessage(&msg);   // Send to window procedure
}

Memory Management

  • Dynamic allocation for message text: new wchar_t[length]
  • Cleanup responsibility: receiving window deletes the memory
  • Proper resource management to prevent memory leaks

Extending the Application

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

Requirements

  • Windows operating system
  • C++ compiler (MinGW-w64, Visual Studio, or similar)
  • Basic understanding of C++ and Windows API concepts

Troubleshooting

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

About

Example of Windows Message Communication between 2 windows

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors