Class: Win32::API::Callback
- Inherits:
-
Object
- Object
- Win32::API::Callback
- Defined in:
- ext/win32/api.c
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
The numeric address of the function pointer.
-
#prototype ⇒ Object
readonly
The prototype, returned as an array of characters.
-
#return_type ⇒ Object
readonly
The return type, returned as a single character, S, P, L, I, V or B.
Instance Method Summary collapse
-
#Win32::API::Callback.new(prototype) {|proto| ... } ⇒ Object
constructor
Creates and returns a new Win32::API::Callback object.
Constructor Details
#Win32::API::Callback.new(prototype) {|proto| ... } ⇒ Object
Creates and returns a new Win32::API::Callback object. The prototype arguments are yielded back to the block in the same order they were declared.
The prototype
is the function prototype for the callback function. This is a string. The possible valid characters are ‘I’ (integer), ‘L’ (long), ‘V’ (void), ‘P’ (pointer) or ‘S’ (string). Unlike API objects, API::Callback objects do not have a default prototype.
The return
argument is the return type for the callback function. The valid characters are the same as for the prototype
. The default is ‘L’ (long).
Example:
require 'win32/api'
include Win32
EnumWindows = API.new('EnumWindows', 'KP', 'L', 'user32')
GetWindowText = API.new('GetWindowText', 'LPI', 'I', 'user32')
EnumWindowsProc = API::Callback.new('LP', 'I'){ |handle, param|
buf = "\0" * 200
GetWindowText.call(handle, buf, 200);
puts buf.strip unless buf.strip.empty?
buf.index(param).nil? ? true : false
}
EnumWindows.call(EnumWindowsProc, 'UEDIT32')
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'ext/win32/api.c', line 111
static VALUE callback_init(int argc, VALUE* argv, VALUE self)
{
extern void *CallbackTable[];
VALUE v_proto, v_return, v_proc;
int i;
rb_scan_args(argc, argv, "11&", &v_proto, &v_return, &v_proc);
/* Validate prototype characters */
for(i = 0; i < RSTRING(v_proto)->len; i++){
switch(RSTRING(v_proto)->ptr[i]){
case 'I': case 'L': case 'P': case 'V': case 'S':
break;
default:
rb_raise(cAPIProtoError, "Illegal prototype '%c'",
RSTRING(v_proto)->ptr[i]
);
}
}
if(NIL_P(v_return) || RARRAY(v_return)->len == 0){
v_return = rb_str_new2("L");
}
else{
switch(*(char*)RSTRING(v_return)->ptr){
case 'I': case 'L': case 'P': case 'V': case 'S':
break;
default:
rb_raise(cAPIProtoError, "Illegal return type '%s'",
RSTRING(v_return)->ptr
);
}
}
rb_iv_set(self, "@function", v_proc);
rb_iv_set(self, "@prototype", v_proto);
rb_iv_set(self, "@return_type", v_return);
rb_iv_set(self, "@address", ULONG2NUM((LPARAM)CallbackTable[RSTRING(v_proto)->len]));
ActiveCallback = self;
return self;
}
|
Instance Attribute Details
#address ⇒ Object (readonly)
The numeric address of the function pointer
#prototype ⇒ Object (readonly)
The prototype, returned as an array of characters
#return_type ⇒ Object (readonly)
The return type, returned as a single character, S, P, L, I, V or B