windows-api 0.4.3 → 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES +68 -64
- data/README +3 -3
- data/Rakefile +16 -0
- data/lib/windows/api.rb +503 -504
- data/lib/windows/wide_string.rb +59 -59
- data/test/test_windows_api.rb +171 -171
- data/windows-api.gemspec +1 -1
- metadata +3 -3
data/lib/windows/wide_string.rb
CHANGED
@@ -1,60 +1,60 @@
|
|
1
|
-
require 'windows/unicode'
|
2
|
-
require 'windows/msvcrt/string'
|
3
|
-
|
4
|
-
# This is a class that simplifies wide string handling. It is NOT meant
|
5
|
-
# for general consumption, but for internal use by the Win32Utils Team.
|
6
|
-
# Use at your own risk.
|
7
|
-
#
|
8
|
-
class WideString < String
|
9
|
-
include Windows::Unicode
|
10
|
-
include Windows::MSVCRT::String
|
11
|
-
|
12
|
-
ACP = CP_ACP
|
13
|
-
UTF7 = CP_UTF7
|
14
|
-
UTF8 = CP_UTF8
|
15
|
-
|
16
|
-
# Get or set the encoding of the wide string object
|
17
|
-
#
|
18
|
-
attr_accessor :encoding
|
19
|
-
|
20
|
-
# Creates a new wide +string+ with the given +encoding+, or UTF8 if
|
21
|
-
# no encoding is specified.
|
22
|
-
#
|
23
|
-
def initialize(string, encoding = UTF8)
|
24
|
-
super(multi_to_wide(string, encoding))
|
25
|
-
@encoding = encoding
|
26
|
-
end
|
27
|
-
|
28
|
-
# Returns the multibyte version of the wide string.
|
29
|
-
#
|
30
|
-
def to_multi
|
31
|
-
wide_to_multi(self, @encoding)
|
32
|
-
end
|
33
|
-
|
34
|
-
# Replaces the wide string with a multibyte version.
|
35
|
-
#
|
36
|
-
def to_multi!
|
37
|
-
self.replace(wide_to_multi(self, @encoding))
|
38
|
-
end
|
39
|
-
|
40
|
-
alias to_s to_multi
|
41
|
-
alias to_str to_multi
|
42
|
-
alias inspect to_multi
|
43
|
-
|
44
|
-
# Strips the trailing two null characters from the string.
|
45
|
-
#
|
46
|
-
def wstrip
|
47
|
-
self[0..-3] if string[-2..-1] == "\000\000"
|
48
|
-
end
|
49
|
-
|
50
|
-
# The length of the wide string in chars.
|
51
|
-
#
|
52
|
-
def length
|
53
|
-
wcslen(self) * 2
|
54
|
-
end
|
55
|
-
|
56
|
-
# The size of the wide string in bytes.
|
57
|
-
def size
|
58
|
-
wcslen(self)
|
59
|
-
end
|
1
|
+
require 'windows/unicode'
|
2
|
+
require 'windows/msvcrt/string'
|
3
|
+
|
4
|
+
# This is a class that simplifies wide string handling. It is NOT meant
|
5
|
+
# for general consumption, but for internal use by the Win32Utils Team.
|
6
|
+
# Use at your own risk.
|
7
|
+
#
|
8
|
+
class WideString < String
|
9
|
+
include Windows::Unicode
|
10
|
+
include Windows::MSVCRT::String
|
11
|
+
|
12
|
+
ACP = CP_ACP
|
13
|
+
UTF7 = CP_UTF7
|
14
|
+
UTF8 = CP_UTF8
|
15
|
+
|
16
|
+
# Get or set the encoding of the wide string object
|
17
|
+
#
|
18
|
+
attr_accessor :encoding
|
19
|
+
|
20
|
+
# Creates a new wide +string+ with the given +encoding+, or UTF8 if
|
21
|
+
# no encoding is specified.
|
22
|
+
#
|
23
|
+
def initialize(string, encoding = UTF8)
|
24
|
+
super(multi_to_wide(string, encoding))
|
25
|
+
@encoding = encoding
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns the multibyte version of the wide string.
|
29
|
+
#
|
30
|
+
def to_multi
|
31
|
+
wide_to_multi(self, @encoding)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Replaces the wide string with a multibyte version.
|
35
|
+
#
|
36
|
+
def to_multi!
|
37
|
+
self.replace(wide_to_multi(self, @encoding))
|
38
|
+
end
|
39
|
+
|
40
|
+
alias to_s to_multi
|
41
|
+
alias to_str to_multi
|
42
|
+
alias inspect to_multi
|
43
|
+
|
44
|
+
# Strips the trailing two null characters from the string.
|
45
|
+
#
|
46
|
+
def wstrip
|
47
|
+
self[0..-3] if string[-2..-1] == "\000\000"
|
48
|
+
end
|
49
|
+
|
50
|
+
# The length of the wide string in chars.
|
51
|
+
#
|
52
|
+
def length
|
53
|
+
wcslen(self) * 2
|
54
|
+
end
|
55
|
+
|
56
|
+
# The size of the wide string in bytes.
|
57
|
+
def size
|
58
|
+
wcslen(self)
|
59
|
+
end
|
60
60
|
end
|
data/test/test_windows_api.rb
CHANGED
@@ -1,171 +1,171 @@
|
|
1
|
-
############################################################################
|
2
|
-
# test_windows_api.rb
|
3
|
-
#
|
4
|
-
# Test case for the Windows::API class. You should run this as Rake task,
|
5
|
-
# i.e. 'rake test', instead of running it directly.
|
6
|
-
############################################################################
|
7
|
-
require 'windows/api'
|
8
|
-
require 'test-unit'
|
9
|
-
include Windows
|
10
|
-
|
11
|
-
module Windows
|
12
|
-
module Test
|
13
|
-
API.auto_namespace = 'Windows::Test'
|
14
|
-
API.auto_unicode = true
|
15
|
-
API.auto_method = true
|
16
|
-
API.auto_constant = true
|
17
|
-
$test_method = API.new('GetCurrentDirectory', 'PP', 'L')
|
18
|
-
end
|
19
|
-
|
20
|
-
module Foo
|
21
|
-
API.auto_namespace = 'Windows::Foo'
|
22
|
-
API.auto_unicode = false
|
23
|
-
API.auto_method = false
|
24
|
-
API.auto_constant = false
|
25
|
-
$foo_method = API.new('GetSystemDirectory', 'PL', 'L')
|
26
|
-
end
|
27
|
-
|
28
|
-
module Bar
|
29
|
-
API.auto_namespace = 'Windows::Bar'
|
30
|
-
API.auto_constant = true
|
31
|
-
API.auto_method = true
|
32
|
-
$bar_method = API.new('GetUserName', 'PP', 'I', 'advapi32')
|
33
|
-
end
|
34
|
-
|
35
|
-
module Baz
|
36
|
-
API.auto_namespace = 'Windows::Baz'
|
37
|
-
API.auto_constant = true
|
38
|
-
API.auto_method = true
|
39
|
-
|
40
|
-
# No ANSI equivalent for ReadDirectoryChangesW
|
41
|
-
$strstr = API.new('strstr', 'PP', 'P', 'msvcrt')
|
42
|
-
$umask = API.new('_umask', 'I', 'I', 'msvcrt')
|
43
|
-
$wave = API.new('waveOutGetNumDevs', 'V', 'I', 'winmm')
|
44
|
-
$read = API.new('ReadDirectoryChangesW', 'LPLILPPP', 'B')
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
class TC_Windows_API < Test::Unit::TestCase
|
49
|
-
include Windows::Test
|
50
|
-
include Windows::Foo
|
51
|
-
include Windows::Bar
|
52
|
-
include Windows::Baz
|
53
|
-
|
54
|
-
def setup
|
55
|
-
@buf = 0.chr * 256
|
56
|
-
@runtimes = ['msvcrt', 'msvcr80', 'msvcr90', 'msvcr100']
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_version
|
60
|
-
assert_equal('0.4.
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_full_data_types
|
64
|
-
assert_nothing_raised{
|
65
|
-
API.new('GetWindowsDirectory', ['LPTSTR', 'UINT'], 'BOOL')
|
66
|
-
}
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_msvcrt_constant
|
70
|
-
assert_true(@runtimes.include?(Windows::MSVCRT_DLL))
|
71
|
-
end
|
72
|
-
|
73
|
-
# Validate that funcs like 'strstr' get an uppercase constant like 'Strstr'
|
74
|
-
def test_lower_case_to_capitalized_constant
|
75
|
-
assert_not_nil(Windows::Baz::Strstr)
|
76
|
-
assert_not_nil(Windows::Baz::Umask)
|
77
|
-
assert_not_nil(Windows::Baz::WaveOutGetNumDevs)
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_explicit_wide_function_only
|
81
|
-
assert_not_nil(Windows::Baz::ReadDirectoryChangesW)
|
82
|
-
assert_false(Windows::Baz.constants.include?('ReadDirectoryChanges'))
|
83
|
-
assert_false(Windows::Baz.constants.include?('ReadDirectoryChangesA'))
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_lower_case_auto_methods
|
87
|
-
assert_respond_to(self, :strstr)
|
88
|
-
assert_respond_to(self, :umask)
|
89
|
-
assert_respond_to(self, :_umask)
|
90
|
-
assert_respond_to(self, :waveOutGetNumDevs)
|
91
|
-
assert_equal('llo', strstr('hello', 'l'))
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_auto_unicode
|
95
|
-
assert_not_nil(Windows::Bar::GetUserName)
|
96
|
-
assert_respond_to(self, :GetUserName)
|
97
|
-
assert_not_respond_to(self, :GetUserNameA)
|
98
|
-
assert_not_respond_to(self, :GetUserNameW)
|
99
|
-
end
|
100
|
-
|
101
|
-
def test_auto_constant
|
102
|
-
assert_not_nil(Windows::Test::GetCurrentDirectory)
|
103
|
-
assert_not_nil(Windows::Bar::GetUserName)
|
104
|
-
assert_kind_of(Win32::API, Windows::Test::GetCurrentDirectory)
|
105
|
-
assert_respond_to(Windows::Test::GetCurrentDirectory, :call)
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_auto_method
|
109
|
-
assert_respond_to(self, :GetCurrentDirectory)
|
110
|
-
assert_respond_to(self, :GetCurrentDirectoryA)
|
111
|
-
assert_respond_to(self, :GetCurrentDirectoryW)
|
112
|
-
assert_not_respond_to(self, :GetSystemDirectory)
|
113
|
-
assert_not_respond_to(self, :GetSystemDirectoryA)
|
114
|
-
assert_not_respond_to(self, :GetSystemDirectoryW)
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_call
|
118
|
-
assert_respond_to($test_method, :call)
|
119
|
-
assert_respond_to($foo_method, :call)
|
120
|
-
assert_nothing_raised{ $test_method.call(@buf.length, @buf) }
|
121
|
-
assert_nothing_raised{ $foo_method.call(@buf, @buf.length) }
|
122
|
-
end
|
123
|
-
|
124
|
-
def test_dll_name
|
125
|
-
assert_respond_to($test_method, :dll_name)
|
126
|
-
assert_equal('kernel32', $test_method.dll_name)
|
127
|
-
end
|
128
|
-
|
129
|
-
def test_function_name
|
130
|
-
assert_respond_to($test_method, :function_name)
|
131
|
-
assert_equal('GetCurrentDirectory', $test_method.function_name)
|
132
|
-
end
|
133
|
-
|
134
|
-
def test_prototype
|
135
|
-
assert_respond_to($test_method, :prototype)
|
136
|
-
assert_equal(['P', 'P'], $test_method.prototype)
|
137
|
-
end
|
138
|
-
|
139
|
-
def test_return_type
|
140
|
-
assert_respond_to($test_method, :return_type)
|
141
|
-
assert_equal('L', $test_method.return_type)
|
142
|
-
end
|
143
|
-
|
144
|
-
def test_effective_function_name
|
145
|
-
assert_respond_to($test_method, :effective_function_name)
|
146
|
-
assert_equal('GetCurrentDirectoryA', $test_method.effective_function_name)
|
147
|
-
assert_equal('strstr', $strstr.effective_function_name)
|
148
|
-
assert_equal('waveOutGetNumDevs', $wave.effective_function_name)
|
149
|
-
assert_equal('ReadDirectoryChangesW', $read.effective_function_name)
|
150
|
-
end
|
151
|
-
|
152
|
-
def test_bad_prototype_raises_error
|
153
|
-
assert_raise(Win32::API::PrototypeError){
|
154
|
-
Windows::API.new('GetCurrentDirectory', 'XL', 'L')
|
155
|
-
}
|
156
|
-
assert_raise(Win32::API::PrototypeError){
|
157
|
-
Windows::API.new('GetCurrentDirectory', 'PL', 'X')
|
158
|
-
}
|
159
|
-
end
|
160
|
-
|
161
|
-
def test_bad_function_raises_error
|
162
|
-
assert_raise(Win32::API::LoadLibraryError){
|
163
|
-
Windows::API.new('GetCurrentFooBar', 'LL', 'L')
|
164
|
-
}
|
165
|
-
end
|
166
|
-
|
167
|
-
def teardown
|
168
|
-
@buf = nil
|
169
|
-
@runtimes = nil
|
170
|
-
end
|
171
|
-
end
|
1
|
+
############################################################################
|
2
|
+
# test_windows_api.rb
|
3
|
+
#
|
4
|
+
# Test case for the Windows::API class. You should run this as Rake task,
|
5
|
+
# i.e. 'rake test', instead of running it directly.
|
6
|
+
############################################################################
|
7
|
+
require 'windows/api'
|
8
|
+
require 'test-unit'
|
9
|
+
include Windows
|
10
|
+
|
11
|
+
module Windows
|
12
|
+
module Test
|
13
|
+
API.auto_namespace = 'Windows::Test'
|
14
|
+
API.auto_unicode = true
|
15
|
+
API.auto_method = true
|
16
|
+
API.auto_constant = true
|
17
|
+
$test_method = API.new('GetCurrentDirectory', 'PP', 'L')
|
18
|
+
end
|
19
|
+
|
20
|
+
module Foo
|
21
|
+
API.auto_namespace = 'Windows::Foo'
|
22
|
+
API.auto_unicode = false
|
23
|
+
API.auto_method = false
|
24
|
+
API.auto_constant = false
|
25
|
+
$foo_method = API.new('GetSystemDirectory', 'PL', 'L')
|
26
|
+
end
|
27
|
+
|
28
|
+
module Bar
|
29
|
+
API.auto_namespace = 'Windows::Bar'
|
30
|
+
API.auto_constant = true
|
31
|
+
API.auto_method = true
|
32
|
+
$bar_method = API.new('GetUserName', 'PP', 'I', 'advapi32')
|
33
|
+
end
|
34
|
+
|
35
|
+
module Baz
|
36
|
+
API.auto_namespace = 'Windows::Baz'
|
37
|
+
API.auto_constant = true
|
38
|
+
API.auto_method = true
|
39
|
+
|
40
|
+
# No ANSI equivalent for ReadDirectoryChangesW
|
41
|
+
$strstr = API.new('strstr', 'PP', 'P', 'msvcrt')
|
42
|
+
$umask = API.new('_umask', 'I', 'I', 'msvcrt')
|
43
|
+
$wave = API.new('waveOutGetNumDevs', 'V', 'I', 'winmm')
|
44
|
+
$read = API.new('ReadDirectoryChangesW', 'LPLILPPP', 'B')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class TC_Windows_API < Test::Unit::TestCase
|
49
|
+
include Windows::Test
|
50
|
+
include Windows::Foo
|
51
|
+
include Windows::Bar
|
52
|
+
include Windows::Baz
|
53
|
+
|
54
|
+
def setup
|
55
|
+
@buf = 0.chr * 256
|
56
|
+
@runtimes = ['msvcrt', 'msvcr80', 'msvcr90', 'msvcr100']
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_version
|
60
|
+
assert_equal('0.4.4', API::VERSION)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_full_data_types
|
64
|
+
assert_nothing_raised{
|
65
|
+
API.new('GetWindowsDirectory', ['LPTSTR', 'UINT'], 'BOOL')
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_msvcrt_constant
|
70
|
+
assert_true(@runtimes.include?(Windows::MSVCRT_DLL))
|
71
|
+
end
|
72
|
+
|
73
|
+
# Validate that funcs like 'strstr' get an uppercase constant like 'Strstr'
|
74
|
+
def test_lower_case_to_capitalized_constant
|
75
|
+
assert_not_nil(Windows::Baz::Strstr)
|
76
|
+
assert_not_nil(Windows::Baz::Umask)
|
77
|
+
assert_not_nil(Windows::Baz::WaveOutGetNumDevs)
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_explicit_wide_function_only
|
81
|
+
assert_not_nil(Windows::Baz::ReadDirectoryChangesW)
|
82
|
+
assert_false(Windows::Baz.constants.include?('ReadDirectoryChanges'))
|
83
|
+
assert_false(Windows::Baz.constants.include?('ReadDirectoryChangesA'))
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_lower_case_auto_methods
|
87
|
+
assert_respond_to(self, :strstr)
|
88
|
+
assert_respond_to(self, :umask)
|
89
|
+
assert_respond_to(self, :_umask)
|
90
|
+
assert_respond_to(self, :waveOutGetNumDevs)
|
91
|
+
assert_equal('llo', strstr('hello', 'l'))
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_auto_unicode
|
95
|
+
assert_not_nil(Windows::Bar::GetUserName)
|
96
|
+
assert_respond_to(self, :GetUserName)
|
97
|
+
assert_not_respond_to(self, :GetUserNameA)
|
98
|
+
assert_not_respond_to(self, :GetUserNameW)
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_auto_constant
|
102
|
+
assert_not_nil(Windows::Test::GetCurrentDirectory)
|
103
|
+
assert_not_nil(Windows::Bar::GetUserName)
|
104
|
+
assert_kind_of(Win32::API, Windows::Test::GetCurrentDirectory)
|
105
|
+
assert_respond_to(Windows::Test::GetCurrentDirectory, :call)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_auto_method
|
109
|
+
assert_respond_to(self, :GetCurrentDirectory)
|
110
|
+
assert_respond_to(self, :GetCurrentDirectoryA)
|
111
|
+
assert_respond_to(self, :GetCurrentDirectoryW)
|
112
|
+
assert_not_respond_to(self, :GetSystemDirectory)
|
113
|
+
assert_not_respond_to(self, :GetSystemDirectoryA)
|
114
|
+
assert_not_respond_to(self, :GetSystemDirectoryW)
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_call
|
118
|
+
assert_respond_to($test_method, :call)
|
119
|
+
assert_respond_to($foo_method, :call)
|
120
|
+
assert_nothing_raised{ $test_method.call(@buf.length, @buf) }
|
121
|
+
assert_nothing_raised{ $foo_method.call(@buf, @buf.length) }
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_dll_name
|
125
|
+
assert_respond_to($test_method, :dll_name)
|
126
|
+
assert_equal('kernel32', $test_method.dll_name)
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_function_name
|
130
|
+
assert_respond_to($test_method, :function_name)
|
131
|
+
assert_equal('GetCurrentDirectory', $test_method.function_name)
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_prototype
|
135
|
+
assert_respond_to($test_method, :prototype)
|
136
|
+
assert_equal(['P', 'P'], $test_method.prototype)
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_return_type
|
140
|
+
assert_respond_to($test_method, :return_type)
|
141
|
+
assert_equal('L', $test_method.return_type)
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_effective_function_name
|
145
|
+
assert_respond_to($test_method, :effective_function_name)
|
146
|
+
assert_equal('GetCurrentDirectoryA', $test_method.effective_function_name)
|
147
|
+
assert_equal('strstr', $strstr.effective_function_name)
|
148
|
+
assert_equal('waveOutGetNumDevs', $wave.effective_function_name)
|
149
|
+
assert_equal('ReadDirectoryChangesW', $read.effective_function_name)
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_bad_prototype_raises_error
|
153
|
+
assert_raise(Win32::API::PrototypeError){
|
154
|
+
Windows::API.new('GetCurrentDirectory', 'XL', 'L')
|
155
|
+
}
|
156
|
+
assert_raise(Win32::API::PrototypeError){
|
157
|
+
Windows::API.new('GetCurrentDirectory', 'PL', 'X')
|
158
|
+
}
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_bad_function_raises_error
|
162
|
+
assert_raise(Win32::API::LoadLibraryError){
|
163
|
+
Windows::API.new('GetCurrentFooBar', 'LL', 'L')
|
164
|
+
}
|
165
|
+
end
|
166
|
+
|
167
|
+
def teardown
|
168
|
+
@buf = nil
|
169
|
+
@runtimes = nil
|
170
|
+
end
|
171
|
+
end
|