-
Notifications
You must be signed in to change notification settings - Fork 5
/
Clipboard.vb
123 lines (86 loc) · 2.93 KB
/
Clipboard.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'
' V.2
'
option explicit
' { WinAPI declarations
declare function GlobalAlloc lib "kernel32" ( _
byVal wFlags as long, _
byVal dwBytes as long) as long
declare function lstrcpy lib "kernel32" ( _
byVal lpString1 as any, _
byVal lpString2 as any) as long
declare function EmptyClipboard lib "User32" () as long
declare function CloseClipboard lib "User32" () as long
declare function OpenClipboard lib "User32" ( _
byVal hwnd as long ) as long
declare function GlobalLock lib "kernel32" ( _
byVal hMem as long ) as long
declare function GlobalUnlock lib "kernel32" ( _
byVal hMem as long ) as long
declare function SetClipboardData lib "User32" ( _
byVal wFormat as long, _
byVal hMem as long ) as long
declare function GetClipboardData lib "User32" ( _
byVal wFormat as long ) as long
private const GHND = &h42
private const CF_TEXT = 1
' }
sub textToClipboard(txt as string) ' {
dim memory as long
dim lockedMemory as long
memory = GlobalAlloc(GHND, len(txt) + 1)
if memory = 0 then
msgBox "GlobalAlloc failed"
exit sub
end if
lockedMemory = GlobalLock(memory)
if lockedMemory = 0 then
msgBox "GlobalLock failed"
exit sub
end if
lockedMemory = lstrcpy(lockedMemory, txt)
call GlobalUnlock(memory)
if openClipboard(0) = 0 Then
msgBox "openClipboard failed"
exit sub
end if
EmptyClipboard
call SetClipboardData(CF_TEXT, memory)
if CloseClipboard() = 0 then
msgBox "CloseClipboard failed"
end if
end sub ' }
function clipboardToText() as string ' {
dim h as long
dim lockedMemory as long
if OpenClipboard(0) = 0 then
msgBox "Could not open Clipboard"
exit function
end if
h = GetClipboardData(CF_TEXT)
if h = 0 then
msgBox "GetClipboardData could not allocate memory"
exit function
end if
lockedMemory = GlobalLock(h)
if lockedMemory = 0 then
msgBox "GlobalLock failed"
exit function
end if
dim str as string
' Hopefully, 1 MB is enough...
' ... otherwise, the lstrcpy below will fail miserably!
str = space$(1# * 1024 * 1024)
lstrcpy str, lockedMemory
GlobalUnlock(lockedMemory)
'
' Remove 'text' after null-byte:
'
dim pos as long
pos = inStr(1, str , Chr$(0), 0)
str = mid(str, 1, pos-1)
if CloseClipboard() = 0 then
msgBox "CloseClipboard failed"
end if
clipboardToText = str
end function ' }