-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathwindows_cfunc1.rb
More file actions
43 lines (37 loc) · 1.46 KB
/
windows_cfunc1.rb
File metadata and controls
43 lines (37 loc) · 1.46 KB
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
module Win32
module User32
# win32 api compatibile version
# each libcall format is like this:
# - return tyupe
# - libraray
# - function to call
# - variable number of arguments to function
# - last param CFunc::Int.ew(X) - where X is number of arguments you pass above
DLL = CFunc.libcall(CFunc::Pointer, "kernel32.dll", "LoadLibraryA", "user32.dll", CFunc::Int.new(1))
MessageBeep = begin
ptr = CFunc.libcall(CFunc::Pointer, "kernel32.dll", "GetProcAddress", DLL, "MessageBeep", CFunc::Int.new(2))
CFunc::FunctionPointer.new(ptr).tap do |func|
func.arguments_type = [CFunc::UInt32]
func.result_type = CFunc::UInt32
end
end
MB_ICONASTERISK = 0x40
# shortcut version - it's a little bit slower, because it will try to open user32.dll again
MessageBox = proc do |text, caption, type|
CFunc.libcall(
CFunc::UInt32, # retrun type
"user32.dll", # dll
"MessageBoxA", # function
nil, text, caption, CFunc::Int.new(type), # args
CFunc::Int.new(4) # number of args
)
end
MB_OKCANCEL = 0x1
end
end
# then you can call MessageBeep like that
Win32::User32::MessageBeep.call(Win32::User32::MB_ICONASTERISK)
# if you want get return value:
ret = Win32::User32::MessageBeep.call(Win32::User32::MB_ICONASTERISK).value
# and calling shortcut version
Win32::User32::MessageBox.("MRuby MessageBox", "MRuby MessageBox", Win32::User32::MB_OKCANCEL)