public
Last active

Ruby 1.9.2 DL nested structs

  • Download Gist
dl_readfile.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
require 'dl/import'
require 'dl/types'
 
module Win32FileAPI
extend DL::Importer
 
dlload 'kernel32.dll', 'user32.dll'
 
include DL::Win32Types
 
# structs
SECURITY_ATTRIBUTES = struct [
'DWORD nLength',
'PVOID lpSecurityDescriptor',
'BOOL bInheritHandle',
]
typealias 'PSECURITY_ATTRIBUTES', 'SECURITY_ATTRIBUTES*'
 
# FIXME these 3 are wrong
OL_OFFSET = struct [
'DWORD Offset',
'DWORD OffsetHigh',
]
OL_UNION = union [
'OL_OFFSET _offset',
'PVOID Pointer',
]
OVERLAPPED = struct [
'PDWORD Internal',
'PDWORD InternalHigh',
'OL_UNION _internal',
'HANDLE hEvent',
]
typealias 'POVERLAPPED', 'OVERLAPPED*'
 
# user32.dll functions
extern 'int MessageBoxA(HWND, LPCSTR, LPCSTR, UINT)', :stdcall
 
# kernel32.dll functions
extern 'HANDLE CreateFileA(LPCSTR, DWORD, DWORD, PSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE)', :stdcall
# XXX cheat? - BOOL ReadFile(HANDLE, PVOID, DWORD, PDWORD, char*)
# really cheat? - BOOL ReadFile(HANDLE, PVOID, DWORD, PDWORD, PVOID)
extern 'BOOL ReadFile(HANDLE, PVOID, DWORD, PDWORD, POVERLAPPED)', :stdcall
end
 
rv = Win32FileAPI::MessageBoxA(DL::NULL, 'Hello from Ruby 1.9 DL!', 'DL Info', 0x40 | 0x3)
puts rv

Union means that members are overlaped - you can use either DWORDs Offset and OffsetHigh, or Pointer.

And MSDN states that Pointer should not be used, so I think you should try to directly delare Offset and OffsetHigh like


  OVERLAPPED = struct [
    'PDWORD Internal',
    'PDWORD InternalHigh',
    'DWORD Offset',
    'DWORD OffsetHigh',
    #'OL_UNION _internal', # skip since Offsets are declared directly
    'HANDLE hEvent',
  ]

And then OL_OFFSET and OL_UNION are not needed.

re: unions, understood. I overlooked Pointer's doc "Reserved for system use; do not use after initialization to zero."

I think you're right that it all collapses down to your struct example...thanks...back to looking at porting something like http://msdn.microsoft.com/en-us/library/aa365592(v=vs.85).aspx to ruby.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.