OLD | NEW |
| (Empty) |
1 // Copyright 2004-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #include "omaha/base/clipboard.h" | |
17 #include "base/basictypes.h" | |
18 #include "omaha/base/debug.h" | |
19 | |
20 namespace omaha { | |
21 | |
22 // Put the given string on the system clipboard | |
23 void SetClipboard(const TCHAR *string_to_set) { | |
24 ASSERT(string_to_set, (L"")); | |
25 | |
26 int len = lstrlen(string_to_set); | |
27 | |
28 // | |
29 // Note to developer: It is not always possible to step through this code | |
30 // since the debugger will possibly steal the clipboard. E.g. OpenClipboard | |
31 // might succeed and EmptyClipboard might fail with "Thread does not have | |
32 // clipboard open". | |
33 // | |
34 | |
35 // Actual clipboard processing | |
36 if (::OpenClipboard(NULL)) { | |
37 BOOL b = ::EmptyClipboard(); | |
38 ASSERT(b, (L"EmptyClipboard failed")); | |
39 | |
40 // Include the terminating null | |
41 len++; | |
42 | |
43 HANDLE copy_handle = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, | |
44 len * sizeof(TCHAR)); | |
45 ASSERT(copy_handle, (L"")); | |
46 | |
47 byte* copy_data = reinterpret_cast<byte*>(::GlobalLock(copy_handle)); | |
48 memcpy(copy_data, string_to_set, len * sizeof(TCHAR)); | |
49 ::GlobalUnlock(copy_handle); | |
50 | |
51 #ifdef _UNICODE | |
52 HANDLE h = ::SetClipboardData(CF_UNICODETEXT, copy_handle); | |
53 #else | |
54 HANDLE h = ::SetClipboardData(CF_TEXT, copy_handle); | |
55 #endif | |
56 | |
57 ASSERT(h != NULL, (L"SetClipboardData failed")); | |
58 if (!h) { | |
59 ::GlobalFree(copy_handle); | |
60 } | |
61 | |
62 VERIFY(::CloseClipboard(), (L"")); | |
63 } else { | |
64 ASSERT(false, (L"OpenClipboard failed - %i", ::GetLastError())); | |
65 } | |
66 } | |
67 | |
68 } // namespace omaha | |
69 | |
OLD | NEW |