Module: Win32
- Defined in:
- documented/init.rb
Overview
Namespace for Windows API interop via Fiddle.
Provides wrappers for kernel32, user32, advapi32, and shell32 DLL calls. Only available on Windows (mingw/win platform, non-Darwin).
Defined Under Namespace
Modules: Advapi32, Kernel32, Shell32, User32
Constant Summary collapse
- SIZEOF_CHAR =
Size in bytes of a char type in native code (typically 1).
Fiddle::SIZEOF_CHAR
- SIZEOF_LONG =
Size in bytes of a long type in native code (typically 4 or 8).
Fiddle::SIZEOF_LONG
- SEE_MASK_NOCLOSEPROCESS =
ShellExecuteEx mask flag to prevent process handle closure on return.
0x00000040- MB_OK =
MessageBox type flag: display OK button only.
0x00000000- MB_OKCANCEL =
MessageBox type flag: display OK and Cancel buttons.
0x00000001- MB_YESNO =
MessageBox type flag: display Yes and No buttons.
0x00000004- MB_ICONERROR =
MessageBox icon flag: display error icon (stop sign).
0x00000010- MB_ICONQUESTION =
MessageBox icon flag: display question mark icon.
0x00000020- MB_ICONWARNING =
MessageBox icon flag: display warning (exclamation) icon.
0x00000030- IDIOK =
1- IDICANCEL =
2- IDIYES =
6- IDINO =
7- KEY_ALL_ACCESS =
Registry key access mask: all standard access rights.
0xF003F- KEY_CREATE_SUB_KEY =
Registry key access mask: create subkey.
0x0004- KEY_ENUMERATE_SUB_KEYS =
Registry key access mask: enumerate subkeys.
0x0008- KEY_EXECUTE =
Registry key access mask: read-equivalent permissions.
0x20019- KEY_NOTIFY =
Registry key access mask: request change notifications.
0x0010- KEY_QUERY_VALUE =
Registry key access mask: query value.
0x0001- KEY_READ =
Registry key access mask: read key and values.
0x20019- KEY_SET_VALUE =
Registry key access mask: set value.
0x0002- KEY_WOW64_32KEY =
Registry key access mask: operate on 32-bit registry view on 64-bit Windows.
0x0200- KEY_WOW64_64KEY =
Registry key access mask: operate on 64-bit registry view on 64-bit Windows.
0x0100- KEY_WRITE =
Registry key access mask: write key and values.
0x20006- TokenElevation =
20- TOKEN_QUERY =
8- STILL_ACTIVE =
259- SW_SHOWNORMAL =
1- SW_SHOW =
5- PROCESS_QUERY_INFORMATION =
1024- PROCESS_VM_READ =
16- HKEY_LOCAL_MACHINE =
-2147483646
- REG_NONE =
0- REG_SZ =
1- REG_EXPAND_SZ =
2- REG_BINARY =
3- REG_DWORD =
4- REG_DWORD_LITTLE_ENDIAN =
4- REG_DWORD_BIG_ENDIAN =
5- REG_LINK =
6- REG_MULTI_SZ =
7- REG_QWORD =
11- REG_QWORD_LITTLE_ENDIAN =
11
Class Method Summary collapse
- .admin? ⇒ Boolean
-
.CreateProcess(args) ⇒ Hash
private
Spawns a new Windows process with optional inheritance, environment, and startup configuration.
- .GetCurrentProcess ⇒ Object
-
.GetExitCodeProcess(args) ⇒ Hash
private
Retrieves the exit code of a process.
-
.GetLastError ⇒ Integer
private
Returns the last Win32 error code from the calling thread.
-
.GetModuleFileName(args = {}) ⇒ Hash
private
Retrieves the file path of the executable module for a process.
-
.GetTokenInformation(args) ⇒ Hash?
private
Retrieves security token information for the current process.
-
.GetVersionEx ⇒ Hash
private
Retrieves Windows version and system information.
-
.isXP? ⇒ Boolean
private
Returns true if the system is Windows XP or earlier.
-
.MessageBox(args) ⇒ Integer
private
Displays a modal dialog box with a message, title, buttons, and icon.
-
.OpenProcessToken(args) ⇒ Hash
private
Opens the security token for a process.
-
.RegCloseKey(args) ⇒ Integer
private
Closes an open registry key handle.
-
.RegDeleteValue(args) ⇒ Integer
private
Deletes a registry value.
-
.RegOpenKeyEx(args) ⇒ Hash
private
Opens a registry key.
-
.RegQueryValueEx(args) ⇒ Hash
private
Retrieves the data and type of a registry value.
-
.RegSetValueEx(args) ⇒ Integer
private
Sets the data of a registry value, creating or overwriting it.
-
.ShellExecute(args) ⇒ Integer
private
Legacy execution wrapper; executes a file or URI with minimal configuration.
-
.ShellExecuteEx(args) ⇒ Hash
private
Executes a file or URI with optional verb, parameters, and window display mode.
Class Method Details
.admin? ⇒ Boolean
516 517 518 519 520 521 522 523 524 525 |
# File 'documented/init.rb', line 516 def self.admin? if Win32.isXP? return true else r = Win32.OpenProcessToken(:ProcessHandle => Win32.GetCurrentProcess, :DesiredAccess => TOKEN_QUERY) token_handle = r[:TokenHandle] r = Win32.GetTokenInformation(:TokenInformationClass => TokenElevation, :TokenHandle => token_handle) return (r[:TokenIsElevated] != 0) end end |
.CreateProcess(args) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Spawns a new Windows process with optional inheritance, environment, and startup configuration.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'documented/init.rb', line 140 def self.CreateProcess(args) if args[:lpCommandLine] lpCommandLine = args[:lpCommandLine].dup else lpCommandLine = nil end if args[:bInheritHandles] == false bInheritHandles = 0 elsif args[:bInheritHandles] == true bInheritHandles = 1 else bInheritHandles = args[:bInheritHandles].to_i end if args[:lpEnvironment].is_a?(Array) # fixme end lpStartupInfo = [68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] lpStartupInfo_index = { :lpDesktop => 2, :lpTitle => 3, :dwX => 4, :dwY => 5, :dwXSize => 6, :dwYSize => 7, :dwXCountChars => 8, :dwYCountChars => 9, :dwFillAttribute => 10, :dwFlags => 11, :wShowWindow => 12, :hStdInput => 15, :hStdOutput => 16, :hStdError => 17 } for sym in [:lpDesktop, :lpTitle] if args[sym] args[sym] = "#{args[sym]}\0" unless args[sym][-1, 1] == "\0" lpStartupInfo[lpStartupInfo_index[sym]] = Fiddle::Pointer.to_ptr(args[sym]).to_i end end for sym in [:dwX, :dwY, :dwXSize, :dwYSize, :dwXCountChars, :dwYCountChars, :dwFillAttribute, :dwFlags, :wShowWindow, :hStdInput, :hStdOutput, :hStdError] if args[sym] lpStartupInfo[lpStartupInfo_index[sym]] = args[sym] end end lpStartupInfo = lpStartupInfo.pack('LLLLLLLLLLLLSSLLLL') lpProcessInformation = [0, 0, 0, 0,].pack('LLLL') r = Kernel32.CreateProcess(args[:lpApplicationName], lpCommandLine, args[:lpProcessAttributes], args[:lpThreadAttributes], bInheritHandles, args[:dwCreationFlags].to_i, args[:lpEnvironment], args[:lpCurrentDirectory], lpStartupInfo, lpProcessInformation) lpProcessInformation = lpProcessInformation.unpack('LLLL') return :return => (r > 0 ? true : false), :hProcess => lpProcessInformation[0], :hThread => lpProcessInformation[1], :dwProcessId => lpProcessInformation[2], :dwThreadId => lpProcessInformation[3] end |
.GetCurrentProcess ⇒ Object
180 181 182 |
# File 'documented/init.rb', line 180 def self.GetCurrentProcess return Kernel32.GetCurrentProcess end |
.GetExitCodeProcess(args) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves the exit code of a process.
190 191 192 193 194 |
# File 'documented/init.rb', line 190 def self.GetExitCodeProcess(args) lpExitCode = [0].pack('L') r = Kernel32.GetExitCodeProcess(args[:hProcess].to_i, lpExitCode) return :return => r, :lpExitCode => lpExitCode.unpack('L')[0] end |
.GetLastError ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the last Win32 error code from the calling thread.
120 121 122 |
# File 'documented/init.rb', line 120 def self.GetLastError return Kernel32.GetLastError() end |
.GetModuleFileName(args = {}) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves the file path of the executable module for a process.
203 204 205 206 207 208 |
# File 'documented/init.rb', line 203 def self.GetModuleFileName(args = {}) args[:nSize] ||= 256 buffer = "\0" * args[:nSize].to_i r = Kernel32.GetModuleFileName(args[:hModule].to_i, buffer, args[:nSize].to_i) return :return => r, :lpFilename => buffer.gsub("\0", '') end |
.GetTokenInformation(args) ⇒ Hash?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves security token information for the current process.
262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'documented/init.rb', line 262 def self.GetTokenInformation(args) if args[:TokenInformationClass] == TokenElevation token_information_length = SIZEOF_LONG token_information = [0].pack('L') else return nil end return_length = [0].pack('L') r = Advapi32.GetTokenInformation(args[:TokenHandle].to_i, args[:TokenInformationClass], token_information, token_information_length, return_length) if args[:TokenInformationClass] == TokenElevation return :return => r, :TokenIsElevated => token_information.unpack('L')[0] end end |
.GetVersionEx ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves Windows version and system information.
214 215 216 217 218 219 |
# File 'documented/init.rb', line 214 def self.GetVersionEx a = [156, 0, 0, 0, 0, ("\0" * 128), 0, 0, 0, 0, 0].pack('LLLLLa128SSSCC') r = Kernel32.GetVersionEx(a) a = a.unpack('LLLLLa128SSSCC') return :return => r, :dwOSVersionInfoSize => a[0], :dwMajorVersion => a[1], :dwMinorVersion => a[2], :dwBuildNumber => a[3], :dwPlatformId => a[4], :szCSDVersion => a[5].strip, :wServicePackMajor => a[6], :wServicePackMinor => a[7], :wSuiteMask => a[8], :wProductType => a[9] end |
.isXP? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns true if the system is Windows XP or earlier.
508 509 510 |
# File 'documented/init.rb', line 508 def self.isXP? return (Win32.GetVersionEx[:dwMajorVersion] < 6) end |
.MessageBox(args) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Displays a modal dialog box with a message, title, buttons, and icon.
237 238 239 240 |
# File 'documented/init.rb', line 237 def self.MessageBox(args) args[:lpCaption] ||= "Lich v#{LICH_VERSION}" return User32.MessageBox(args[:hWnd].to_i, args[:lpText], args[:lpCaption], args[:uType].to_i) end |
.OpenProcessToken(args) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Opens the security token for a process.
283 284 285 286 287 |
# File 'documented/init.rb', line 283 def self.OpenProcessToken(args) token_handle = [0].pack('L') r = Advapi32.OpenProcessToken(args[:ProcessHandle].to_i, args[:DesiredAccess].to_i, token_handle) return :return => r, :TokenHandle => token_handle.unpack('L')[0] end |
.RegCloseKey(args) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Closes an open registry key handle.
398 399 400 |
# File 'documented/init.rb', line 398 def self.RegCloseKey(args) return Advapi32.RegCloseKey(args[:hKey]) end |
.RegDeleteValue(args) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Deletes a registry value.
387 388 389 390 |
# File 'documented/init.rb', line 387 def self.RegDeleteValue(args) args[:lpValueName] ||= 0 return Advapi32.RegDeleteValue(args[:hKey].to_i, args[:lpValueName]) end |
.RegOpenKeyEx(args) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Opens a registry key.
297 298 299 300 301 |
# File 'documented/init.rb', line 297 def self.RegOpenKeyEx(args) phkResult = [0].pack('L') r = Advapi32.RegOpenKeyEx(args[:hKey].to_i, args[:lpSubKey].to_s, 0, args[:samDesired].to_i, phkResult) return :return => r, :phkResult => phkResult.unpack('L')[0] end |
.RegQueryValueEx(args) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves the data and type of a registry value.
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'documented/init.rb', line 310 def self.RegQueryValueEx(args) args[:lpValueName] ||= 0 lpcbData = [0].pack('L') r = Advapi32.RegQueryValueEx(args[:hKey].to_i, args[:lpValueName], 0, 0, 0, lpcbData) if r == 0 lpcbData = lpcbData.unpack('L')[0] lpData = String.new.rjust(lpcbData, "\x00") lpcbData = [lpcbData].pack('L') lpType = [0].pack('L') r = Advapi32.RegQueryValueEx(args[:hKey].to_i, args[:lpValueName], 0, lpType, lpData, lpcbData) lpType = lpType.unpack('L')[0] lpcbData = lpcbData.unpack('L')[0] if [REG_EXPAND_SZ, REG_SZ, REG_LINK].include?(lpType) lpData.gsub!("\x00", '') elsif lpType == REG_MULTI_SZ lpData = lpData.gsub("\x00\x00", '').split("\x00") elsif lpType == REG_DWORD lpData = lpData.unpack('L')[0] elsif lpType == REG_QWORD lpData = lpData.unpack('Q')[0] elsif lpType == REG_BINARY # fixme elsif lpType == REG_DWORD_BIG_ENDIAN # fixme else # fixme end return :return => r, :lpType => lpType, :lpcbData => lpcbData, :lpData => lpData else return :return => r end end |
.RegSetValueEx(args) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets the data of a registry value, creating or overwriting it.
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'documented/init.rb', line 352 def self.RegSetValueEx(args) if [REG_EXPAND_SZ, REG_SZ, REG_LINK].include?(args[:dwType]) and (args[:lpData].is_a?(String)) lpData = args[:lpData].dup lpData.concat("\x00") cbData = lpData.length elsif (args[:dwType] == REG_MULTI_SZ) and (args[:lpData].is_a?(Array)) lpData = args[:lpData].join("\x00").concat("\x00\x00") cbData = lpData.length elsif (args[:dwType] == REG_DWORD) and (args[:lpData].is_a?(Integer)) lpData = [args[:lpData]].pack('L') cbData = 4 elsif (args[:dwType] == REG_QWORD) and (args[:lpData].is_a?(Integer)) lpData = [args[:lpData]].pack('Q') cbData = 8 elsif args[:dwType] == REG_BINARY # fixme return false elsif args[:dwType] == REG_DWORD_BIG_ENDIAN # fixme return false else # fixme return false end args[:lpValueName] ||= 0 return Advapi32.RegSetValueEx(args[:hKey].to_i, args[:lpValueName], 0, args[:dwType], lpData, cbData) end |
.ShellExecute(args) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Legacy execution wrapper; executes a file or URI with minimal configuration.
458 459 460 461 462 463 464 |
# File 'documented/init.rb', line 458 def self.ShellExecute(args) args[:lpOperation] ||= 0 args[:lpParameters] ||= 0 args[:lpDirectory] ||= 0 args[:nShowCmd] ||= 1 return Shell32.ShellExecute(args[:hwnd].to_i, args[:lpOperation], args[:lpFile], args[:lpParameters], args[:lpDirectory], args[:nShowCmd]) end |
.ShellExecuteEx(args) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Executes a file or URI with optional verb, parameters, and window display mode.
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'documented/init.rb', line 426 def self.ShellExecuteEx(args) # struct = [ (SIZEOF_LONG * 15), 0, 0, 0, 0, 0, 0, SW_SHOWNORMAL, 0, 0, 0, 0, 0, 0, 0 ] struct = [(SIZEOF_LONG * 15), 0, 0, 0, 0, 0, 0, SW_SHOW, 0, 0, 0, 0, 0, 0, 0] struct_index = { :cbSize => 0, :fMask => 1, :hwnd => 2, :lpVerb => 3, :lpFile => 4, :lpParameters => 5, :lpDirectory => 6, :nShow => 7, :hInstApp => 8, :lpIDList => 9, :lpClass => 10, :hkeyClass => 11, :dwHotKey => 12, :hIcon => 13, :hMonitor => 13, :hProcess => 14 } for sym in [:lpVerb, :lpFile, :lpParameters, :lpDirectory, :lpIDList, :lpClass] if args[sym] args[sym] = "#{args[sym]}\0" unless args[sym][-1, 1] == "\0" struct[struct_index[sym]] = Fiddle::Pointer.to_ptr(args[sym]).to_i end end for sym in [:fMask, :hwnd, :nShow, :hkeyClass, :dwHotKey, :hIcon, :hMonitor, :hProcess] if args[sym] struct[struct_index[sym]] = args[sym] end end struct = struct.pack('LLLLLLLLLLLLLLL') r = Shell32.ShellExecuteEx(struct) struct = struct.unpack('LLLLLLLLLLLLLLL') return :return => r, :hProcess => struct[struct_index[:hProcess]], :hInstApp => struct[struct_index[:hInstApp]] end |