Skip to content

Instantly share code, notes, and snippets.

Created December 30, 2014 13:21
What would you like to do?
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)'
# 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