kernel32lib 0.0.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
+ SHA1:
3
+ metadata.gz: 9461887352b87d1814c7f5be6e1324efad7efa60
4
+ data.tar.gz: 29178f62a4d0b9e696281a1fdc17104a6b61113d
5
+ SHA512:
6
+ metadata.gz: 75ad77da4e4a2be816df0cabd7dd88f2c6fe127396b9ee14e0ea12358ca1c6d752f37cd61fb54c8ec8e5f8d7b6c0230d407ab8814f926f58593efa7ce4f323e3
7
+ data.tar.gz: 8479fd03a6bc3203b442a44a800f35f466482fa585e5a0e2d2a7f302874439afd1e52d9159c3d284ed108338797376f8898af649e1b6746abb986669fb146421
data/CHANGELOG.md ADDED
@@ -0,0 +1,25 @@
1
+ ## 0.0.2 (Jun 2, 2016)
2
+ Features:
3
+ - add WinAPI Consoles functions:
4
+ * GetStdHandle
5
+ * SetConsoleCursorPosition
6
+ * GetConsoleScreenBufferInfo
7
+ * SetConsoleScreenBufferSize
8
+ * SetConsoleWindowInfo
9
+ * SetConsoleTextAttribute
10
+ * GetConsoleOutputCP
11
+ * SetConsoleOutputCP
12
+ Bugfixes:
13
+ - check get_file_information_by_handle
14
+
15
+ ## 0.0.1 (Jun 1, 2016)
16
+ Features:
17
+ - add WinAPI Files functions:
18
+ * CreateFile
19
+ * CloseHandle
20
+ * GetFileInformationByHandle
21
+ * FileTimeToSystemTime
22
+ * GetFileTime
23
+ - add module Fixture (fixture for test)
24
+ Bugfixes:
25
+ - initial release
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Dmitriy Mullo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # Kernel32Lib: kernel32lib
2
+
3
+ Win32 API wrapper for Ruby which uses Fiddle and Fiddle::Importer
4
+
5
+ ## Installation and usage
6
+
7
+ install:
8
+ ```
9
+ gem install kernel32lib
10
+ ```
11
+ usage:
12
+ ```ruby
13
+ require 'kernel32lib/files'
14
+ require 'kernel32lib/consoles'
15
+
16
+ # call Kernel32Lib.function_name(function_params (optional)...)
17
+ # e.g.
18
+ Kernel32Lib.get_file_information_by_handle(__FILE__)
19
+ # or
20
+ Kernel32Lib.get_console_output_cp
21
+ ```
22
+ see more detailed example in ./fixtures/fixture_kernel32lib.rb
23
+
24
+ ## Troubleshooting
25
+
26
+ Visit to [kernel32lib homepage](https://github.com/dim11981/kernel32lib)
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ #require 'kernel32lib'
4
+ require(File.expand_path('../../win_api/files',__FILE__))
5
+ require(File.expand_path('../../win_api/consoles',__FILE__))
6
+
7
+ # Fixture module
8
+ # test fixture
9
+ module Fixture
10
+ def self.show
11
+ system 'cls'
12
+ puts '# get_file_information_by_handle'
13
+ puts " - file information: #{Kernel32Lib.get_file_information_by_handle(__FILE__)}"
14
+ puts
15
+ puts '# get_file_time'
16
+ puts " - file time: #{Kernel32Lib.get_file_time(__FILE__)}"
17
+ puts
18
+ puts '# try console'
19
+ console_output = Kernel32Lib.GetStdHandle(Kernel32Lib::STD_OUTPUT_HANDLE)
20
+ init_console_screen_buffer_info = Kernel32Lib.get_console_screen_buffer_info(console_output)
21
+ init_attributes = init_console_screen_buffer_info[:attributes]
22
+ init_screen_buffer_size = init_console_screen_buffer_info[:size]
23
+ init_window = init_console_screen_buffer_info[:window]
24
+ puts " - console screen buffer info (1): #{init_console_screen_buffer_info}"
25
+ puts " - console output code page: #{Kernel32Lib.get_console_output_cp}"
26
+ Kernel32Lib.set_console_window_info(console_output,true,[0,0,1,1])
27
+ Kernel32Lib.set_console_screen_buffer_size(console_output,[120,80])
28
+ Kernel32Lib.set_console_window_info(console_output,true,[0,0,70,25])
29
+ Kernel32Lib.set_console_text_attribute(console_output,Kernel32Lib::FOREGROUND_GREEN | Kernel32Lib::FOREGROUND_INTENSITY | Kernel32Lib::BACKGROUND_BLUE)
30
+ puts " - console screen buffer info (2): #{Kernel32Lib.get_console_screen_buffer_info(console_output)}"
31
+ 9.times { print '.'; sleep(0.5) }
32
+ Kernel32Lib.set_console_text_attribute(console_output,init_attributes.to_i)
33
+ Kernel32Lib.set_console_window_info(console_output,true,[0,0,1,1])
34
+ Kernel32Lib.set_console_screen_buffer_size(console_output,init_screen_buffer_size)
35
+ Kernel32Lib.set_console_window_info(console_output,true,init_window)
36
+ puts
37
+ end
38
+
39
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require(File.expand_path('../lib/version.rb',__FILE__))
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'kernel32lib'
8
+ s.version = Kernel32Lib::VERSION
9
+ s.licenses = ['MIT']
10
+ s.summary = 'kernel32lib lib'
11
+ s.description = 'Win32 API wrapper for Ruby which uses Fiddle and Fiddle::Importer'
12
+ s.authors = ['Dmitriy Mullo']
13
+ s.email = ['d.a.mullo1981@gmail.com']
14
+ s.homepage = 'https://github.com/dim11981/kernel32lib'
15
+ s.platform = Gem::Platform::RUBY
16
+ s.files = Dir['*.md']+Dir['kernel32lib.*']+Dir['lib/*.rb']+Dir['win_api/*.rb']+Dir['test/*.rb']+Dir['fixtures/*']
17
+ s.require_path = 'lib'
18
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ require 'fiddle'
4
+ require 'fiddle/import'
5
+
6
+ # Kernel32Lib module
7
+ # supports kernel32 WinAPI functions
8
+ module Kernel32Lib
9
+ extend Fiddle::Importer
10
+ dlload 'kernel32.dll'
11
+ extern 'unsigned long GetLastError(void)'
12
+
13
+ # GetLastError
14
+ def self.get_last_error
15
+ Kernel32Lib.GetLastError(0)
16
+ end
17
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ # Kernel32Lib module
4
+ # version
5
+ module Kernel32Lib
6
+ VERSION = '0.0.2'
7
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ require(File.expand_path('../../fixtures/fixture_kernel32lib',__FILE__))
4
+
5
+ # begin test
6
+ Fixture.show
7
+ # end test
@@ -0,0 +1,94 @@
1
+ # encoding: utf-8
2
+
3
+ require(File.expand_path('../../lib/kernel32lib.rb',__FILE__))
4
+
5
+ # Kernel32Lib module
6
+ # Consoles
7
+ module Kernel32Lib
8
+ extern 'void* GetStdHandle(unsigned long)'
9
+ extern 'int SetConsoleCursorPosition(void*,void*)'
10
+ extern 'int GetConsoleScreenBufferInfo(void*,void*)'
11
+ extern 'int SetConsoleScreenBufferSize(void*,void*)'
12
+ extern 'int SetConsoleWindowInfo(void*,void,void*)'
13
+ extern 'int SetConsoleTextAttribute(void*,unsigned long)'
14
+ extern 'unsigned int GetConsoleOutputCP(void)'
15
+ extern 'int SetConsoleOutputCP(unsigned int)'
16
+
17
+ STD_INPUT_HANDLE = -10
18
+ STD_OUTPUT_HANDLE = -11
19
+ STD_ERROR_HANDLE = -12
20
+
21
+ # Attributes flags:
22
+ FOREGROUND_BLUE = 0x0001 # text color contains blue.
23
+ FOREGROUND_GREEN = 0x0002 # text color contains green.
24
+ FOREGROUND_RED = 0x0004 # text color contains red.
25
+ FOREGROUND_WHITE = 0x0007 # text color contains white on black background.
26
+ FOREGROUND_INTENSITY = 0x0008 # text color is intensified.
27
+ BACKGROUND_BLUE = 0x0010 # background color contains blue.
28
+ BACKGROUND_GREEN = 0x0020 # background color contains green.
29
+ BACKGROUND_RED = 0x0040 # background color contains red.
30
+ BACKGROUND_WHITE = 0x0070 # background color contains white with black foreground.
31
+ BACKGROUND_INTENSITY = 0x0080 # background color is intensified.
32
+ COMMON_LVB_LEADING_BYTE = 0x0100 # Leading Byte of DBCS
33
+ COMMON_LVB_TRAILING_BYTE = 0x0200 # Trailing Byte of DBCS
34
+ COMMON_LVB_GRID_HORIZONTAL = 0x0400 # DBCS: Grid attribute: top horizontal.
35
+ COMMON_LVB_GRID_LVERTICAL = 0x0800 # DBCS: Grid attribute: left vertical.
36
+ COMMON_LVB_GRID_RVERTICAL = 0x1000 # DBCS: Grid attribute: right vertical.
37
+ COMMON_LVB_REVERSE_VIDEO = 0x4000 # DBCS: Reverse fore/back ground attribute.
38
+ COMMON_LVB_UNDERSCORE = 0x8000 # DBCS: Underscore.
39
+ COMMON_LVB_SBCSDBCS = 0x0300 # SBCS or DBCS flag.
40
+
41
+ # SetConsoleCursorPosition
42
+ def self.set_console_cursor_position(console_output,cursor_position)
43
+ lp_cursor_position = cursor_position.pack('s2')
44
+ Kernel32Lib.SetConsoleCursorPosition(console_output,lp_cursor_position.unpack('i')[0].to_i)
45
+ end
46
+
47
+ # GetConsoleScreenBufferInfo
48
+ def self.get_console_screen_buffer_info(console_output)
49
+ lp_console_screen_buffer_info = '0'*22
50
+ Kernel32Lib.GetConsoleScreenBufferInfo(console_output,lp_console_screen_buffer_info)
51
+ lp_console_screen_buffer_info = lp_console_screen_buffer_info.unpack('s4Ss4s2')
52
+ console_screen_buffer_info = {
53
+ size: [ lp_console_screen_buffer_info[0].to_i, lp_console_screen_buffer_info[1].to_i ],
54
+ cursor_position: [ lp_console_screen_buffer_info[2], lp_console_screen_buffer_info[3].to_i ],
55
+ attributes: lp_console_screen_buffer_info[4].to_i,
56
+ window: [ lp_console_screen_buffer_info[5].to_i, lp_console_screen_buffer_info[6].to_i, lp_console_screen_buffer_info[7].to_i, lp_console_screen_buffer_info[8].to_i ],
57
+ maximum_window_size: [ lp_console_screen_buffer_info[9].to_i, lp_console_screen_buffer_info[10].to_i ]
58
+ }
59
+ console_screen_buffer_info
60
+ end
61
+
62
+ # SetConsoleTextAttribute
63
+ def self.set_console_text_attribute(console_output,attributes)
64
+ Kernel32Lib.SetConsoleTextAttribute(console_output,attributes)
65
+ end
66
+
67
+ # GetStdHandle
68
+ def self.get_std_handle(std_handle)
69
+ Kernel32Lib.GetStdHandle(std_handle)
70
+ end
71
+
72
+ # GetConsoleOutputCP
73
+ def self.get_console_output_cp
74
+ Kernel32Lib.GetConsoleOutputCP(0)
75
+ end
76
+
77
+ # SetConsoleOutputCP
78
+ def self.set_console_output_cp(code_page_id)
79
+ Kernel32Lib.SetConsoleOutputCP(code_page_id)
80
+ end
81
+
82
+ # SetConsoleScreenBufferSize
83
+ def self.set_console_screen_buffer_size(console_output,size)
84
+ lp_size = size.pack('s2')
85
+ Kernel32Lib.SetConsoleScreenBufferSize(console_output,lp_size.unpack('i')[0].to_i)
86
+ end
87
+
88
+ # SetConsoleWindowInfo
89
+ def self.set_console_window_info(console_output,absolute,console_window)
90
+ lp_console_window = console_window.pack('s4')
91
+ Kernel32Lib.SetConsoleWindowInfo(console_output,absolute,lp_console_window)
92
+ end
93
+
94
+ end
data/win_api/files.rb ADDED
@@ -0,0 +1,113 @@
1
+ # encoding: utf-8
2
+
3
+ require(File.expand_path('../../lib/kernel32lib.rb',__FILE__))
4
+
5
+ # Kernel32Lib module
6
+ # Files
7
+ module Kernel32Lib
8
+ extern 'void* CreateFile(char*,unsigned long,unsigned long,void*,unsigned long,unsigned long,void*)'
9
+ extern 'int CloseHandle(void*)'
10
+ extern 'int GetFileInformationByHandle(void*,void*)'
11
+ extern 'int FileTimeToSystemTime(void*,void*)'
12
+ extern 'int GetFileTime(void*,void*,void*,void*)'
13
+
14
+ # Access rights constants
15
+ FILE_READ_DATA = 1
16
+ FILE_WRITE_DATA = 2
17
+ FILE_APPEND_DATA = 4
18
+ GENERIC_READ = 0x80000000
19
+ GENERIC_WRITE = 0x40000000
20
+ GENERIC_EXECUTE = 0x20000000
21
+ GENERIC_ALL = 0x10000000
22
+
23
+ # Share mode constants
24
+ FILE_SHARE_0 = 0x00000000 # Prevents other processes from opening a file or device if they request delete, read, or write access.
25
+ FILE_SHARE_DELETE = 0x00000004 # Enables subsequent open operations on a file or device to request delete access.
26
+ FILE_SHARE_READ = 0x00000001 # Enables subsequent open operations on a file or device to request read access.
27
+ FILE_SHARE_WRITE = 0x00000002 # Enables subsequent open operations on a file or device to request write access.
28
+
29
+ # Creation disposition constants
30
+ CREATE_ALWAYS = 2 # Creates a new file, always.
31
+ CREATE_NEW = 1 # Creates a new file, only if it does not already exist.
32
+ OPEN_ALWAYS = 4 # Opens a file, always.
33
+ OPEN_EXISTING = 3 # Opens a file or device, only if it exists.
34
+ TRUNCATE_EXISTING = 5 # Opens a file and truncates it so that its size is zero bytes, only if it exists
35
+
36
+ # CreateFile flags and attributes constants
37
+ FILE_ATTRIBUTE_READONLY = 1 # The file is read only. Applications can read the file, but cannot write to or delete it.
38
+ FILE_ATTRIBUTE_HIDDEN = 2 # The file is hidden. Do not include it in an ordinary directory listing.
39
+ FILE_ATTRIBUTE_SYSTEM = 4 # The file is part of or used exclusively by an operating system.
40
+ FILE_ATTRIBUTE_DIRECTORY = 16 # The handle that identifies a directory.
41
+ FILE_ATTRIBUTE_ARCHIVE = 32 # The file should be archived. Applications use this attribute to mark files for backup or removal.
42
+ FILE_ATTRIBUTE_NORMAL = 128 # The file does not have other attributes set. This attribute is valid only if used alone.
43
+ FILE_ATTRIBUTE_TEMPORARY = 256 # The file is being used for temporary storage.
44
+ FILE_ATTRIBUTE_OFFLINE = 4096 # The data of a file is not immediately available.
45
+ FILE_ATTRIBUTE_ENCRYPTED = 16384 # The file or directory is encrypted.
46
+
47
+ FILE_FLAG_BACKUP_SEMANTICS = 0x02000000 # The file is being opened or created for a backup or restore operation.
48
+ FILE_FLAG_DELETE_ON_CLOSE = 0x04000000 # The file is to be deleted immediately after all of its handles are closed, which includes the specified handle and any other open or duplicated handles.
49
+ FILE_FLAG_NO_BUFFERING = 0x20000000 # The file or device is being opened with no system caching for data reads and writes.
50
+ FILE_FLAG_OPEN_NO_RECALL = 0x00100000 # The file data is requested, but it should continue to be located in remote storage.
51
+ FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000 # Normal reparse point processing will not occur; CreateFile will attempt to open the reparse point.
52
+ FILE_FLAG_OVERLAPPED = 0x40000000 # The file or device is being opened or created for asynchronous I/O.
53
+ FILE_FLAG_POSIX_SEMANTICS = 0x0100000 # Access will occur according to POSIX rules.
54
+ FILE_FLAG_RANDOM_ACCESS = 0x10000000 # Access is intended to be random.
55
+ FILE_FLAG_SESSION_AWARE = 0x00800000 # The file or device is being opened with session awareness.
56
+ FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000 # Access is intended to be sequential from beginning to end.
57
+ FILE_FLAG_WRITE_THROUGH = 0x80000000 # Write operations will not go through any intermediate cache, they will go directly to disk.
58
+
59
+ # FileTimeToSystemTime
60
+ def self.file_time_to_system_time(file_time)
61
+ lp_system_time = '0'*16
62
+ Kernel32Lib.FileTimeToSystemTime(file_time,lp_system_time)
63
+ lp_system_time = lp_system_time.unpack('S8')
64
+ Time.mktime(lp_system_time[0],lp_system_time[1],lp_system_time[3],lp_system_time[4],lp_system_time[5],lp_system_time[6]+lp_system_time[7]/1000)
65
+ end
66
+
67
+ # GetFileInformationByHandle
68
+ def self.get_file_information_by_handle(file_path)
69
+ lp_file_information = '0'*52
70
+ #handle = Kernel32Lib.CreateFile(file_name,1,3,0,3,48,0)
71
+ handle = Kernel32Lib.CreateFile(file_path,Kernel32Lib::FILE_READ_DATA,Kernel32Lib::FILE_SHARE_READ | Kernel32Lib::FILE_SHARE_WRITE,nil,Kernel32Lib::OPEN_EXISTING,0,nil)
72
+ Kernel32Lib.GetFileInformationByHandle(handle,lp_file_information)
73
+ Kernel32Lib.CloseHandle(handle)
74
+
75
+ lp_file_information = lp_file_information.unpack('I13')
76
+ file_information = {
77
+ file_path: file_path,
78
+ file_attributes: lp_file_information[0],
79
+ creation_time: Kernel32Lib.file_time_to_system_time(lp_file_information[1,2].pack('I2')),
80
+ last_access_time: Kernel32Lib.file_time_to_system_time(lp_file_information[3,4].pack('I2')),
81
+ last_write_time: Kernel32Lib.file_time_to_system_time(lp_file_information[5,6].pack('I2')),
82
+ volume_serial_number: lp_file_information[7],
83
+ file_size_high: lp_file_information[8],
84
+ file_size_low: lp_file_information[9],
85
+ number_of_links: lp_file_information[10],
86
+ file_index_high: lp_file_information[11],
87
+ file_index_low: lp_file_information[12]
88
+ }
89
+
90
+ file_information
91
+ end
92
+
93
+ # GetFileTime
94
+ def self.get_file_time(file_path)
95
+ lp_creation_time = '0'*8
96
+ lp_access_time = '0'*8
97
+ lp_write_time = '0'*8
98
+ handle = Kernel32Lib.CreateFile(file_path,Kernel32Lib::FILE_READ_DATA,Kernel32Lib::FILE_SHARE_READ | Kernel32Lib::FILE_SHARE_WRITE,nil,Kernel32Lib::OPEN_EXISTING,0,nil)
99
+ Kernel32Lib.GetFileTime(handle,lp_creation_time,lp_access_time,lp_write_time)
100
+ Kernel32Lib.CloseHandle(handle)
101
+
102
+ lp_creation_time = lp_creation_time.unpack('I2')
103
+ lp_access_time = lp_access_time.unpack('I2')
104
+ lp_write_time = lp_write_time.unpack('I2')
105
+
106
+ file_time = {
107
+ creation_time: Kernel32Lib.file_time_to_system_time(lp_creation_time.pack('I2')),
108
+ last_access_time: Kernel32Lib.file_time_to_system_time(lp_access_time.pack('I2')),
109
+ last_write_time: Kernel32Lib.file_time_to_system_time(lp_write_time.pack('I2')),
110
+ }
111
+ file_time
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kernel32lib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Dmitriy Mullo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Win32 API wrapper for Ruby which uses Fiddle and Fiddle::Importer
14
+ email:
15
+ - d.a.mullo1981@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - LICENSE.md
22
+ - README.md
23
+ - fixtures/fixture_kernel32lib.rb
24
+ - kernel32lib.gemspec
25
+ - lib/kernel32lib.rb
26
+ - lib/version.rb
27
+ - test/test_kernel32lib.rb
28
+ - win_api/consoles.rb
29
+ - win_api/files.rb
30
+ homepage: https://github.com/dim11981/kernel32lib
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.4.5.1
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: kernel32lib lib
54
+ test_files: []