win32api4ruby 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4e12c55a94e41ae82a4caa02b093cca95ee6f62a6fc4781a3acc33fe65b59aa5
4
+ data.tar.gz: 86a202af58ee9aae510cd9de1735485bc0d5b6023641fe14125044dbb40771c9
5
+ SHA512:
6
+ metadata.gz: 86de84285923096f3baabaf97ff6e515f2aa0b5fa14face385388274853ddeebcb1d6cca8ae82fa9862a40b2eb1840eb4bdcc912eb1aa21f50d52d7d00401296
7
+ data.tar.gz: f5d6fef669824a102b9fa7e47f25f7c3907282f0635ba0db20152467569594841e5b27f0c5a4237487543691318694d315eb8016a0ae33b36f8932bf2347aea7
checksums.yaml.gz.sig ADDED
@@ -0,0 +1 @@
1
+ .�u�0(I�ͮ� L���qN�ͼE�_�E�6UTU��a��̀�*�F@�|�`*_�a���������3���{P�8���\�k��q[�ߢ|�,x�q��_B��U:Ip9p�'��
data/.autotest ADDED
@@ -0,0 +1,23 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.extra_files << "../some/external/dependency.rb"
7
+ #
8
+ # at.libs << ":../some/external"
9
+ #
10
+ # at.add_exception 'vendor'
11
+ #
12
+ # at.add_mapping(/dependency.rb/) do |f, _|
13
+ # at.files_matching(/test_.*rb$/)
14
+ # end
15
+ #
16
+ # %w(TestA TestB).each do |klass|
17
+ # at.extra_class_map[klass] = "test/test_misc.rb"
18
+ # end
19
+ # end
20
+
21
+ # Autotest.add_hook :run_command do |at|
22
+ # system "rake build"
23
+ # end
data/History.txt ADDED
@@ -0,0 +1,11 @@
1
+ === 0.2.2 / 2026-10-27
2
+
3
+ * Make the gem open source.
4
+
5
+
6
+ === 1.0.0 / 2009-12-18
7
+
8
+ * 1 major enhancement
9
+
10
+ * Birthday!
11
+
data/Manifest.txt ADDED
@@ -0,0 +1,19 @@
1
+ .autotest
2
+ History.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ lib/win32api4ruby.rb
7
+ lib/win32api4ruby/base.rb
8
+ lib/win32api4ruby/comdlg.rb
9
+ lib/win32api4ruby/gdi.rb
10
+ lib/win32api4ruby/kernel.rb
11
+ lib/win32api4ruby/user.rb
12
+ lib/win32api4ruby/winspool.rb
13
+ test/all_tests.rb
14
+ test/base.rb
15
+ test/test_comdlg.rb
16
+ test/test_gdi.rb
17
+ test/test_kernel.rb
18
+ test/test_user.rb
19
+ test/test_winspool.rb
data/README.txt ADDED
@@ -0,0 +1,59 @@
1
+ = win32api4ruby
2
+
3
+ * https://github.com/larskanis/win32api4ruby
4
+
5
+ == DESCRIPTION:
6
+
7
+ Yet another win32 to ruby wrapper
8
+
9
+ == FEATURES:
10
+
11
+ * printing functions
12
+ * error handling
13
+ * both Unicode and ANSI-functions supported
14
+ * helpers for UTF-8
15
+
16
+ == PROBLEMS:
17
+
18
+ * not compatible to win32-gems for Ruby
19
+ * only a small subset of DLL-functions implemented
20
+
21
+ == INSTALL:
22
+
23
+ * gem install win32api4ruby
24
+
25
+ == SYNOPSIS:
26
+
27
+ short exsample:
28
+
29
+ require "win32api4ruby"
30
+ Win32api4ruby::Base.unicode = true
31
+ hwnd = Win32api4ruby::User.create_window_ex(0, 'Button'.to_s_win32, name, User::WS_VISIBLE, 0,0,100,100, 0, 0, 0, 0)
32
+
33
+
34
+ == REQUIREMENTS:
35
+
36
+ * based on FFI
37
+
38
+ == LICENSE:
39
+
40
+ (The MIT License)
41
+
42
+ Permission is hereby granted, free of charge, to any person obtaining
43
+ a copy of this software and associated documentation files (the
44
+ 'Software'), to deal in the Software without restriction, including
45
+ without limitation the rights to use, copy, modify, merge, publish,
46
+ distribute, sublicense, and/or sell copies of the Software, and to
47
+ permit persons to whom the Software is furnished to do so, subject to
48
+ the following conditions:
49
+
50
+ The above copyright notice and this permission notice shall be
51
+ included in all copies or substantial portions of the Software.
52
+
53
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
54
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
55
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
56
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
57
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
58
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
59
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.spec 'win32api4ruby' do
7
+ developer('Lars Kanis', 'lars@greiz-reinsdorf.de')
8
+ self.homepage = "https://github.com/larskanis/win32api4ruby"
9
+
10
+ extra_deps << ['ffi', '>=1.0.0']
11
+ end
12
+
13
+ # vim: syntax=ruby
@@ -0,0 +1,167 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require 'ffi'
4
+
5
+ module Win32api4ruby
6
+ module AbstractMemoryExtensions
7
+ ZeroZeroString = "\0\0"
8
+
9
+ def to_s_utf16
10
+ i = 0
11
+ while self.get_bytes(i,2)!=ZeroZeroString
12
+ i+=2
13
+ end
14
+ self.get_bytes(0,i)
15
+ end
16
+
17
+ # Converts the Data containing a Unicode or Ansi-Text based on the current
18
+ # Win32api4ruby::Base.unicode setting to UTF-8.
19
+ def to_s_utf8
20
+ return nil if null?
21
+ if Win32api4ruby::Base.unicode
22
+ to_s_utf16.encode(Encoding::UTF_8, Encoding::UTF_16LE)
23
+ else
24
+ get_string(0).encode(Encoding::UTF_8, Encoding::Windows_1252)
25
+ end
26
+ end
27
+ end
28
+
29
+ module StringExtensions
30
+ # Converts a UTF-8 string to Windows-encoding based on the current
31
+ # Win32api4ruby::Base.unicode setting.
32
+ def to_s_win32
33
+ if Win32api4ruby::Base.unicode
34
+ FFI::MemoryPointer.from_string (self+"\0").encode(Encoding::UTF_16LE, Encoding::UTF_8)
35
+ else
36
+ FFI::MemoryPointer.from_string self.encode(Encoding::Windows_1252, Encoding::UTF_8)
37
+ end
38
+ end
39
+ end
40
+
41
+ class ::FFI::AbstractMemory
42
+ include Win32api4ruby::AbstractMemoryExtensions
43
+ end
44
+
45
+ class ::String
46
+ include Win32api4ruby::StringExtensions
47
+ end
48
+
49
+ class Base
50
+
51
+ class ApiError < RuntimeError
52
+ def initialize(code, text)
53
+ super(text)
54
+ @code = code
55
+ end
56
+ end
57
+
58
+ # use UNICODE-methods by default
59
+ @@unicode = true
60
+
61
+ def self.unicode=(enabled)
62
+ @@unicode = !!enabled
63
+ end
64
+
65
+ def self.unicode
66
+ @@unicode
67
+ end
68
+
69
+ def self.exception_for_0
70
+ delete_errors
71
+ res = yield
72
+ raise_last_error if res.to_i == 0
73
+ return res
74
+ end
75
+
76
+ def self.exception_for_non_0
77
+ delete_errors
78
+ res = yield
79
+ raise_last_error if res.to_i != 0
80
+ return res
81
+ end
82
+
83
+ def self.exception_for_com_error
84
+ delete_errors
85
+ res = yield
86
+ if res != 0
87
+ text = case
88
+ when (res.to_i&0xffff0000) == 0x80070000 then `net helpmsg #{res.to_i&0xffff}`.strip
89
+ else get_last_error_text
90
+ end
91
+ raise ApiError, sprintf('%#x %s', res, text)
92
+ end
93
+ return res
94
+ end
95
+
96
+ def self.exception_for_nil
97
+ delete_errors
98
+ res = yield
99
+ raise_last_error if res.nil?
100
+ return res
101
+ end
102
+
103
+ def self.exception_for_negative
104
+ delete_errors
105
+ res = yield
106
+ raise_last_error if res < 0
107
+ return res
108
+ end
109
+
110
+ # --------------------------------------------------------------------------------------------------------------
111
+ def self.get_last_error_text(error_code=nil)
112
+ message = FFI::MemoryPointer.new(1024) #store 1kb space
113
+ error_code ||= Kernel.get_last_error()
114
+ Kernel.format_message(0x1000, '', error_code, 0, message, message.size, '')
115
+ message_str = message.to_s_utf8
116
+ sprintf('%s (%d)', message_str.chomp, error_code)
117
+ end
118
+ def self.raise_last_error
119
+ code = Kernel.get_last_error()
120
+ raise ApiError.new(code, get_last_error_text(code))
121
+ end
122
+ # --------------------------------------------------------------------------------------------------------------
123
+ def self.show_error
124
+ puts get_last_error_text
125
+ end
126
+ # --------------------------------------------------------------------------------------------------------------
127
+ def self.api_call(apiFunction, *args)
128
+ begin
129
+ res = apiFunction.call(*args)
130
+ erg = res.kind_of?(Array) ? res[0] : res
131
+ return erg
132
+ rescue RuntimeError => e
133
+ raise "#{e.class} #{e}: #{show_error}"
134
+ end
135
+ end
136
+ # private :api_call
137
+ # --------------------------------------------------------------------------------------------------------------
138
+ def self.check(variable, klass)
139
+ raise "#{variable}, #{variable.class} != #{klass.to_s}" unless variable.kind_of?(klass)
140
+ end
141
+ # --------------------------------------------------------------------------------------------------------------
142
+ def self.delete_errors
143
+ Kernel.set_last_error(0)
144
+ end
145
+
146
+ def self.create_struct(struct, data_or_size=nil)
147
+ case data_or_size
148
+ when String
149
+ struct.new(FFI::MemoryPointer.from_string(data_or_size))
150
+ when FFI::AbstractMemory
151
+ struct.new(data_or_size)
152
+ when Integer
153
+ struct.new(FFI::MemoryPointer.new(data_or_size))
154
+ when NilClass
155
+ struct.new
156
+ when struct
157
+ struct
158
+ else
159
+ raise "#{data_or_size.class} instead of String, FFI::AbstractMemory or Integer"
160
+ end
161
+ end
162
+
163
+ FFI.typedef :pointer, :handle
164
+ FFI.typedef :intptr_t, :lresult
165
+
166
+ end
167
+ end
@@ -0,0 +1,179 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require 'win32api4ruby/base'
4
+
5
+ module Win32api4ruby
6
+ class Comdlg < Base
7
+ extend FFI::Library
8
+ ffi_lib 'comdlg32'
9
+ ffi_convention :stdcall
10
+
11
+ ##
12
+ ## Flags for PrintDlg and PrintDlgEx.
13
+ ##
14
+ PD_ALLPAGES = 0x00000000
15
+ PD_SELECTION = 0x00000001
16
+ PD_PAGENUMS = 0x00000002
17
+ PD_NOSELECTION = 0x00000004
18
+ PD_NOPAGENUMS = 0x00000008
19
+ PD_COLLATE = 0x00000010
20
+ PD_PRINTTOFILE = 0x00000020
21
+ PD_PRINTSETUP = 0x00000040
22
+ PD_NOWARNING = 0x00000080
23
+ PD_RETURNDC = 0x00000100
24
+ PD_RETURNIC = 0x00000200
25
+ PD_RETURNDEFAULT = 0x00000400
26
+ PD_SHOWHELP = 0x00000800
27
+ PD_ENABLEPRINTHOOK = 0x00001000
28
+ PD_ENABLESETUPHOOK = 0x00002000
29
+ PD_ENABLEPRINTTEMPLATE = 0x00004000
30
+ PD_ENABLESETUPTEMPLATE = 0x00008000
31
+ PD_ENABLEPRINTTEMPLATEHANDLE = 0x00010000
32
+ PD_ENABLESETUPTEMPLATEHANDLE = 0x00020000
33
+ PD_USEDEVMODECOPIES = 0x00040000
34
+ PD_USEDEVMODECOPIESANDCOLLATE = 0x00040000
35
+ PD_DISABLEPRINTTOFILE = 0x00080000
36
+ PD_HIDEPRINTTOFILE = 0x00100000
37
+ PD_NONETWORKBUTTON = 0x00200000
38
+
39
+ #~ PRINTDLG_types = "L"*2 + "L"*2 + "L"*2 + "H"*5 + "L"*8
40
+ class PRINTDLG < FFI::Struct
41
+ packed if RUBY_PLATFORM =~ /i?86/
42
+ layout :lStructSize, :uint32,
43
+ :hwndOwner, :handle,
44
+ :hDevMode, :handle,
45
+ :hDevNames, :handle,
46
+ :hDC, :handle,
47
+ :Flags, :uint32,
48
+ :nFromPage, :uint16,
49
+ :nToPage, :uint16,
50
+ :nMinPage, :uint16,
51
+ :nMaxPage, :uint16,
52
+ :nCopies, :uint16,
53
+ :hInstance, :handle,
54
+ :lCustData, :pointer,
55
+ :lpfnPrintHook, :pointer,
56
+ :lpfnSetupHook, :pointer,
57
+ :lpPrintTemplateName, :pointer,
58
+ :lpSetupTemplateName, :pointer,
59
+ :hPrintTemplate, :handle,
60
+ :hSetupTemplate, :handle
61
+ end
62
+
63
+ def self.create_PRINTDLG(data_or_size=nil)
64
+ data = create_struct(PRINTDLG, data_or_size)
65
+ data[:lStructSize] = data.pointer.size
66
+ return data
67
+ end
68
+
69
+ #the functions that wrap the Win32API calls enforce type checking on arguments
70
+ # (through #check). This is to be consistent with the Win32API which will assume
71
+ # different arguments are calling different functions, throwing wacky errors
72
+ #API WRAPPERS
73
+ # --------------------------------------------------------------------------------------------------------------
74
+ attach_function :PrintDlgW, [ PRINTDLG.by_ref ], :int
75
+ attach_function :PrintDlgA, [ PRINTDLG.by_ref ], :int
76
+ def self.print_dlg(pPrintDlg)
77
+ exception_for_0{
78
+ unicode ? PrintDlgW(pPrintDlg) : PrintDlgA(pPrintDlg)
79
+ }
80
+ end
81
+
82
+ attach_function :PrintDlgExW, [ :pointer ], :handle
83
+ attach_function :PrintDlgExA, [ :pointer ], :handle
84
+ def self.print_dlg_ex(pPrintDlgEx)
85
+ exception_for_com_error{
86
+ unicode ? PrintDlgExW(pPrintDlgEx) : PrintDlgExA(pPrintDlgEx)
87
+ }
88
+ end
89
+
90
+ attach_function :PageSetupDlgW, [ :pointer ], :int
91
+ attach_function :PageSetupDlgA, [ :pointer ], :int
92
+ def self.print_setup_dlg(pPageSetupDlg)
93
+ exception_for_0{
94
+ unicode ? PageSetupDlgW(pPageSetupDlg) : PageSetupDlgA(pPageSetupDlg)
95
+ }
96
+ end
97
+
98
+ class PAGESETUPDLG < FFI::Struct
99
+ layout :lStructSize, :uint32,
100
+ :hwndOwner, :handle,
101
+ :hDevMode, :handle,
102
+ :hDevNames, :handle,
103
+ :Flags, :uint32,
104
+
105
+ :ptPaperSizeX, :long,
106
+ :ptPaperSizeY, :long,
107
+
108
+ :rtMinMarginL, :long,
109
+ :rtMinMarginT, :long,
110
+ :rtMinMarginR, :long,
111
+ :rtMinMarginB, :long,
112
+
113
+ :rtMarginL, :long,
114
+ :rtMarginT, :long,
115
+ :rtMarginR, :long,
116
+ :rtMarginB, :long,
117
+
118
+ :hInstance, :handle,
119
+ :lCustData, :pointer,
120
+ :lpfnPageSetupHook, :pointer,
121
+ :lpfnPagePaintHook, :pointer,
122
+ :lpPageSetupTemplateName, :pointer,
123
+ :hPageSetupTemplate, :handle
124
+ end
125
+
126
+ def self.create_PAGESETUPDLG(data_or_size=nil)
127
+ data = create_struct(PAGESETUPDLG, data_or_size)
128
+ data[:lStructSize] = data.pointer.size
129
+ return data
130
+ end
131
+
132
+ class PRINTDLGEX < FFI::Struct
133
+ layout :lStructSize, :uint32,
134
+ :hwndOwner, :handle,
135
+ :hDevMode, :handle,
136
+ :hDevNames, :handle,
137
+ :hDC, :handle,
138
+ :Flags, :uint32,
139
+ :Flags2, :uint32,
140
+ :ExclusionFlags, :uint32,
141
+ :nPageRanges, :uint32,
142
+ :nMaxPageRanges, :uint32,
143
+ :lpPageRanges, :pointer,
144
+ :nMinPage, :uint32,
145
+ :nMaxPage, :uint32,
146
+ :nCopies, :uint32,
147
+ :hInstance, :handle,
148
+ :lpPrintTemplateName, :pointer,
149
+ :lpCallback, :pointer,
150
+ :nPropertyPages, :uint32,
151
+ :plphPropertyPages, :pointer,
152
+ :nStartPage, :uint32,
153
+ :dwResultAction, :uint32
154
+ end
155
+ def self.create_PRINTDLGEX(data_or_size=nil)
156
+ data = create_struct(PRINTDLGEX, data_or_size)
157
+ data[:lStructSize] = data.pointer.size
158
+ return data
159
+ end
160
+
161
+ class DEVNAMES < FFI::Struct
162
+ # struct tagDEVNAMES {
163
+ # WORD wDriverOffset;
164
+ # WORD wDeviceOffset;
165
+ # WORD wOutputOffset;
166
+ # WORD wDefault;
167
+ # } DEVNAMES
168
+ layout :wDriverOffset, :uint16,
169
+ :wDeviceOffset, :uint16,
170
+ :wOutputOffset, :uint16,
171
+ :wDefault, :uint16
172
+ end
173
+ def self.create_DEVNAMES(data_or_size=nil)
174
+ create_struct(DEVNAMES, data_or_size)
175
+ end
176
+
177
+
178
+ end
179
+ end