System Communication API Reference

This section details the Windows API functions and concepts related to inter-process communication (IPC) and system-level communication mechanisms.

Introduction to System Communication

Effective communication between different processes is crucial for building robust and efficient Windows applications. This documentation covers various methods, including:

  • Pipes: Unidirectional or bidirectional data streams for communication between related processes.
  • Message Queues: Asynchronous message passing between processes, often used for decoupled systems.
  • Shared Memory: Allowing multiple processes to access the same region of memory for high-performance data sharing.
  • Sockets: For network communication, both local and remote, using TCP/IP or other protocols.
  • Windows Messages: The fundamental mechanism for intra-process and inter-process communication in Windows GUI applications.
  • COM (Component Object Model): A powerful object-oriented framework for inter-process communication and software interoperability.

Core Communication APIs

CreatePipe

BOOL CreatePipe( _Out_ PHANDLE hReadPipe, _Out_ PHANDLE hWritePipe, _In_opt_ LPSECURITY_ATTRIBUTES lpPipeAttributes, _In_opt_ DWORD nSize );

Description:

Creates an anonymous pipe, a unidirectional data stream. It returns handles to the read and write ends of the pipe. Useful for redirecting standard input/output of child processes.

Parameters:

  • hReadPipe: Pointer to a HANDLE variable that receives the read end of the pipe.
  • hWritePipe: Pointer to a HANDLE variable that receives the write end of the pipe.
  • lpPipeAttributes: A pointer to a SECURITY_ATTRIBUTES structure that specifies the security attributes of the new pipe.
  • nSize: The buffer size for the pipe, in bytes.

Return Value:

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

CreateFileMapping

HANDLE CreateFileMapping( _In_ HANDLE hFile, _In_opt_ LPSECURITY_ATTRIBUTES lpFileMappingAttributes, _In_ DWORD flProtect, _In_ DWORD dwMaximumSizeHigh, _In_ DWORD dwMaximumSizeLow, _In_opt_ LPCTSTR lpName );

Description:

Creates or opens a named or unnamed file mapping object. This is the first step in creating shared memory.

Parameters:

  • hFile: A handle to the file that will be used to initialize the size of the memory-mapped file.
  • lpFileMappingAttributes: Security attributes.
  • flProtect: Memory protection for the pages of the file mapping object.
  • dwMaximumSizeHigh, dwMaximumSizeLow: High and low parts of the maximum size of the memory-mapped file.
  • lpName: The name of the mapping object.

Return Value:

If the function succeeds, the return value is a handle to the newly created file mapping object. If the function fails, the return value is NULL.

MapViewOfFile

LPVOID MapViewOfFile( _In_ HANDLE hFileMappingObject, _In_ DWORD dwDesiredAccess, _In_ DWORD dwFileOffsetHigh, _In_ DWORD dwFileOffsetLow, _In_ SIZE_T dwNumberOfBytesToMap );

Description:

Maps a view of a file mapping into the address space of the calling process. This function is used to access shared memory.

Parameters:

  • hFileMappingObject: Handle to a file mapping object.
  • dwDesiredAccess: Access to the mapped views.
  • dwFileOffsetHigh, dwFileOffsetLow: Offset into the file mapping.
  • dwNumberOfBytesToMap: Number of bytes to map.

Return Value:

If the function succeeds, the return value is the starting address of the mapped view of the specified file. If the function fails, the return value is NULL.

PostMessage

BOOL PostMessage( _In_opt_ HWND hWnd, _In_ UINT Msg, _In_ WPARAM wParam, _In_ LPARAM lParam );

Description:

Places (posts) a message into the message queue of the specified thread or an array of threads. Unlike SendMessage, this function returns immediately and does not wait for the destination thread to process the message.

Parameters:

  • hWnd: Handle to the destination window.
  • Msg: The message to be posted.
  • wParam: Additional message-specific information.
  • lParam: Additional message-specific information.

Return Value:

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

CreateThread

HANDLE CreateThread( _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, _In_ SIZE_T dwStackSize, _In_ LPTHREAD_START_ROUTINE lpStartAddress, _In_opt_ LPVOID lpParameter, _In_ DWORD dwCreationFlags, _Out_opt_ LPDWORD lpThreadId );

Description:

Creates a new thread to execute within the virtual address space of the calling process. Threads are fundamental for concurrent operations and can be used to handle communication tasks asynchronously.

Parameters:

  • lpThreadAttributes: Security attributes.
  • dwStackSize: Initial size of the thread stack.
  • lpStartAddress: Pointer to the application-defined function.
  • lpParameter: Argument for the thread function.
  • dwCreationFlags: Control flags.
  • lpThreadId: Receives the thread identifier.

Return Value:

If the function succeeds, the return value is a handle to the new thread. If the function fails, the return value is NULL.