vrvirtualdesktop 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/Win32/Base.rb ADDED
@@ -0,0 +1,275 @@
1
+ ##################################################################################################
2
+ #
3
+ # $Author: Steve $
4
+ # $Date: 2005/12/16 18:35:42 $
5
+ # $Version: 0.99$
6
+ # $Change: $
7
+ #
8
+ ##################################################################################################
9
+ require "rubygems"
10
+ require_gem "SimpleTrace", "< 0.1.0"
11
+
12
+ #-------------------------------------------------------------------------------------------------
13
+ # This module contains some very common Win32 methods like LastError, language macros, etc
14
+ module Win32
15
+
16
+ require "Win32/APIMapper"
17
+ require "Win32/Encoding"
18
+ require "Win32/Base-consts.rb"
19
+
20
+ #---------------------------------------------------------------------------------------------
21
+ #---------------------------------------------------------------------------------------------
22
+ # Win32 Exceptions
23
+ class Error < StandardError
24
+ #-----------------------------------------------------------------------------------------
25
+ # class methodsFor: "initialization"
26
+ def initialize(p_nError = nil)
27
+ $TRACE.debug 9, "p_nError = '#{p_nError}'"
28
+ @err = LastErrorInfo.new()
29
+ if p_nError.kind_of?(String) then
30
+ @err_string = p_nError
31
+ $TRACE.debug 9, "@err_string = '#{@err_string}'"
32
+ else
33
+ @err.set(p_nError) if !p_nError.nil?
34
+ end
35
+ end
36
+
37
+ #-----------------------------------------------------------------------------------------
38
+ # methodsFor: "accessing"
39
+ def errno
40
+ return @err.to_i
41
+ end
42
+
43
+ #-----------------------------------------------------------------------------------------
44
+ # methodsFor: "printing"
45
+ def to_s
46
+ $TRACE.debug 9, "@err_string = '#{@err_string}'"
47
+ if @err_string then
48
+ return @err_string + @err.to_s
49
+ else
50
+ return @err.to_s
51
+ end
52
+ end
53
+
54
+ def message
55
+ self.to_s
56
+ end
57
+ end
58
+
59
+ #---------------------------------------------------------------------------------------------
60
+ #---------------------------------------------------------------------------------------------
61
+ # Win32 MessageBoxes
62
+ include APIMapper
63
+
64
+ APIMapper.Initialize(
65
+ 'MessageBox' => { DLL => 'User32', NAME => 'MessageBoxW',
66
+ IN => ['L', 'P', 'P', 'L'], OUT => 'L'}
67
+ )
68
+
69
+ MB_OK = 0x00000000
70
+ MB_OKCANCEL = 0x00000001
71
+ MB_ABORTRETRYIGNORE = 0x00000002
72
+ MB_YESNOCANCEL = 0x00000003
73
+ MB_YESNO = 0x00000004
74
+ MB_RETRYCANCEL = 0x00000005
75
+
76
+ MB_ICONHAND = 0x00000010
77
+ MB_ICONQUESTION = 0x00000020
78
+ MB_ICONEXCLAMATION = 0x00000030
79
+ MB_ICONASTERISK = 0x00000040
80
+ MB_USERICON = 0x00000080
81
+ MB_ICONWARNING = MB_ICONEXCLAMATION
82
+ MB_ICONERROR = MB_ICONHAND
83
+ MB_ICONINFORMATION = MB_ICONASTERISK
84
+ MB_ICONSTOP = MB_ICONHAND
85
+
86
+ def Win32.MessageBox(p_strTitle, p_strMessage, p_nIcon = 0)
87
+ APIMapper.WCall("MessageBox",
88
+ 0,
89
+ p_strMessage.to_ucs2(true), p_strTitle.to_ucs2(true),
90
+ p_nIcon)
91
+ end
92
+
93
+ #---------------------------------------------------------------------------------------------
94
+ #---------------------------------------------------------------------------------------------
95
+ # This class encodes and decodes Win32 Language IDs
96
+ class LangID
97
+
98
+ #-----------------------------------------------------------------------------------------
99
+ # constsFor: "Languages IDs"
100
+ # (from WinBase.h)
101
+ LANG_NEUTRAL = 0x00
102
+
103
+ SUBLANG_NEUTRAL = 0x00 # language neutral
104
+ SUBLANG_DEFAULT = 0x01 # user default
105
+ SUBLANG_SYS_DEFAULT = 0x02 # system default
106
+
107
+ #-----------------------------------------------------------------------------------------
108
+ # class methodsFor: "initialization"
109
+ def initialize(p_primaryLang = LANG_NEUTRAL, p_subLang = SUBLANG_NEUTRAL)
110
+ @nLangID = (p_subLang << 10) | p_primaryLang
111
+ end
112
+
113
+ #-----------------------------------------------------------------------------------------
114
+ # methodsFor: "accessing"
115
+ def to_s
116
+ return @nLangID.to_s
117
+ end
118
+
119
+ def to_i
120
+ return @nLangID
121
+ end
122
+
123
+ def primary
124
+ return @nLangID & 0x3FF
125
+ end
126
+
127
+ def sub
128
+ return @nLangID >> 10
129
+ end
130
+ end
131
+
132
+ #---------------------------------------------------------------------------------------------
133
+ #---------------------------------------------------------------------------------------------
134
+ # This class is used to collect Win32 Last Error
135
+
136
+ class LastErrorInfo
137
+
138
+ #-----------------------------------------------------------------------------------------
139
+ include APIMapper
140
+
141
+ APIMapper.Initialize(
142
+ 'GetLastError' => { DLL => 'kernel32', NAME => 'GetLastError',
143
+ IN => [], OUT =>'L'},
144
+ 'SetLastError' => { DLL => 'kernel32', NAME => 'SetLastError',
145
+ IN => ['L'], OUT => 'V'},
146
+ 'FormatMessage' => { DLL => 'kernel32', NAME => 'FormatMessageW',
147
+ IN => ['L', 'P', 'L', 'L', 'P', 'L', 'P'], OUT => 'L'}
148
+ )
149
+
150
+ #-----------------------------------------------------------------------------------------
151
+ # constsFor: "Message Formats"
152
+ # (from WinBase.h)
153
+ FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100
154
+ FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
155
+ FORMAT_MESSAGE_FROM_STRING = 0x00000400
156
+ FORMAT_MESSAGE_FROM_HMODULE = 0x00000800
157
+ FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
158
+ FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000
159
+ FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF
160
+
161
+ #-----------------------------------------------------------------------------------------
162
+ # class methodsFor: "initialization"
163
+ def initialize(p_nErrorCode = nil)
164
+ @err = p_nErrorCode
165
+ @err.nil? ? get() : set(p_nErrorCode)
166
+ end
167
+
168
+ #-----------------------------------------------------------------------------------------
169
+ # methodsFor: "accessing"
170
+ def to_i
171
+ return @err
172
+ end
173
+
174
+ def get(p_bSync = true)
175
+ return p_bSync ? (@err = WCall('GetLastError')) : @err
176
+ end
177
+
178
+ def set(p_err, p_bSync = true)
179
+ @err = p_err
180
+ WCall('SetLastError', p_err) if p_bSync
181
+ end
182
+
183
+ def text
184
+ pBuffer = " " * 1024 # buffer
185
+ ret = WCall('FormatMessage',
186
+ FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
187
+ 0, @err, LangID.new.to_i, pBuffer, 1024, 0)
188
+ return pBuffer[0, (ret - 2) * 2].to_utf8 # we don't need the \r\n at the end
189
+ end
190
+
191
+ #-----------------------------------------------------------------------------------------
192
+ # methodsFor: "printing"
193
+ def to_s
194
+ get() if @err.nil?
195
+ return "Win32 Error: #{@err}, " + text()
196
+ end
197
+ end
198
+
199
+ #---------------------------------------------------------------------------------------------
200
+ #---------------------------------------------------------------------------------------------
201
+ #TODO: The SecurityAttributes class is not finished!!!
202
+ class SecurityAttributes
203
+
204
+ #-----------------------------------------------------------------------------------------
205
+ # constsFor: "Message Formats"
206
+ # (from WinNT.h)
207
+ SE_ASSIGNPRIMARYTOKEN_NAME = "SeAssignPrimaryTokenPrivilege"
208
+ SE_AUDIT_NAME = "SeAuditPrivilege"
209
+ SE_BACKUP_NAME = "SeBackupPrivilege"
210
+ SE_CHANGE_NOTIFY_NAME = "SeChangeNotifyPrivilege"
211
+ SE_CREATE_PAGEFILE_NAME = "SeCreatePagefilePrivilege"
212
+ SE_CREATE_PERMANENT_NAME = "SeCreatePermanentPrivilege"
213
+ SE_CREATE_TOKEN_NAME = "SeCreateTokenPrivilege"
214
+ SE_DEBUG_NAME = "SeDebugPrivilege"
215
+ SE_INCREASE_QUOTA_NAME = "SeIncreaseQuotaPrivilege"
216
+ SE_INC_BASE_PRIORITY_NAME = "SeIncreaseBasePriorityPrivilege"
217
+ SE_LOAD_DRIVER_NAME = "SeLoadDriverPrivilege"
218
+ SE_LOCK_MEMORY_NAME = "SeLockMemoryPrivilege"
219
+ SE_MACHINE_ACCOUNT_NAME = "SeMachineAccountPrivilege"
220
+ SE_PROF_SINGLE_PROCESS_NAME = "SeProfileSingleProcessPrivilege"
221
+ SE_REMOTE_SHUTDOWN_NAME = "SeRemoteShutdownPrivilege"
222
+ SE_RESTORE_NAME = "SeRestorePrivilege"
223
+ SE_SECURITY_NAME = "SeSecurityPrivilege"
224
+ SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
225
+ SE_SYSTEMTIME_NAME = "SeSystemtimePrivilege"
226
+ SE_SYSTEM_ENVIRONMENT_NAME = "SeSystemEnvironmentPrivilege"
227
+ SE_SYSTEM_PROFILE_NAME = "SeSystemProfilePrivilege"
228
+ SE_TAKE_OWNERSHIP_NAME = "SeTakeOwnershipPrivilege"
229
+ SE_TCB_NAME = "SeTcbPrivilege"
230
+ SE_UNSOLICITED_INPUT_NAME = "SeUnsolicitedInputPrivilege"
231
+
232
+ #-----------------------------------------------------------------------------------------
233
+ # class methodsFor: "Data sizing"
234
+ def SecurityAttributes.sizeof
235
+ return 12 # 1 DWORD, 1 void*, 1 BOOL
236
+ end
237
+
238
+ #-----------------------------------------------------------------------------------------
239
+ # class methodsFor: "initialization"
240
+ def initialize(p_bInheritHandle = true)
241
+ @bInheritHandle = p_bInheritHandle
242
+ end
243
+
244
+ #-----------------------------------------------------------------------------------------
245
+ # methodsFor: "packing"
246
+ def pack
247
+ [SecurityAttributes.sizeof, 0, @bInheritHandle].pack('L3')
248
+ end
249
+ end
250
+
251
+ end
252
+ # Values are 32 bit values layed out as follows:
253
+ #
254
+ # 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
255
+ # 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
256
+ # +---+-+-+-----------------------+-------------------------------+
257
+ # |Sev|C|R| Facility | Code |
258
+ # +---+-+-+-----------------------+-------------------------------+
259
+ #
260
+ # where
261
+ #
262
+ # Sev - is the severity code
263
+ #
264
+ # 00 - Success
265
+ # 01 - Informational
266
+ # 10 - Warning
267
+ # 11 - Error
268
+ #
269
+ # C - is the Customer code flag
270
+ #
271
+ # R - is a reserved bit
272
+ #
273
+ # Facility - is the facility code
274
+ #
275
+ # Code - is the facility's status code
@@ -0,0 +1,68 @@
1
+ ##################################################################################################
2
+ #
3
+ # $Author: Steve $
4
+ # $Date: 2005/12/16 18:35:42 $
5
+ # $Change: $
6
+ #
7
+ ##################################################################################################
8
+
9
+ #-------------------------------------------------------------------------------------------------
10
+ # This mixin brings utf8 and ucs2 encoding to the class String
11
+ module Encoding
12
+ #---------------------------------------------------------------------------------------------
13
+ # attributesFor: "String Encoding"
14
+ attr_writer :encoding
15
+ attr_reader :encoding
16
+
17
+ #---------------------------------------------------------------------------------------------
18
+ # constsFor: "String Encoding IDs"
19
+ ANSI = 'ANSI'
20
+ UTF8 = 'UTF8'
21
+ UCS2 = 'UCS2'
22
+
23
+ #---------------------------------------------------------------------------------------------
24
+ # class methodsFor: "initialization"
25
+ def initialize(p_encoding = ANSI)
26
+ @encoding = p_encoding
27
+ end
28
+
29
+ #---------------------------------------------------------------------------------------------
30
+ # methodsFor: "encoding"
31
+ def to_ucs2(p_bZeroTerminate = false)
32
+ return self if defined? @encoding and UCS2 == @encoding
33
+ array_enc = self.unpack('U*')
34
+ array_ucs2 = []
35
+ array_enc.each do |byte|
36
+ array_ucs2 << (byte & 0xFF) << (byte >> 8)
37
+ end
38
+ strRet = array_ucs2.pack('C*')
39
+ strRet += "\000\000" if p_bZeroTerminate
40
+ strRet.encoding = UCS2
41
+ return strRet
42
+ end
43
+
44
+ def to_utf8(p_bZeroTerminate = false)
45
+ return self if defined? @encoding and (UTF8 == @encoding or ANSI == @encoding)
46
+ array_enc = self.unpack('C*')
47
+ array_utf8 = []
48
+ 0.step(array_enc.size-1, 2) do |i|
49
+ array_utf8 << (array_enc.at(i) + array_enc.at(i+1) * 0x100)
50
+ end
51
+ strRet = array_utf8.pack('U*')
52
+ strRet += "\000" if p_bZeroTerminate
53
+ strRet.encoding = UTF8
54
+ return strRet
55
+ end
56
+
57
+ def to_hex
58
+ str = ''
59
+ self.unpack('C*').each do |byte|
60
+ str += " %02X" % byte
61
+ end
62
+ return str[1 .. -1]
63
+ end
64
+ end
65
+
66
+ class String
67
+ include Encoding
68
+ end
@@ -0,0 +1,107 @@
1
+ require "Win32API"
2
+
3
+ $GetSystemMetrics = Win32API.new("user32","GetSystemMetrics","I","I")
4
+
5
+ #/*
6
+ # * GetSystemMetrics() codes
7
+ #*/
8
+
9
+ SM_CXSCREEN = 0
10
+ SM_CYSCREEN = 1
11
+ SM_CXVSCROLL = 2
12
+ SM_CYHSCROLL = 3
13
+ SM_CYCAPTION = 4
14
+ SM_CXBORDER = 5
15
+ SM_CYBORDER = 6
16
+ SM_CXDLGFRAME = 7
17
+ SM_CYDLGFRAME = 8
18
+ SM_CYVTHUMB = 9
19
+ SM_CXHTHUMB = 10
20
+ SM_CXICON = 11
21
+ SM_CYICON = 12
22
+ SM_CXCURSOR = 13
23
+ SM_CYCURSOR = 14
24
+ SM_CYMENU = 15
25
+ SM_CXFULLSCREEN = 16
26
+ SM_CYFULLSCREEN = 17
27
+ SM_CYKANJIWINDOW = 18
28
+ SM_MOUSEPRESENT = 19
29
+ SM_CYVSCROLL = 20
30
+ SM_CXHSCROLL = 21
31
+ SM_DEBUG = 22
32
+ SM_SWAPBUTTON = 23
33
+ SM_RESERVED1 = 24
34
+ SM_RESERVED2 = 25
35
+ SM_RESERVED3 = 26
36
+ SM_RESERVED4 = 27
37
+ SM_CXMIN = 28
38
+ SM_CYMIN = 29
39
+ SM_CXSIZE = 30
40
+ SM_CYSIZE = 31
41
+ SM_CXFRAME = 32
42
+ SM_CYFRAME = 33
43
+ SM_CXMINTRACK = 34
44
+ SM_CYMINTRACK = 35
45
+ SM_CXDOUBLECLK = 36
46
+ SM_CYDOUBLECLK = 37
47
+ SM_CXICONSPACING = 38
48
+ SM_CYICONSPACING = 39
49
+ SM_MENUDROPALIGNMENT = 40
50
+ SM_PENWINDOWS = 41
51
+ SM_DBCSENABLED = 42
52
+ SM_CMOUSEBUTTONS = 43
53
+
54
+ #if(WINVER >= 0x0400)
55
+ SM_CXFIXEDFRAME = SM_CXDLGFRAME #/* ;win40 name change */
56
+ SM_CYFIXEDFRAME = SM_CYDLGFRAME #/* ;win40 name change */
57
+ SM_CXSIZEFRAME = SM_CXFRAME #/* ;win40 name change */
58
+ SM_CYSIZEFRAME = SM_CYFRAME #/* ;win40 name change */
59
+
60
+ SM_SECURE = 44
61
+ SM_CXEDGE = 45
62
+ SM_CYEDGE = 46
63
+ SM_CXMINSPACING = 47
64
+ SM_CYMINSPACING = 48
65
+ SM_CXSMICON = 49
66
+ SM_CYSMICON = 50
67
+ SM_CYSMCAPTION = 51
68
+ SM_CXSMSIZE = 52
69
+ SM_CYSMSIZE = 53
70
+ SM_CXMENUSIZE = 54
71
+ SM_CYMENUSIZE = 55
72
+ SM_ARRANGE = 56
73
+ SM_CXMINIMIZED = 57
74
+ SM_CYMINIMIZED = 58
75
+ SM_CXMAXTRACK = 59
76
+ SM_CYMAXTRACK = 60
77
+ SM_CXMAXIMIZED = 61
78
+ SM_CYMAXIMIZED = 62
79
+ SM_NETWORK = 63
80
+ SM_CLEANBOOT = 67
81
+ SM_CXDRAG = 68
82
+ SM_CYDRAG = 69
83
+ #endif /* WINVER >= 0x0400 */
84
+ SM_SHOWSOUNDS = 70
85
+ #if(WINVER >= 0x0400)
86
+ SM_CXMENUCHECK = 71 #/* Use instead of GetMenuCheckMarkDimensions()! */
87
+ SM_CYMENUCHECK = 72
88
+ SM_SLOWMACHINE = 73
89
+ SM_MIDEASTENABLED = 74
90
+ #endif /* WINVER >= 0x0400 */
91
+ #if (WINVER >= 0x0500) || (_WIN32_WINNT >= 0x0400)
92
+ SM_MOUSEWHEELPRESENT = 75
93
+ #endif
94
+ #if(WINVER >= 0x0500)
95
+ SM_XVIRTUALSCREEN = 76
96
+ SM_YVIRTUALSCREEN = 77
97
+ SM_CXVIRTUALSCREEN = 78
98
+ SM_CYVIRTUALSCREEN = 79
99
+ SM_CMONITORS = 80
100
+ SM_SAMEDISPLAYFORMAT = 81
101
+ #endif /* WINVER >= 0x0500 */
102
+
103
+ #if (WINVER < 0x0500) && (!defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0400))
104
+ SM_CMETRICS = 76
105
+ #else
106
+ #SM_CMETRICS = 83
107
+ #endif