PostMessage Or SendMessage

博客介绍了PostMessage和SendMessage的差异。PostMessage将消息放入队列,不确定处理时间和是否处理,执行后马上返回;SendMessage需等到收到消息处理的返回码(DWord类型)才继续,必须等消息被处理后才返回。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    PostMessage只负责将消息放到消息队列中,不确定何时及是否处理
    SendMessage要等到受到消息处理的返回码(DWord类型)后才继续
    PostMessage执行后马上返回
    SendMessage必须等到消息被处理后才会返回。
### Non-Window Class SendMessage Alternative Methods In environments where traditional window classes are not available or applicable, sending messages requires alternative approaches. The standard `SendMessage` function is designed specifically for communicating with windows through their message queues; however, other mechanisms exist within the Windows API and C++ that facilitate inter-process communication (IPC) without relying on window handles. #### Using PostThreadMessage Function For scenarios involving threads rather than specific windows, one can utilize the `PostThreadMessage` function which posts a message to the thread’s message queue identified by its ID: ```cpp BOOL result = PostThreadMessage( DWORD idThread, // Thread identifier UINT msg, // Message to post WPARAM wParam, // First message parameter LPARAM lParam // Second message parameter ); if (!result) { // Handle error } ``` This approach allows messaging between different parts of an application running under separate threads but does not require these components to own windows[^1]. #### Named Pipes for IPC Another robust solution involves employing named pipes—a powerful feature provided by both Windows APIs and supported across multiple platforms via libraries like Boost.Interprocess. This method supports bidirectional data exchange over pipe connections established either locally or remotely: ```cpp #include <windows.h> #include <stdio.h> int main() { HANDLE hPipe; char buffer[1024]; DWORD dwRead; hPipe = CreateFile( TEXT("\\\\.\\pipe\\mynamedpipe"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hPipe != INVALID_HANDLE_VALUE) { while (ReadFile(hPipe, buffer, sizeof(buffer)-1, &dwRead, NULL)) { buffer[dwRead] = '\0'; printf("%s", buffer); } CloseHandle(hPipe); } return 0; } ``` Named pipes offer more flexibility compared to simple threading-based solutions since they enable communication beyond process boundaries[^3]. #### Memory-Mapped Files Memory-mapped files represent yet another effective way to share information among processes or even persist state during program execution. By mapping portions of file storage directly into memory space accessible from user-mode applications, this technique provides efficient read/write operations suitable for large datasets: ```cpp HANDLE hMapFile; LPCTSTR pBuf; hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // Use paging file NULL, // Default security PAGE_READWRITE, // Read/Write access 0, // Max size high DWORD BUF_SIZE, // Max size low DWORD szName); // Name of map file pBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE); // Write some content to shared memory region... UnmapViewOfFile(pBuf); CloseHandle(hMapFile); ``` Such implementations allow complex interactions without necessitating direct interaction with GUI elements or handling HWND parameters explicitly[^4].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值