MCPcopy Create free account
hub / github.com/Open-GD/OpenGD / CopyToClipboard

Function CopyToClipboard

Source/NodeSerializer.cpp:127–187  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

125#include <string_view>
126
127static void CopyToClipboard(const std::string_view text) {
128#if (AX_TARGET_PLATFORM == AX_PLATFORM_WIN32)
129 // Open the clipboard
130 if (!OpenClipboard(nullptr)) {
131 // Handle error if clipboard cannot be opened
132 // You can add appropriate error handling here
133 return;
134 }
135
136 // Empty the clipboard
137 if (!EmptyClipboard()) {
138 // Handle error if clipboard cannot be emptied
139 // You can add appropriate error handling here
140 CloseClipboard();
141 return;
142 }
143
144 // Get the length of the text
145 const size_t textLength = text.length();
146
147 // Allocate global memory to hold the text
148 HGLOBAL hClipboardData = GlobalAlloc(GMEM_DDESHARE, (textLength + 1) * sizeof(char));
149 if (hClipboardData == nullptr) {
150 // Handle error if memory allocation fails
151 // You can add appropriate error handling here
152 CloseClipboard();
153 return;
154 }
155
156 // Lock the global memory handle and get a pointer to the memory
157 char* pClipboardData = static_cast<char*>(GlobalLock(hClipboardData));
158 if (pClipboardData == nullptr) {
159 // Handle error if memory locking fails
160 // You can add appropriate error handling here
161 GlobalFree(hClipboardData);
162 CloseClipboard();
163 return;
164 }
165
166 // Copy the text to the global memory
167 memcpy(pClipboardData, text.data(), textLength * sizeof(char));
168
169 // Null-terminate the text
170 pClipboardData[textLength] = '\0';
171
172 // Unlock the global memory
173 GlobalUnlock(hClipboardData);
174
175 // Set the clipboard data
176 if (!SetClipboardData(CF_TEXT, hClipboardData)) {
177 // Handle error if clipboard data cannot be set
178 // You can add appropriate error handling here
179 GlobalFree(hClipboardData);
180 CloseClipboard();
181 return;
182 }
183
184 // Close the clipboard

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected