Home >Java >javaTutorial >How to understand the clipboard of C++ MFC inter-process communication
The content of this article is about how to understand the clipboard of C MFC inter-process communication. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
#The Windows Clipboard is a relatively simple inter-process communication mechanism, and its overhead is relatively small. Its implementation principle is very simple. In fact, it is a memory area maintained by the operating system.
This memory area does not belong to any separate process, but every process can access this memory area. When a process will The data is placed in the memory area, and another process can retrieve the data from the memory area to achieve communication. The implementation process consists of two parts, one is the shared memory operation and the other is the clipboard operation.
1. Clipboard operation
(1) HWND GetClipboardOwner();
Function: Get the handle pointing to the current owner of the clipboard
If this function is executed successfully, it returns the window that owns the clipboard handle. Otherwise, NULL is returned.
(2) BOOL OpenClipboard(HWND hWndNewOwner);
The first parameter hWndNewOwner points to a window handle associated with it, which means that this window opens the clipboard. If this parameter is set to NULL, Then open the clipboard with the current task or process. If the clipboard is opened successfully, this function returns a non-zero value. If another program has already opened the clipboard, the current program cannot open the clipboard, so the clipboard will fail to be opened, and the function returns a 0 value. In fact, this is easy to understand. If you think about it, the clipboard only has such a memory area. Your process A wants to write data into it, and your process B wants to write data into it. So don't mess up. The way to solve this mess is, If my process A is writing data to the clipboard (which can be understood as process A opening the clipboard), then process B cannot write data to the clipboard. Since process B cannot write data to the clipboard, Then I will let process B fail to open the clipboard. So if a program has opened the clipboard, then other applications will not be able to modify the clipboard until the program that opened the clipboard calls the CloseClipboard function, And only after calling the EmptyClipboard function, open the clipboard The current window can have a clipboard
(3) BOOL CloseClipboard(void);
If a process opens the clipboard, before the process does not call the CloseClipboard function to close the clipboard handle, Other processes cannot open the clipboard, so we should close the clipboard every time we use it. Note that closing the clipboard here does not mean that the program currently opening the clipboard loses ownership of the clipboard. Only after another program calls the EmptyClipboard function will the current program lose ownership of the clipboard. The program that calls the EmptyClipboard function can have a clipboard.
(4) HANDLE SetClipboardData(UINT uFormat, HANDLE hMem);
SetClipboardData function is used to place data into the clipboard. This function places data into the clipboard in the specified clipboard format. The first parameter uFormat is used to specify the format of the data to be placed on the clipboard, such as CF_BITMAP, CF_TEXT, CF_DIB, etc. (for other formats, please refer to MSDN). The second parameter hMem is used to specify the handle of data with the specified format. This parameter can be NULL. If the parameter is NULL, it indicates that until a program requests the data in the clipboard, the program (that is, has the clipboard The ownership process) will copy the data to the clipboard, that is, provide data in the specified clipboard format. The above mentioned is the delayed submission technology. This delayed submission technology will be introduced in detail later.
(5) BOOL IsClipboardFormatAvailable( UINT format );
This function is used to determine whether the data format on the clipboard is the format specified by format.
(6) HANDLE GetClipboardData( UINT uFormat );
This function returns a handle to the clipboard object that exists in the clipboard in the specified format according to the format specified by uFormat.
2. Shared memory allocation
(1) HGLOBAL WINAPI GlobalAlloc( UINT uFlags, SIZE_T dwBytes );
The first parameter uFlags is used to specify the method of allocating memory. Its values are as shown in the following list. However, in the use of the clipboard, due to the need to achieve dynamic data exchange, GHND or GMEM_MOVEABLE must be used):
GHND A combination of GMEM_MOVEABLE and GMEM_ZEROINIT.
GMEM_FIXED Allocates a fixed memory and the return value is a pointer.
GMEM_MOVEABLE Allocate a piece of movable memory.
GMEM_ZEROINIT Initialize the memory content to 0
GPTR That is the combination of GMEM_FIXED and GMEM_ZEROINIT.
The second parameter dwBytes is used to specify the number of bytes allocated.
(2) HGLOBAL WINAPI GlobalReAlloc(HGLOBAL hMem, SIZE_T dwBytes, UINT uFlags);
This function is a reallocation function, that is, to expand the memory space on the original data object hMem.
The first parameter hMem represents the data object handle returned by the GlobalAlloc function.
The second parameter dwBytes specifies the size of the memory that needs to be reallocated.
The third parameter uFlags specifies the allocation method (refer to the GlobalAlloc function).
(3) SIZE_T WINAPI GlobalSize( HGLOBAL hMem );
This function is used to return the size of the memory block.
The first parameter hMem represents the data object handle returned by the GlobalAlloc function.
(4) LPVOID WINAPI GlobalLock( HGLOBAL hMem );
The function of this function is to lock the global memory object and then return the pointer to the first byte of the object's memory block.
The first parameter hMem represents the data object handle returned by the GlobalAlloc function.
(5) BOOL WINAPI GlobalUnlock( HGLOBAL hMem );
You can obtain access to this global memory through the above GlobalLock function.
Locking means that you are already using this memory Global memory, other programs can no longer use this global memory, and if you never unlock it, it is not a problem. Other programs will never be able to use this global memory, which is still called global memory. What are you doing? So this function is used to unlock the global memory object.
The first parameter hMem represents the data object handle returned by the GlobalAlloc function.
(6) HGLOBAL WINAPI GlobalFree( HGLOBAL hMem );
This function releases the global memory block.
The first parameter hMem represents the data object handle returned by the GlobalAlloc function.
The following is a sample code. Readers can also perform a certain test by performing Ctrl C (copy data to the clipboard) Ctrl V (copy data from the clipboard) on their own computers:
// Ctrl+C.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream> #include <process.h> #include <windows.h> using namespace std;int main() { HWND hWnd = GetClipboardOwner();//获取当前剪贴板所属的窗口句柄 DWORD Len = 32; HGLOBAL pClipData; pClipData = GlobalAlloc(GHND,Len+1);//分配共享内存 char* pData; pData = (char*)GlobalLock(pClipData);//内存控制句柄加锁,其他进程不能再访问 for(int i = 0;i < Len;i++) { pData[i] = 'a'+i; //在全局内存中赋值 } GlobalUnlock(pClipData);//内存控制句柄解锁,其他进程可以访问 if(!OpenClipboard(hWnd))//打开剪贴板 { cout<<"OPen fail!"<<endl; return 0; } EmptyClipboard();//清空剪贴板,这一步才真正拥有剪贴板 SetClipboardData(CF_TEXT,pClipData);//将共享内存里的数据放入剪贴板 CloseClipboard();//关闭剪贴板 cout<<"剪贴完成"<<endl; return 0; }
//Ctrl+V.cpp#include "stdafx.h"#include <iostream> #include <process.h> #include <windows.h> using namespace std;int main() { HWND hWnd = GetClipboardOwner();//获取当前剪贴板所属的窗口句柄 if(!OpenClipboard(hWnd))//打开剪贴板 { cout<<"OPen fail!"<<endl; return 0; } if(IsClipboardFormatAvailable(CF_TEXT)) { HANDLE hCilpData = GetClipboardData(CF_TEXT); DWORD Len = GlobalSize(hCilpData); char* pData; pData = (char*)GlobalLock(hCilpData);//内存控制句柄加锁,其他进程不能再访问 cout<<"剪贴板内容是:"<<pData<<endl; GlobalUnlock(hCilpData);//内存控制句柄解锁,其他进程可以访问 } EmptyClipboard();//清空剪贴板,这一步才真正拥有剪贴板 CloseClipboard();//关闭剪贴板 cin.get(); return 0; }
The above is the detailed content of How to understand the clipboard of C++ MFC inter-process communication. For more information, please follow other related articles on the PHP Chinese website!