search
HomeJavajavaTutorialHow 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] = &#39;a&#39;+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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software