1 | /* $Id: clipboard-win.cpp 79178 2019-06-17 10:52:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Shared Clipboard: Windows-specific functions for clipboard handling.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <iprt/alloc.h>
|
---|
19 | #include <iprt/assert.h>
|
---|
20 | #include <iprt/errcore.h>
|
---|
21 | #include <iprt/ldr.h>
|
---|
22 | #include <iprt/thread.h>
|
---|
23 |
|
---|
24 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
25 | # include <iprt/win/windows.h>
|
---|
26 | # include <iprt/win/shlobj.h> /* For CFSTR_FILEDESCRIPTORXXX + CFSTR_FILECONTENTS. */
|
---|
27 | # include <iprt/utf16.h>
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
|
---|
31 | #include <VBox/log.h>
|
---|
32 |
|
---|
33 | #include <VBox/err.h>
|
---|
34 |
|
---|
35 | #include <VBox/GuestHost/SharedClipboard.h>
|
---|
36 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
37 | # include <VBox/GuestHost/SharedClipboard-uri.h>
|
---|
38 | #endif
|
---|
39 | #include <VBox/GuestHost/SharedClipboard-win.h>
|
---|
40 | #include <VBox/GuestHost/clipboard-helper.h>
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Opens the clipboard of a specific window.
|
---|
44 | *
|
---|
45 | * @returns VBox status code.
|
---|
46 | * @param hWnd Handle of window to open clipboard for.
|
---|
47 | */
|
---|
48 | int VBoxClipboardWinOpen(HWND hWnd)
|
---|
49 | {
|
---|
50 | /* "OpenClipboard fails if another window has the clipboard open."
|
---|
51 | * So try a few times and wait up to 1 second.
|
---|
52 | */
|
---|
53 | BOOL fOpened = FALSE;
|
---|
54 |
|
---|
55 | LogFlowFunc(("hWnd=%p\n", hWnd));
|
---|
56 |
|
---|
57 | int i = 0;
|
---|
58 | for (;;)
|
---|
59 | {
|
---|
60 | if (OpenClipboard(hWnd))
|
---|
61 | {
|
---|
62 | fOpened = TRUE;
|
---|
63 | break;
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (i >= 10) /* sleep interval = [1..512] ms */
|
---|
67 | break;
|
---|
68 |
|
---|
69 | RTThreadSleep(1 << i);
|
---|
70 | ++i;
|
---|
71 | }
|
---|
72 |
|
---|
73 | #ifdef LOG_ENABLED
|
---|
74 | if (i > 0)
|
---|
75 | LogFlowFunc(("%d times tried to open clipboard\n", i + 1));
|
---|
76 | #endif
|
---|
77 |
|
---|
78 | int rc;
|
---|
79 | if (fOpened)
|
---|
80 | rc = VINF_SUCCESS;
|
---|
81 | else
|
---|
82 | {
|
---|
83 | const DWORD dwLastErr = GetLastError();
|
---|
84 | rc = RTErrConvertFromWin32(dwLastErr);
|
---|
85 | LogFunc(("Failed to open clipboard, rc=%Rrc (0x%x)\n", rc, dwLastErr));
|
---|
86 | }
|
---|
87 |
|
---|
88 | return rc;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Closes the clipboard for the current thread.
|
---|
93 | *
|
---|
94 | * @returns VBox status code.
|
---|
95 | */
|
---|
96 | int VBoxClipboardWinClose(void)
|
---|
97 | {
|
---|
98 | int rc;
|
---|
99 |
|
---|
100 | LogFlowFuncEnter();
|
---|
101 |
|
---|
102 | const BOOL fRc = CloseClipboard();
|
---|
103 | if (RT_UNLIKELY(!fRc))
|
---|
104 | {
|
---|
105 | const DWORD dwLastErr = GetLastError();
|
---|
106 | if (dwLastErr == ERROR_CLIPBOARD_NOT_OPEN)
|
---|
107 | rc = VERR_INVALID_STATE;
|
---|
108 | else
|
---|
109 | rc = RTErrConvertFromWin32(dwLastErr);
|
---|
110 |
|
---|
111 | LogFunc(("Failed with %Rrc (0x%x)\n", rc, dwLastErr));
|
---|
112 | }
|
---|
113 | else
|
---|
114 | rc = VINF_SUCCESS;
|
---|
115 |
|
---|
116 | return rc;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Clears the clipboard for the current thread.
|
---|
121 | *
|
---|
122 | * @returns VBox status code.
|
---|
123 | */
|
---|
124 | int VBoxClipboardWinClear(void)
|
---|
125 | {
|
---|
126 | int rc;
|
---|
127 |
|
---|
128 | LogFlowFuncEnter();
|
---|
129 |
|
---|
130 | const BOOL fRc = EmptyClipboard();
|
---|
131 | if (RT_UNLIKELY(!fRc))
|
---|
132 | {
|
---|
133 | const DWORD dwLastErr = GetLastError();
|
---|
134 | if (dwLastErr == ERROR_CLIPBOARD_NOT_OPEN)
|
---|
135 | rc = VERR_INVALID_STATE;
|
---|
136 | else
|
---|
137 | rc = RTErrConvertFromWin32(dwLastErr);
|
---|
138 |
|
---|
139 | LogFunc(("Failed with %Rrc (0x%x)\n", rc, dwLastErr));
|
---|
140 | }
|
---|
141 | else
|
---|
142 | rc = VINF_SUCCESS;
|
---|
143 |
|
---|
144 | return rc;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Checks and initializes function pointer which are required for using
|
---|
149 | * the new clipboard API.
|
---|
150 | *
|
---|
151 | * @returns VBox status code.
|
---|
152 | * @param pAPI Where to store the retrieved function pointers.
|
---|
153 | * Will be set to NULL if the new API is not available.
|
---|
154 | */
|
---|
155 | int VBoxClipboardWinCheckAndInitNewAPI(PVBOXCLIPBOARDWINAPINEW pAPI)
|
---|
156 | {
|
---|
157 | RTLDRMOD hUser32 = NIL_RTLDRMOD;
|
---|
158 | int rc = RTLdrLoadSystem("User32.dll", /* fNoUnload = */ true, &hUser32);
|
---|
159 | if (RT_SUCCESS(rc))
|
---|
160 | {
|
---|
161 | rc = RTLdrGetSymbol(hUser32, "AddClipboardFormatListener", (void **)&pAPI->pfnAddClipboardFormatListener);
|
---|
162 | if (RT_SUCCESS(rc))
|
---|
163 | {
|
---|
164 | rc = RTLdrGetSymbol(hUser32, "RemoveClipboardFormatListener", (void **)&pAPI->pfnRemoveClipboardFormatListener);
|
---|
165 | }
|
---|
166 |
|
---|
167 | RTLdrClose(hUser32);
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (RT_SUCCESS(rc))
|
---|
171 | {
|
---|
172 | LogFunc(("New Clipboard API enabled\n"));
|
---|
173 | }
|
---|
174 | else
|
---|
175 | {
|
---|
176 | RT_BZERO(pAPI, sizeof(VBOXCLIPBOARDWINAPINEW));
|
---|
177 | LogFunc(("New Clipboard API not available; rc=%Rrc\n", rc));
|
---|
178 | }
|
---|
179 |
|
---|
180 | return rc;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Returns if the new clipboard API is available or not.
|
---|
185 | *
|
---|
186 | * @returns @c true if the new API is available, or @c false if not.
|
---|
187 | * @param pAPI Structure used for checking if the new clipboard API is available or not.
|
---|
188 | */
|
---|
189 | bool VBoxClipboardWinIsNewAPI(PVBOXCLIPBOARDWINAPINEW pAPI)
|
---|
190 | {
|
---|
191 | if (!pAPI)
|
---|
192 | return false;
|
---|
193 | return pAPI->pfnAddClipboardFormatListener != NULL;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Adds ourselves into the chain of cliboard listeners.
|
---|
198 | *
|
---|
199 | * @returns VBox status code.
|
---|
200 | * @param pCtx Windows clipboard context to use to add ourselves.
|
---|
201 | */
|
---|
202 | int VBoxClipboardWinChainAdd(PVBOXCLIPBOARDWINCTX pCtx)
|
---|
203 | {
|
---|
204 | const PVBOXCLIPBOARDWINAPINEW pAPI = &pCtx->newAPI;
|
---|
205 |
|
---|
206 | BOOL fRc;
|
---|
207 | if (VBoxClipboardWinIsNewAPI(pAPI))
|
---|
208 | {
|
---|
209 | fRc = pAPI->pfnAddClipboardFormatListener(pCtx->hWnd);
|
---|
210 | }
|
---|
211 | else
|
---|
212 | {
|
---|
213 | pCtx->hWndNextInChain = SetClipboardViewer(pCtx->hWnd);
|
---|
214 | fRc = pCtx->hWndNextInChain != NULL;
|
---|
215 | }
|
---|
216 |
|
---|
217 | int rc = VINF_SUCCESS;
|
---|
218 |
|
---|
219 | if (!fRc)
|
---|
220 | {
|
---|
221 | const DWORD dwLastErr = GetLastError();
|
---|
222 | rc = RTErrConvertFromWin32(dwLastErr);
|
---|
223 | LogFunc(("Failed with %Rrc (0x%x)\n", rc, dwLastErr));
|
---|
224 | }
|
---|
225 |
|
---|
226 | return rc;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Remove ourselves from the chain of cliboard listeners
|
---|
231 | *
|
---|
232 | * @returns VBox status code.
|
---|
233 | * @param pCtx Windows clipboard context to use to remove ourselves.
|
---|
234 | */
|
---|
235 | int VBoxClipboardWinChainRemove(PVBOXCLIPBOARDWINCTX pCtx)
|
---|
236 | {
|
---|
237 | if (!pCtx->hWnd)
|
---|
238 | return VINF_SUCCESS;
|
---|
239 |
|
---|
240 | const PVBOXCLIPBOARDWINAPINEW pAPI = &pCtx->newAPI;
|
---|
241 |
|
---|
242 | BOOL fRc;
|
---|
243 | if (VBoxClipboardWinIsNewAPI(pAPI))
|
---|
244 | {
|
---|
245 | fRc = pAPI->pfnRemoveClipboardFormatListener(pCtx->hWnd);
|
---|
246 | }
|
---|
247 | else
|
---|
248 | {
|
---|
249 | fRc = ChangeClipboardChain(pCtx->hWnd, pCtx->hWndNextInChain);
|
---|
250 | if (fRc)
|
---|
251 | pCtx->hWndNextInChain = NULL;
|
---|
252 | }
|
---|
253 |
|
---|
254 | int rc = VINF_SUCCESS;
|
---|
255 |
|
---|
256 | if (!fRc)
|
---|
257 | {
|
---|
258 | const DWORD dwLastErr = GetLastError();
|
---|
259 | rc = RTErrConvertFromWin32(dwLastErr);
|
---|
260 | LogFunc(("Failed with %Rrc (0x%x)\n", rc, dwLastErr));
|
---|
261 | }
|
---|
262 |
|
---|
263 | return rc;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Callback which is invoked when we have successfully pinged ourselves down the
|
---|
268 | * clipboard chain. We simply unset a boolean flag to say that we are responding.
|
---|
269 | * There is a race if a ping returns after the next one is initiated, but nothing
|
---|
270 | * very bad is likely to happen.
|
---|
271 | *
|
---|
272 | * @param hWnd Window handle to use for this callback. Not used currently.
|
---|
273 | * @param uMsg Message to handle. Not used currently.
|
---|
274 | * @param dwData Pointer to user-provided data. Contains our Windows clipboard context.
|
---|
275 | * @param lResult Additional data to pass. Not used currently.
|
---|
276 | */
|
---|
277 | VOID CALLBACK VBoxClipboardWinChainPingProc(HWND hWnd, UINT uMsg, ULONG_PTR dwData, LRESULT lResult)
|
---|
278 | {
|
---|
279 | RT_NOREF(hWnd);
|
---|
280 | RT_NOREF(uMsg);
|
---|
281 | RT_NOREF(lResult);
|
---|
282 |
|
---|
283 | /** @todo r=andy Why not using SetWindowLongPtr for keeping the context? */
|
---|
284 | PVBOXCLIPBOARDWINCTX pCtx = (PVBOXCLIPBOARDWINCTX)dwData;
|
---|
285 | AssertPtrReturnVoid(pCtx);
|
---|
286 |
|
---|
287 | pCtx->oldAPI.fCBChainPingInProcess = FALSE;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Passes a window message to the next window in the clipboard chain.
|
---|
292 | *
|
---|
293 | * @returns LRESULT
|
---|
294 | * @param pWinCtx Window context to use.
|
---|
295 | * @param msg Window message to pass.
|
---|
296 | * @param wParam WPARAM to pass.
|
---|
297 | * @param lParam LPARAM to pass.
|
---|
298 | */
|
---|
299 | LRESULT VBoxClipboardWinChainPassToNext(PVBOXCLIPBOARDWINCTX pWinCtx,
|
---|
300 | UINT msg, WPARAM wParam, LPARAM lParam)
|
---|
301 | {
|
---|
302 | LogFlowFuncEnter();
|
---|
303 |
|
---|
304 | LRESULT lresultRc = 0;
|
---|
305 |
|
---|
306 | if (pWinCtx->hWndNextInChain)
|
---|
307 | {
|
---|
308 | LogFunc(("hWndNextInChain=%p\n", pWinCtx->hWndNextInChain));
|
---|
309 |
|
---|
310 | /* Pass the message to next window in the clipboard chain. */
|
---|
311 | DWORD_PTR dwResult;
|
---|
312 | lresultRc = SendMessageTimeout(pWinCtx->hWndNextInChain, msg, wParam, lParam, 0,
|
---|
313 | VBOX_CLIPBOARD_CBCHAIN_TIMEOUT_MS, &dwResult);
|
---|
314 | if (!lresultRc)
|
---|
315 | lresultRc = dwResult;
|
---|
316 | }
|
---|
317 |
|
---|
318 | LogFlowFunc(("lresultRc=%ld\n", lresultRc));
|
---|
319 | return lresultRc;
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Converts a (registered or standard) Windows clipboard format to a VBox clipboard format.
|
---|
324 | *
|
---|
325 | * @returns Converted VBox clipboard format, or VBOX_SHARED_CLIPBOARD_FMT_NONE if not found.
|
---|
326 | * @param uFormat Windows clipboard format to convert.
|
---|
327 | */
|
---|
328 | VBOXCLIPBOARDFORMAT VBoxClipboardWinClipboardFormatToVBox(UINT uFormat)
|
---|
329 | {
|
---|
330 | /* Insert the requested clipboard format data into the clipboard. */
|
---|
331 | VBOXCLIPBOARDFORMAT vboxFormat = VBOX_SHARED_CLIPBOARD_FMT_NONE;
|
---|
332 |
|
---|
333 | switch (uFormat)
|
---|
334 | {
|
---|
335 | case CF_UNICODETEXT:
|
---|
336 | vboxFormat = VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT;
|
---|
337 | break;
|
---|
338 |
|
---|
339 | case CF_DIB:
|
---|
340 | vboxFormat = VBOX_SHARED_CLIPBOARD_FMT_BITMAP;
|
---|
341 | break;
|
---|
342 |
|
---|
343 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
344 | /* CF_HDROP handles file system entries which are locally present
|
---|
345 | * on source for transferring to the target.
|
---|
346 | *
|
---|
347 | * This does *not* invoke any IDataObject / IStream implementations! */
|
---|
348 | case CF_HDROP:
|
---|
349 | vboxFormat = VBOX_SHARED_CLIPBOARD_FMT_URI_LIST;
|
---|
350 | break;
|
---|
351 | #endif
|
---|
352 |
|
---|
353 | default:
|
---|
354 | if (uFormat >= 0xC000) /** Formats registered with RegisterClipboardFormat() start at this index. */
|
---|
355 | {
|
---|
356 | TCHAR szFormatName[256]; /** @todo r=andy Do we need Unicode support here as well? */
|
---|
357 | int cActual = GetClipboardFormatName(uFormat, szFormatName, sizeof(szFormatName) / sizeof(TCHAR));
|
---|
358 | if (cActual)
|
---|
359 | {
|
---|
360 | LogFlowFunc(("uFormat=%u -> szFormatName=%s\n", uFormat, szFormatName));
|
---|
361 |
|
---|
362 | if (RTStrCmp(szFormatName, VBOX_CLIPBOARD_WIN_REGFMT_HTML) == 0)
|
---|
363 | vboxFormat = VBOX_SHARED_CLIPBOARD_FMT_HTML;
|
---|
364 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
365 | /* These types invoke our IDataObject / IStream implementations. */
|
---|
366 | else if ( (RTStrCmp(szFormatName, CFSTR_FILEDESCRIPTORA) == 0)
|
---|
367 | || (RTStrCmp(szFormatName, CFSTR_FILECONTENTS) == 0))
|
---|
368 | vboxFormat = VBOX_SHARED_CLIPBOARD_FMT_URI_LIST;
|
---|
369 | /** @todo Do we need to handle CFSTR_FILEDESCRIPTORW here as well? */
|
---|
370 | #endif
|
---|
371 | }
|
---|
372 | }
|
---|
373 | break;
|
---|
374 | }
|
---|
375 |
|
---|
376 | LogFlowFunc(("uFormat=%u -> vboxFormat=0x%x\n", uFormat, vboxFormat));
|
---|
377 | return vboxFormat;
|
---|
378 | }
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Retrieves all supported clipboard formats of a specific clipboard.
|
---|
382 | *
|
---|
383 | * @returns VBox status code.
|
---|
384 | * @param pCtx Windows clipboard context to retrieve formats for.
|
---|
385 | * @param pfFormats Where to store the retrieved formats of type VBOX_SHARED_CLIPBOARD_FMT_ (bitmask).
|
---|
386 | */
|
---|
387 | int VBoxClipboardWinGetFormats(PVBOXCLIPBOARDWINCTX pCtx, PVBOXCLIPBOARDFORMATS pfFormats)
|
---|
388 | {
|
---|
389 | AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
|
---|
390 | AssertPtrReturn(pfFormats, VERR_INVALID_POINTER);
|
---|
391 |
|
---|
392 | VBOXCLIPBOARDFORMATS fFormats = VBOX_SHARED_CLIPBOARD_FMT_NONE;
|
---|
393 |
|
---|
394 | /* Query list of available formats and report to host. */
|
---|
395 | int rc = VBoxClipboardWinOpen(pCtx->hWnd);
|
---|
396 | if (RT_SUCCESS(rc))
|
---|
397 | {
|
---|
398 | UINT uCurFormat = 0; /* Must be set to zero for EnumClipboardFormats(). */
|
---|
399 | while ((uCurFormat = EnumClipboardFormats(uCurFormat)) != 0)
|
---|
400 | fFormats |= VBoxClipboardWinClipboardFormatToVBox(uCurFormat);
|
---|
401 |
|
---|
402 | int rc2 = VBoxClipboardWinClose();
|
---|
403 | AssertRC(rc2);
|
---|
404 | }
|
---|
405 |
|
---|
406 | if (RT_FAILURE(rc))
|
---|
407 | {
|
---|
408 | LogFunc(("Failed with rc=%Rrc\n", rc));
|
---|
409 | }
|
---|
410 | else
|
---|
411 | {
|
---|
412 | LogFlowFunc(("fFormats=0x%08X\n", fFormats));
|
---|
413 | *pfFormats = fFormats;
|
---|
414 | }
|
---|
415 |
|
---|
416 | return rc;
|
---|
417 | }
|
---|
418 |
|
---|
419 | /**
|
---|
420 | * Extracts a field value from CF_HTML data.
|
---|
421 | *
|
---|
422 | * @returns VBox status code.
|
---|
423 | * @param pszSrc source in CF_HTML format.
|
---|
424 | * @param pszOption Name of CF_HTML field.
|
---|
425 | * @param puValue Where to return extracted value of CF_HTML field.
|
---|
426 | */
|
---|
427 | int VBoxClipboardWinGetCFHTMLHeaderValue(const char *pszSrc, const char *pszOption, uint32_t *puValue)
|
---|
428 | {
|
---|
429 | AssertPtrReturn(pszSrc, VERR_INVALID_POINTER);
|
---|
430 | AssertPtrReturn(pszOption, VERR_INVALID_POINTER);
|
---|
431 |
|
---|
432 | int rc = VERR_INVALID_PARAMETER;
|
---|
433 |
|
---|
434 | const char *pszOptionValue = RTStrStr(pszSrc, pszOption);
|
---|
435 | if (pszOptionValue)
|
---|
436 | {
|
---|
437 | size_t cchOption = strlen(pszOption);
|
---|
438 | Assert(cchOption);
|
---|
439 |
|
---|
440 | rc = RTStrToUInt32Ex(pszOptionValue + cchOption, NULL, 10, puValue);
|
---|
441 | }
|
---|
442 | return rc;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /**
|
---|
446 | * Check that the source string contains CF_HTML struct.
|
---|
447 | *
|
---|
448 | * @returns @c true if the @a pszSource string is in CF_HTML format.
|
---|
449 | * @param pszSource Source string to check.
|
---|
450 | */
|
---|
451 | bool VBoxClipboardWinIsCFHTML(const char *pszSource)
|
---|
452 | {
|
---|
453 | return RTStrStr(pszSource, "Version:") != NULL
|
---|
454 | && RTStrStr(pszSource, "StartHTML:") != NULL;
|
---|
455 | }
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * Converts clipboard data from CF_HTML format to MIME clipboard format.
|
---|
459 | *
|
---|
460 | * Returns allocated buffer that contains html converted to text/html mime type
|
---|
461 | *
|
---|
462 | * @returns VBox status code.
|
---|
463 | * @param pszSource The input.
|
---|
464 | * @param cch The length of the input.
|
---|
465 | * @param ppszOutput Where to return the result. Free using RTMemFree.
|
---|
466 | * @param pcbOutput Where to the return length of the result (bytes/chars).
|
---|
467 | */
|
---|
468 | int VBoxClipboardWinConvertCFHTMLToMIME(const char *pszSource, const uint32_t cch, char **ppszOutput, uint32_t *pcbOutput)
|
---|
469 | {
|
---|
470 | Assert(pszSource);
|
---|
471 | Assert(cch);
|
---|
472 | Assert(ppszOutput);
|
---|
473 | Assert(pcbOutput);
|
---|
474 |
|
---|
475 | uint32_t offStart;
|
---|
476 | int rc = VBoxClipboardWinGetCFHTMLHeaderValue(pszSource, "StartFragment:", &offStart);
|
---|
477 | if (RT_SUCCESS(rc))
|
---|
478 | {
|
---|
479 | uint32_t offEnd;
|
---|
480 | rc = VBoxClipboardWinGetCFHTMLHeaderValue(pszSource, "EndFragment:", &offEnd);
|
---|
481 | if (RT_SUCCESS(rc))
|
---|
482 | {
|
---|
483 | if ( offStart > 0
|
---|
484 | && offEnd > 0
|
---|
485 | && offEnd > offStart
|
---|
486 | && offEnd <= cch)
|
---|
487 | {
|
---|
488 | uint32_t cchSubStr = offEnd - offStart;
|
---|
489 | char *pszResult = (char *)RTMemAlloc(cchSubStr + 1);
|
---|
490 | if (pszResult)
|
---|
491 | {
|
---|
492 | rc = RTStrCopyEx(pszResult, cchSubStr + 1, pszSource + offStart, cchSubStr);
|
---|
493 | if (RT_SUCCESS(rc))
|
---|
494 | {
|
---|
495 | *ppszOutput = pszResult;
|
---|
496 | *pcbOutput = (uint32_t)(cchSubStr + 1);
|
---|
497 | rc = VINF_SUCCESS;
|
---|
498 | }
|
---|
499 | else
|
---|
500 | {
|
---|
501 | LogRelFlowFunc(("Error: Unknown CF_HTML format. Expected EndFragment. rc = %Rrc\n", rc));
|
---|
502 | RTMemFree(pszResult);
|
---|
503 | }
|
---|
504 | }
|
---|
505 | else
|
---|
506 | {
|
---|
507 | LogRelFlowFunc(("Error: Unknown CF_HTML format. Expected EndFragment\n"));
|
---|
508 | rc = VERR_NO_MEMORY;
|
---|
509 | }
|
---|
510 | }
|
---|
511 | else
|
---|
512 | {
|
---|
513 | LogRelFlowFunc(("Error: CF_HTML out of bounds - offStart=%#x offEnd=%#x cch=%#x\n", offStart, offEnd, cch));
|
---|
514 | rc = VERR_INVALID_PARAMETER;
|
---|
515 | }
|
---|
516 | }
|
---|
517 | else
|
---|
518 | {
|
---|
519 | LogRelFlowFunc(("Error: Unknown CF_HTML format. Expected EndFragment. rc = %Rrc\n", rc));
|
---|
520 | rc = VERR_INVALID_PARAMETER;
|
---|
521 | }
|
---|
522 | }
|
---|
523 | else
|
---|
524 | {
|
---|
525 | LogRelFlowFunc(("Error: Unknown CF_HTML format. Expected StartFragment. rc = %Rrc\n", rc));
|
---|
526 | rc = VERR_INVALID_PARAMETER;
|
---|
527 | }
|
---|
528 |
|
---|
529 | return rc;
|
---|
530 | }
|
---|
531 |
|
---|
532 | /**
|
---|
533 | * Converts source UTF-8 MIME HTML clipboard data to UTF-8 CF_HTML format.
|
---|
534 | *
|
---|
535 | * This is just encapsulation work, slapping a header on the data.
|
---|
536 | *
|
---|
537 | * It allocates [..]
|
---|
538 | *
|
---|
539 | * Calculations:
|
---|
540 | * Header length = format Length + (2*(10 - 5('%010d'))('digits')) - 2('%s') = format length + 8
|
---|
541 | * EndHtml = Header length + fragment length
|
---|
542 | * StartHtml = 105(constant)
|
---|
543 | * StartFragment = 141(constant) may vary if the header html content will be extended
|
---|
544 | * EndFragment = Header length + fragment length - 38(ending length)
|
---|
545 | *
|
---|
546 | * @param pszSource Source buffer that contains utf-16 string in mime html format
|
---|
547 | * @param cb Size of source buffer in bytes
|
---|
548 | * @param ppszOutput Where to return the allocated output buffer to put converted UTF-8
|
---|
549 | * CF_HTML clipboard data. This function allocates memory for this.
|
---|
550 | * @param pcbOutput Where to return the size of allocated result buffer in bytes/chars, including zero terminator
|
---|
551 | *
|
---|
552 | * @note output buffer should be free using RTMemFree()
|
---|
553 | * @note Everything inside of fragment can be UTF8. Windows allows it. Everything in header should be Latin1.
|
---|
554 | */
|
---|
555 | int VBoxClipboardWinConvertMIMEToCFHTML(const char *pszSource, size_t cb, char **ppszOutput, uint32_t *pcbOutput)
|
---|
556 | {
|
---|
557 | Assert(ppszOutput);
|
---|
558 | Assert(pcbOutput);
|
---|
559 | Assert(pszSource);
|
---|
560 | Assert(cb);
|
---|
561 |
|
---|
562 | /* construct CF_HTML formatted string */
|
---|
563 | char *pszResult = NULL;
|
---|
564 | size_t cchFragment;
|
---|
565 | int rc = RTStrNLenEx(pszSource, cb, &cchFragment);
|
---|
566 | if (!RT_SUCCESS(rc))
|
---|
567 | {
|
---|
568 | LogRelFlowFunc(("Error: invalid source fragment. rc = %Rrc\n"));
|
---|
569 | return VERR_INVALID_PARAMETER;
|
---|
570 | }
|
---|
571 |
|
---|
572 | /*
|
---|
573 | @StartHtml - pos before <html>
|
---|
574 | @EndHtml - whole size of text excluding ending zero char
|
---|
575 | @StartFragment - pos after <!--StartFragment-->
|
---|
576 | @EndFragment - pos before <!--EndFragment-->
|
---|
577 | @note: all values includes CR\LF inserted into text
|
---|
578 | Calculations:
|
---|
579 | Header length = format Length + (3*6('digits')) - 2('%s') = format length + 16 (control value - 183)
|
---|
580 | EndHtml = Header length + fragment length
|
---|
581 | StartHtml = 105(constant)
|
---|
582 | StartFragment = 143(constant)
|
---|
583 | EndFragment = Header length + fragment length - 40(ending length)
|
---|
584 | */
|
---|
585 | static const char s_szFormatSample[] =
|
---|
586 | /* 0: */ "Version:1.0\r\n"
|
---|
587 | /* 13: */ "StartHTML:000000101\r\n"
|
---|
588 | /* 34: */ "EndHTML:%0000009u\r\n" // END HTML = Header length + fragment length
|
---|
589 | /* 53: */ "StartFragment:000000137\r\n"
|
---|
590 | /* 78: */ "EndFragment:%0000009u\r\n"
|
---|
591 | /* 101: */ "<html>\r\n"
|
---|
592 | /* 109: */ "<body>\r\n"
|
---|
593 | /* 117: */ "<!--StartFragment-->"
|
---|
594 | /* 137: */ "%s"
|
---|
595 | /* 137+2: */ "<!--EndFragment-->\r\n"
|
---|
596 | /* 157+2: */ "</body>\r\n"
|
---|
597 | /* 166+2: */ "</html>\r\n";
|
---|
598 | /* 175+2: */
|
---|
599 | AssertCompile(sizeof(s_szFormatSample) == 175 + 2 + 1);
|
---|
600 |
|
---|
601 | /* calculate parameters of CF_HTML header */
|
---|
602 | size_t cchHeader = sizeof(s_szFormatSample) - 1;
|
---|
603 | size_t offEndHtml = cchHeader + cchFragment;
|
---|
604 | size_t offEndFragment = cchHeader + cchFragment - 38; /* 175-137 = 38 */
|
---|
605 | pszResult = (char *)RTMemAlloc(offEndHtml + 1);
|
---|
606 | if (pszResult == NULL)
|
---|
607 | {
|
---|
608 | LogRelFlowFunc(("Error: Cannot allocate memory for result buffer. rc = %Rrc\n"));
|
---|
609 | return VERR_NO_MEMORY;
|
---|
610 | }
|
---|
611 |
|
---|
612 | /* format result CF_HTML string */
|
---|
613 | size_t cchFormatted = RTStrPrintf(pszResult, offEndHtml + 1,
|
---|
614 | s_szFormatSample, offEndHtml, offEndFragment, pszSource);
|
---|
615 | Assert(offEndHtml == cchFormatted); NOREF(cchFormatted);
|
---|
616 |
|
---|
617 | #ifdef VBOX_STRICT
|
---|
618 | /* Control calculations. check consistency.*/
|
---|
619 | static const char s_szStartFragment[] = "<!--StartFragment-->";
|
---|
620 | static const char s_szEndFragment[] = "<!--EndFragment-->";
|
---|
621 |
|
---|
622 | /* check 'StartFragment:' value */
|
---|
623 | const char *pszRealStartFragment = RTStrStr(pszResult, s_szStartFragment);
|
---|
624 | Assert(&pszRealStartFragment[sizeof(s_szStartFragment) - 1] - pszResult == 137);
|
---|
625 |
|
---|
626 | /* check 'EndFragment:' value */
|
---|
627 | const char *pszRealEndFragment = RTStrStr(pszResult, s_szEndFragment);
|
---|
628 | Assert((size_t)(pszRealEndFragment - pszResult) == offEndFragment);
|
---|
629 | #endif
|
---|
630 |
|
---|
631 | *ppszOutput = pszResult;
|
---|
632 | *pcbOutput = (uint32_t)cchFormatted + 1;
|
---|
633 | Assert(*pcbOutput == cchFormatted + 1);
|
---|
634 |
|
---|
635 | return VINF_SUCCESS;
|
---|
636 | }
|
---|
637 |
|
---|
638 | /**
|
---|
639 | * Handles the WM_CHANGECBCHAIN code.
|
---|
640 | *
|
---|
641 | * @returns LRESULT
|
---|
642 | * @param pWinCtx Windows context to use.
|
---|
643 | * @param hWnd Window handle to use.
|
---|
644 | * @param msg Message ID to pass on.
|
---|
645 | * @param wParam wParam to pass on
|
---|
646 | * @param lParam lParam to pass on.
|
---|
647 | */
|
---|
648 | LRESULT VBoxClipboardWinHandleWMChangeCBChain(PVBOXCLIPBOARDWINCTX pWinCtx,
|
---|
649 | HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
---|
650 | {
|
---|
651 | LRESULT lresultRc = 0;
|
---|
652 |
|
---|
653 | LogFlowFuncEnter();
|
---|
654 |
|
---|
655 | if (VBoxClipboardWinIsNewAPI(&pWinCtx->newAPI))
|
---|
656 | {
|
---|
657 | lresultRc = DefWindowProc(hWnd, msg, wParam, lParam);
|
---|
658 | }
|
---|
659 | else /* Old API */
|
---|
660 | {
|
---|
661 | HWND hwndRemoved = (HWND)wParam;
|
---|
662 | HWND hwndNext = (HWND)lParam;
|
---|
663 |
|
---|
664 | if (hwndRemoved == pWinCtx->hWndNextInChain)
|
---|
665 | {
|
---|
666 | /* The window that was next to our in the chain is being removed.
|
---|
667 | * Relink to the new next window.
|
---|
668 | */
|
---|
669 | pWinCtx->hWndNextInChain = hwndNext;
|
---|
670 | }
|
---|
671 | else
|
---|
672 | {
|
---|
673 | if (pWinCtx->hWndNextInChain)
|
---|
674 | {
|
---|
675 | /* Pass the message further. */
|
---|
676 | DWORD_PTR dwResult;
|
---|
677 | lresultRc = SendMessageTimeout(pWinCtx->hWndNextInChain, WM_CHANGECBCHAIN, wParam, lParam, 0,
|
---|
678 | VBOX_CLIPBOARD_CBCHAIN_TIMEOUT_MS,
|
---|
679 | &dwResult);
|
---|
680 | if (!lresultRc)
|
---|
681 | lresultRc = (LRESULT)dwResult;
|
---|
682 | }
|
---|
683 | }
|
---|
684 | }
|
---|
685 |
|
---|
686 | LogFlowFunc(("lresultRc=%ld\n", lresultRc));
|
---|
687 | return lresultRc;
|
---|
688 | }
|
---|
689 |
|
---|
690 | /**
|
---|
691 | * Handles the WM_DESTROY code.
|
---|
692 | *
|
---|
693 | * @returns VBox status code.
|
---|
694 | * @param pWinCtx Windows context to use.
|
---|
695 | */
|
---|
696 | int VBoxClipboardWinHandleWMDestroy(PVBOXCLIPBOARDWINCTX pWinCtx)
|
---|
697 | {
|
---|
698 | LogFlowFuncEnter();
|
---|
699 |
|
---|
700 | int rc = VINF_SUCCESS;
|
---|
701 |
|
---|
702 | /* MS recommends to remove from Clipboard chain in this callback. */
|
---|
703 | VBoxClipboardWinChainRemove(pWinCtx);
|
---|
704 |
|
---|
705 | if (pWinCtx->oldAPI.timerRefresh)
|
---|
706 | {
|
---|
707 | Assert(pWinCtx->hWnd);
|
---|
708 | KillTimer(pWinCtx->hWnd, 0);
|
---|
709 | }
|
---|
710 |
|
---|
711 | LogFlowFuncLeaveRC(rc);
|
---|
712 | return rc;
|
---|
713 | }
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * Handles the WM_RENDERALLFORMATS message.
|
---|
717 | *
|
---|
718 | * @returns VBox status code.
|
---|
719 | * @param pWinCtx Windows context to use.
|
---|
720 | * @param hWnd Window handle to use.
|
---|
721 | */
|
---|
722 | int VBoxClipboardWinHandleWMRenderAllFormats(PVBOXCLIPBOARDWINCTX pWinCtx, HWND hWnd)
|
---|
723 | {
|
---|
724 | RT_NOREF(pWinCtx);
|
---|
725 |
|
---|
726 | LogFlowFuncEnter();
|
---|
727 |
|
---|
728 | /* Do nothing. The clipboard formats will be unavailable now, because the
|
---|
729 | * windows is to be destroyed and therefore the guest side becomes inactive.
|
---|
730 | */
|
---|
731 | int rc = VBoxClipboardWinOpen(hWnd);
|
---|
732 | if (RT_SUCCESS(rc))
|
---|
733 | {
|
---|
734 | VBoxClipboardWinClear();
|
---|
735 | VBoxClipboardWinClose();
|
---|
736 | }
|
---|
737 |
|
---|
738 | LogFlowFuncLeaveRC(rc);
|
---|
739 | return rc;
|
---|
740 | }
|
---|
741 |
|
---|
742 | /**
|
---|
743 | * Handles the WM_TIMER code, which is needed if we're running with the so-called "old" Windows clipboard API.
|
---|
744 | * Does nothing if we're running with the "new" Windows API.
|
---|
745 | *
|
---|
746 | * @returns VBox status code.
|
---|
747 | * @param pWinCtx Windows context to use.
|
---|
748 | */
|
---|
749 | int VBoxClipboardWinHandleWMTimer(PVBOXCLIPBOARDWINCTX pWinCtx)
|
---|
750 | {
|
---|
751 | int rc = VINF_SUCCESS;
|
---|
752 |
|
---|
753 | if (!VBoxClipboardWinIsNewAPI(&pWinCtx->newAPI)) /* Only run when using the "old" Windows API. */
|
---|
754 | {
|
---|
755 | LogFlowFuncEnter();
|
---|
756 |
|
---|
757 | HWND hViewer = GetClipboardViewer();
|
---|
758 |
|
---|
759 | /* Re-register ourselves in the clipboard chain if our last ping
|
---|
760 | * timed out or there seems to be no valid chain. */
|
---|
761 | if (!hViewer || pWinCtx->oldAPI.fCBChainPingInProcess)
|
---|
762 | {
|
---|
763 | VBoxClipboardWinChainRemove(pWinCtx);
|
---|
764 | VBoxClipboardWinChainAdd(pWinCtx);
|
---|
765 | }
|
---|
766 |
|
---|
767 | /* Start a new ping by passing a dummy WM_CHANGECBCHAIN to be
|
---|
768 | * processed by ourselves to the chain. */
|
---|
769 | pWinCtx->oldAPI.fCBChainPingInProcess = TRUE;
|
---|
770 |
|
---|
771 | hViewer = GetClipboardViewer();
|
---|
772 | if (hViewer)
|
---|
773 | SendMessageCallback(hViewer, WM_CHANGECBCHAIN, (WPARAM)pWinCtx->hWndNextInChain, (LPARAM)pWinCtx->hWndNextInChain,
|
---|
774 | VBoxClipboardWinChainPingProc, (ULONG_PTR)pWinCtx);
|
---|
775 | }
|
---|
776 |
|
---|
777 | LogFlowFuncLeaveRC(rc);
|
---|
778 | return rc;
|
---|
779 | }
|
---|
780 |
|
---|
781 | /**
|
---|
782 | * Announces a clipboard format to the Windows clipboard.
|
---|
783 | * The actual rendering (setting) of the clipboard data will be done later with a separate WM_RENDERFORMAT message.
|
---|
784 | *
|
---|
785 | * @returns VBox status code. VERR_NOT_SUPPORTED if the format is not supported / handled.
|
---|
786 | * @param pWinCtx Windows context to use.
|
---|
787 | * @param fFormats Clipboard format(s) to announce.
|
---|
788 | */
|
---|
789 | int VBoxClipboardWinAnnounceFormats(PVBOXCLIPBOARDWINCTX pWinCtx, VBOXCLIPBOARDFORMATS fFormats)
|
---|
790 | {
|
---|
791 | LogFunc(("fFormats=0x%x\n", fFormats));
|
---|
792 |
|
---|
793 | HANDLE hClip = NULL;
|
---|
794 | UINT cfFormat = 0;
|
---|
795 |
|
---|
796 | int rc = VINF_SUCCESS;
|
---|
797 |
|
---|
798 | /** @todo r=andy Only one clipboard format can be set at once, at least on Windows. */
|
---|
799 | /** @todo Implement more flexible clipboard precedence for supported formats. */
|
---|
800 |
|
---|
801 | if (fFormats & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
|
---|
802 | {
|
---|
803 | LogFunc(("CF_UNICODETEXT\n"));
|
---|
804 | hClip = SetClipboardData(CF_UNICODETEXT, NULL);
|
---|
805 | }
|
---|
806 | else if (fFormats & VBOX_SHARED_CLIPBOARD_FMT_BITMAP)
|
---|
807 | {
|
---|
808 | LogFunc(("CF_DIB\n"));
|
---|
809 | hClip = SetClipboardData(CF_DIB, NULL);
|
---|
810 | }
|
---|
811 | else if (fFormats & VBOX_SHARED_CLIPBOARD_FMT_HTML)
|
---|
812 | {
|
---|
813 | LogFunc(("VBOX_SHARED_CLIPBOARD_FMT_HTML\n"));
|
---|
814 | cfFormat = RegisterClipboardFormat(VBOX_CLIPBOARD_WIN_REGFMT_HTML);
|
---|
815 | if (cfFormat != 0)
|
---|
816 | hClip = SetClipboardData(cfFormat, NULL);
|
---|
817 | }
|
---|
818 | else
|
---|
819 | {
|
---|
820 | LogRel(("Shared Clipboard: Unsupported format(s) (0x%x), skipping\n", fFormats));
|
---|
821 | rc = VERR_NOT_SUPPORTED;
|
---|
822 | }
|
---|
823 |
|
---|
824 | if (RT_SUCCESS(rc))
|
---|
825 | {
|
---|
826 | pWinCtx->hWndClipboardOwnerUs = GetClipboardOwner();
|
---|
827 | }
|
---|
828 |
|
---|
829 | LogFlowFuncLeaveRC(rc);
|
---|
830 | return rc;
|
---|
831 | }
|
---|
832 |
|
---|
833 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
834 | /**
|
---|
835 | * Main entry function for reading an URI transfer from a source.
|
---|
836 | *
|
---|
837 | * @returns VBox status code.
|
---|
838 | * @param pWinCtx Windows context to use.
|
---|
839 | * @param pURICtx URI context to use.
|
---|
840 | * @param pProviderCtx Provider (creation) context to use.
|
---|
841 | * @param fFormats Format to handle for the transfer.
|
---|
842 | */
|
---|
843 | int VBoxClipboardWinURIReadMain(PVBOXCLIPBOARDWINCTX pWinCtx, PSHAREDCLIPBOARDURICTX pURICtx,
|
---|
844 | PSHAREDCLIPBOARDPROVIDERCREATIONCTX pProviderCtx, VBOXCLIPBOARDFORMATS fFormats)
|
---|
845 | {
|
---|
846 | AssertPtrReturn(pURICtx, VERR_INVALID_POINTER);
|
---|
847 | AssertPtrReturn(pProviderCtx, VERR_INVALID_POINTER);
|
---|
848 |
|
---|
849 | /* Sanity. */
|
---|
850 | AssertReturn(fFormats & VBOX_SHARED_CLIPBOARD_FMT_URI_LIST, VERR_NOT_SUPPORTED);
|
---|
851 |
|
---|
852 | LogFlowFunc(("fFormats=0x%x\n", fFormats));
|
---|
853 |
|
---|
854 | if (SharedClipboardURICtxMaximumTransfersReached(pURICtx))
|
---|
855 | {
|
---|
856 | LogRel(("Shared Clipboard: Only one transfer at a time supported (current %RU32 transfer(s) active), skipping\n",
|
---|
857 | SharedClipboardURICtxGetActiveTransfers(pURICtx)));
|
---|
858 | return VERR_SHCLPB_MAX_TRANSFERS_REACHED;
|
---|
859 | }
|
---|
860 |
|
---|
861 | /* We want to read from the source. */
|
---|
862 | PSHAREDCLIPBOARDURITRANSFER pTransfer;
|
---|
863 | int rc = SharedClipboardURITransferCreate(SHAREDCLIPBOARDURITRANSFERDIR_READ, pProviderCtx, &pTransfer);
|
---|
864 | if (RT_SUCCESS(rc))
|
---|
865 | {
|
---|
866 | rc = SharedClipboardURICtxTransferAdd(pURICtx, pTransfer);
|
---|
867 | if (RT_SUCCESS(rc))
|
---|
868 | {
|
---|
869 | SharedClipboardWinURITransferCtx *pWinURITransferCtx = new SharedClipboardWinURITransferCtx();
|
---|
870 | if (pWinURITransferCtx)
|
---|
871 | {
|
---|
872 | pTransfer->pvUser = pWinURITransferCtx;
|
---|
873 | pTransfer->cbUser = sizeof(SharedClipboardWinURITransferCtx);
|
---|
874 |
|
---|
875 | pWinURITransferCtx->pDataObj = new VBoxClipboardWinDataObject(pTransfer);
|
---|
876 | if (pWinURITransferCtx->pDataObj)
|
---|
877 | {
|
---|
878 | rc = pWinURITransferCtx->pDataObj->Init();
|
---|
879 | if (RT_SUCCESS(rc))
|
---|
880 | {
|
---|
881 | VBoxClipboardWinClose();
|
---|
882 | /* Note: Clipboard must be closed first before calling OleSetClipboard(). */
|
---|
883 |
|
---|
884 | /** @todo There is a potential race between VBoxClipboardWinClose() and OleSetClipboard(),
|
---|
885 | * where another application could own the clipboard (open), and thus the call to
|
---|
886 | * OleSetClipboard() will fail. Needs (better) fixing. */
|
---|
887 | for (unsigned uTries = 0; uTries < 3; uTries++)
|
---|
888 | {
|
---|
889 | HRESULT hr = OleSetClipboard(pWinURITransferCtx->pDataObj);
|
---|
890 | if (SUCCEEDED(hr))
|
---|
891 | {
|
---|
892 | /*
|
---|
893 | * Calling OleSetClipboard() changed the clipboard owner, which in turn will let us receive
|
---|
894 | * a WM_CLIPBOARDUPDATE message. To not confuse ourselves with our own clipboard owner changes,
|
---|
895 | * save a new window handle and deal with it in WM_CLIPBOARDUPDATE.
|
---|
896 | */
|
---|
897 | pWinCtx->hWndClipboardOwnerUs = GetClipboardOwner();
|
---|
898 | break;
|
---|
899 | }
|
---|
900 | else
|
---|
901 | {
|
---|
902 | rc = VERR_ACCESS_DENIED; /** @todo Fudge; fix this. */
|
---|
903 | LogRel(("Shared Clipboard: Failed with %Rhrc when setting data object to clipboard\n", hr));
|
---|
904 | RTThreadSleep(100); /* Wait a bit. */
|
---|
905 | }
|
---|
906 | }
|
---|
907 | }
|
---|
908 | }
|
---|
909 | else
|
---|
910 | rc = VERR_NO_MEMORY;
|
---|
911 | }
|
---|
912 | else
|
---|
913 | rc = VERR_NO_MEMORY;
|
---|
914 | }
|
---|
915 | }
|
---|
916 |
|
---|
917 | LogFlowFuncLeaveRC(rc);
|
---|
918 | return rc;
|
---|
919 | }
|
---|
920 |
|
---|
921 | /**
|
---|
922 | * Converts a DROPFILES (HDROP) structure to a string list, separated by \r\n.
|
---|
923 | * Does not do any locking on the input data.
|
---|
924 | *
|
---|
925 | * @returns VBox status code.
|
---|
926 | * @param pDropFiles Pointer to DROPFILES structure to convert.
|
---|
927 | * @param ppszData Where to return the converted (allocated) data on success.
|
---|
928 | * Must be free'd by the caller with RTMemFree().
|
---|
929 | * @param pcbData Size (in bytes) of the allocated data returned.
|
---|
930 | */
|
---|
931 | int VBoxClipboardWinDropFilesToStringList(DROPFILES *pDropFiles, char **ppszbData, size_t *pcbData)
|
---|
932 | {
|
---|
933 | AssertPtrReturn(pDropFiles, VERR_INVALID_POINTER);
|
---|
934 | AssertPtrReturn(ppszbData, VERR_INVALID_POINTER);
|
---|
935 | AssertPtrReturn(pcbData, VERR_INVALID_POINTER);
|
---|
936 |
|
---|
937 | /* Do we need to do Unicode stuff? */
|
---|
938 | const bool fUnicode = RT_BOOL(pDropFiles->fWide);
|
---|
939 |
|
---|
940 | /* Get the offset of the file list. */
|
---|
941 | Assert(pDropFiles->pFiles >= sizeof(DROPFILES));
|
---|
942 |
|
---|
943 | /* Note: This is *not* pDropFiles->pFiles! DragQueryFile only
|
---|
944 | * will work with the plain storage medium pointer! */
|
---|
945 | HDROP hDrop = (HDROP)(pDropFiles);
|
---|
946 |
|
---|
947 | int rc = VINF_SUCCESS;
|
---|
948 |
|
---|
949 | /* First, get the file count. */
|
---|
950 | /** @todo Does this work on Windows 2000 / NT4? */
|
---|
951 | char *pszFiles = NULL;
|
---|
952 | uint32_t cchFiles = 0;
|
---|
953 | UINT cFiles = DragQueryFile(hDrop, UINT32_MAX /* iFile */, NULL /* lpszFile */, 0 /* cchFile */);
|
---|
954 |
|
---|
955 | LogFlowFunc(("Got %RU16 file(s), fUnicode=%RTbool\n", cFiles, fUnicode));
|
---|
956 |
|
---|
957 | for (UINT i = 0; i < cFiles; i++)
|
---|
958 | {
|
---|
959 | UINT cchFile = DragQueryFile(hDrop, i /* File index */, NULL /* Query size first */, 0 /* cchFile */);
|
---|
960 | Assert(cchFile);
|
---|
961 |
|
---|
962 | if (RT_FAILURE(rc))
|
---|
963 | break;
|
---|
964 |
|
---|
965 | char *pszFileUtf8 = NULL; /* UTF-8 version. */
|
---|
966 | UINT cchFileUtf8 = 0;
|
---|
967 | if (fUnicode)
|
---|
968 | {
|
---|
969 | /* Allocate enough space (including terminator). */
|
---|
970 | WCHAR *pwszFile = (WCHAR *)RTMemAlloc((cchFile + 1) * sizeof(WCHAR));
|
---|
971 | if (pwszFile)
|
---|
972 | {
|
---|
973 | const UINT cwcFileUtf16 = DragQueryFileW(hDrop, i /* File index */,
|
---|
974 | pwszFile, cchFile + 1 /* Include terminator */);
|
---|
975 |
|
---|
976 | AssertMsg(cwcFileUtf16 == cchFile, ("cchFileUtf16 (%RU16) does not match cchFile (%RU16)\n",
|
---|
977 | cwcFileUtf16, cchFile));
|
---|
978 | RT_NOREF(cwcFileUtf16);
|
---|
979 |
|
---|
980 | rc = RTUtf16ToUtf8(pwszFile, &pszFileUtf8);
|
---|
981 | if (RT_SUCCESS(rc))
|
---|
982 | {
|
---|
983 | cchFileUtf8 = (UINT)strlen(pszFileUtf8);
|
---|
984 | Assert(cchFileUtf8);
|
---|
985 | }
|
---|
986 |
|
---|
987 | RTMemFree(pwszFile);
|
---|
988 | }
|
---|
989 | else
|
---|
990 | rc = VERR_NO_MEMORY;
|
---|
991 | }
|
---|
992 | else /* ANSI */
|
---|
993 | {
|
---|
994 | /* Allocate enough space (including terminator). */
|
---|
995 | pszFileUtf8 = (char *)RTMemAlloc((cchFile + 1) * sizeof(char));
|
---|
996 | if (pszFileUtf8)
|
---|
997 | {
|
---|
998 | cchFileUtf8 = DragQueryFileA(hDrop, i /* File index */,
|
---|
999 | pszFileUtf8, cchFile + 1 /* Include terminator */);
|
---|
1000 |
|
---|
1001 | AssertMsg(cchFileUtf8 == cchFile, ("cchFileUtf8 (%RU16) does not match cchFile (%RU16)\n",
|
---|
1002 | cchFileUtf8, cchFile));
|
---|
1003 | }
|
---|
1004 | else
|
---|
1005 | rc = VERR_NO_MEMORY;
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | if (RT_SUCCESS(rc))
|
---|
1009 | {
|
---|
1010 | LogFlowFunc(("\tFile: %s (cchFile=%RU16)\n", pszFileUtf8, cchFileUtf8));
|
---|
1011 |
|
---|
1012 | LogRel(("Shared Clipboard: Adding guest file '%s'\n", pszFileUtf8));
|
---|
1013 |
|
---|
1014 | rc = RTStrAAppendExN(&pszFiles, 1 /* cPairs */, pszFileUtf8, cchFileUtf8);
|
---|
1015 | if (RT_SUCCESS(rc))
|
---|
1016 | cchFiles += cchFileUtf8;
|
---|
1017 | }
|
---|
1018 | else
|
---|
1019 | LogFunc(("Error handling file entry #%u, rc=%Rrc\n", i, rc));
|
---|
1020 |
|
---|
1021 | if (pszFileUtf8)
|
---|
1022 | RTStrFree(pszFileUtf8);
|
---|
1023 |
|
---|
1024 | if (RT_FAILURE(rc))
|
---|
1025 | break;
|
---|
1026 |
|
---|
1027 | /* Add separation between filenames.
|
---|
1028 | * Note: Also do this for the last element of the list. */
|
---|
1029 | rc = RTStrAAppendExN(&pszFiles, 1 /* cPairs */, "\r\n", 2 /* Bytes */);
|
---|
1030 | if (RT_SUCCESS(rc))
|
---|
1031 | cchFiles += 2; /* Include \r\n */
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | if (RT_SUCCESS(rc))
|
---|
1035 | {
|
---|
1036 | cchFiles += 1; /* Add string termination. */
|
---|
1037 | uint32_t cbFiles = cchFiles * sizeof(char);
|
---|
1038 |
|
---|
1039 | LogFlowFunc(("cFiles=%u, cchFiles=%RU32, cbFiles=%RU32, pszFiles=0x%p\n",
|
---|
1040 | cFiles, cchFiles, cbFiles, pszFiles));
|
---|
1041 |
|
---|
1042 | /* Translate the list into URI elements. */
|
---|
1043 | SharedClipboardURIList lstURI;
|
---|
1044 | rc = lstURI.AppendNativePathsFromList(pszFiles, cbFiles,
|
---|
1045 | SHAREDCLIPBOARDURILIST_FLAGS_ABSOLUTE_PATHS);
|
---|
1046 | if (RT_SUCCESS(rc))
|
---|
1047 | {
|
---|
1048 | RTCString strRoot = lstURI.GetRootEntries();
|
---|
1049 | size_t cbRoot = strRoot.length() + 1; /* Include termination */
|
---|
1050 |
|
---|
1051 | void *pvData = RTMemAlloc(cbRoot);
|
---|
1052 | if (pvData)
|
---|
1053 | {
|
---|
1054 | memcpy(pvData, strRoot.c_str(), cbRoot);
|
---|
1055 |
|
---|
1056 | *ppszbData = (char *)pvData;
|
---|
1057 | *pcbData = cbRoot;
|
---|
1058 | }
|
---|
1059 | else
|
---|
1060 | rc = VERR_NO_MEMORY;
|
---|
1061 | }
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | LogFlowFunc(("Building CF_HDROP list rc=%Rrc, pszFiles=0x%p, cFiles=%RU16, cchFiles=%RU32\n",
|
---|
1065 | rc, pszFiles, cFiles, cchFiles));
|
---|
1066 |
|
---|
1067 | if (pszFiles)
|
---|
1068 | RTStrFree(pszFiles);
|
---|
1069 |
|
---|
1070 | return rc;
|
---|
1071 | }
|
---|
1072 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_URI_LIST */
|
---|
1073 |
|
---|