Skip to content

Instantly share code, notes, and snippets.

@Youka
Created December 30, 2014 13:21
Show Gist options
  • Save Youka/3ebbdfd03454afa7d0c4 to your computer and use it in GitHub Desktop.
Save Youka/3ebbdfd03454afa7d0c4 to your computer and use it in GitHub Desktop.
Simple use of ruby's fiddle library for windows messageboxes
# Load importer part of fiddle (ffi) library
require 'fiddle/import'
# Create module as body for an importer instance
module MessageBox
# Extend this module to an importer
extend Fiddle::Importer
# Load 'user32' dynamic library into this importer
dlload 'user32'
# Set C aliases to this importer for further understanding of function signatures
typealias 'HANDLE', 'void*'
typealias 'HWND', 'HANDLE'
typealias 'LPCSTR', 'const char*'
typealias 'LPCWSTR', 'const wchar_t*'
typealias 'UINT', 'unsigned int'
# Import C functions from loaded libraries and set them as module functions
extern 'int MessageBoxA(HWND, LPCSTR, LPCSTR, UINT)'
extern 'int MessageBoxW(HWND, LPCWSTR, LPCWSTR, UINT)'
end
# Tests
MessageBox::MessageBoxA nil, "Hello world!の", "Fiddle ANSI", 0
MessageBox::MessageBoxW nil, "Hello world!の".encode("utf-16le"), "Fiddle WIDECHAR".encode("utf-16le"), 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment