Who doesn’t know how to copy and paste? Ctrl+C and Ctrl+V can achieve this. These two important operations are used by many people multiple times a day, but the process is often not understood; as long as it works, that’s enough. In a basic computer applications course, the teacher will certainly tell you:The copy and paste function utilizes the computer’s clipboard functionality. When a segment of text or other data is selected and the copy operation is executed, the computer temporarily stores this data in the clipboard. Later, when the paste operation is executed, the computer reads the data from the clipboard and inserts it into the desired location.
So how is the copy and paste function implemented?
The copy and paste function is a fundamental and powerful feature in computer operating systems, allowing users to quickly move or copy data between different locations or applications.The core principle involves the operating system’s clipboard service, data storage format recognition, and inter-process communication protocols.This process is primarily achieved through the clipboard, which is a temporary storage area provided by the operating system to hold the information that the user wants to copy or cut. Among these, data storage format recognition plays an important role, as it ensures that data can be correctly received and parsed when copying and pasting between different applications.
Data source:https://docs.pingcode.com/ask/ask-ask/295245.html
If you are interested, you can refer to the above address for more information.
Below, I will use C++ to restore the seemingly simple actions of “copy/paste” into a complete link involving the operating system, process memory, IPC, and kernel objects, to see how this process is implemented. For readability, I will write each step as “pseudo-code + comments”, which you can directly treat as a compilable header file level example.
//cpp
/******************************************************************
* 1. Common abstraction layer: Treat the clipboard as a “global kv store”, where the key is MIME/CF_* type
*******************************************************************/
class Clipboard {
public:
// Copy: Serialize local data into multiple formats and push to the system clipboard
virtual void copy (const std::map<std::string, std::vector<uint8_t>>& formats) = 0;
// Paste: Return data of the specified format from the system clipboard
virtual std::vector<uint8_t> paste(const std::string& mime) const = 0;
};
/********************************************************************
*2. Windows implementation: OpenClipboard / EmptyClipboard / SetClipboardData
*******************************************************************/
#ifdef _WIN32
#include <windows.h>
// Create clipboard
class Win32Clipboard final : public Clipboard {
public:
// Implementation of copy functionality using containers
void copy(const std::map<std::string, std::vector<uint8_t>>& formats) override {
if(!OpenClipboard(nullptr)) return;
EmptyClipboard(); // Clear the system clipboard object table
for(const auto& [fmtName, blob] : formats) {
UINT cf = formatNameToCF(fmtName); // Convert MIME to CF_* or RegisterClipboardFormat
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, blob.size()+1);
void* dst = GlobalLock(hMem);
memcpy(dst, blob.data(), blob.size());
GlobalUnlock(hMem);
SetClipboardData(cf, hMem); // Attach HGLOBAL handle to the clipboard
}
CloseClipboard();
}
// Paste
std::vector<uint8_t> paste(const std::string& mime) const override {
if(!OpenClipboard(nullptr)) return {};
UINT cf = formatNameToCF(mime);
HANDLE hMem = GetClipboardData(cf);
if(!hMem) { CloseClipboard(); return {}; }
const uint8_t* src = (uint8_t*)GlobalLock(hMem);
size_t sz = GlobalSize(hMem);
std::vector<uint8_t> result(src, src+sz);
GlobalUnlock(hMem);
CloseClipboard();
return result;
}
private:
static UINT formatNameToCF(const std::string& name) {
if(name == “text/plain”)
return CF_TEXT;
return RegisterClipboardFormatA(name.c_str());
}
};
#endif // _WIN32
/********************************************************************
* 3. User code: The essence of a “copy/paste” operation
********************************************************************/
int main() {
Win32Clipboard cb;
/* Copy: Provide “hello clipboard” in both text/plain and text/html */
std::map<std::string, std::vector<uint8_t>> data;
std::string txt = “hello clipboard”;
std::string html = “<b>hello clipboard</b>”;
data[“text/plain”] = {txt.begin(), txt.end()};
data[“text/html”] = {html.begin(), html.end()};
cb.copy(data);
/* Paste: Read text/plain */
auto blob = cb.paste(“text/plain”);
std::cout << std::string(blob.begin(), blob.end()) << std::endl;
}
In summary:
Copy = Serialize local process memory into multiple formats, then register a “global object” with the system kernel;
Paste = Query this object from the system, obtain the handle/file descriptor, and then deserialize back to local memory.