Class: Win32::API::Callback

Inherits:
Object
  • Object
show all
Defined in:
ext/win32/api.c

Instance Attribute Summary collapse

Instance Method Summary collapse

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')

Yields:

  • (proto)


209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'ext/win32/api.c', line 209

static VALUE callback_init(int argc, VALUE* argv, VALUE self)
{
   void *find_callback(VALUE,int);
   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_LEN(v_proto); i++){
      switch(RSTRING_PTR(v_proto)[i]){
         case 'I': case 'L': case 'P': case 'V': case 'S':
            break;
         default:
            rb_raise(cAPIProtoError, "Illegal prototype '%c'",
               RSTRING_PTR(v_proto)[i]
            );
      }
   }

   if(NIL_P(v_return) || RARRAY_LEN(v_return) == 0){
      v_return = rb_str_new2("L");
   }
   else{
      switch(*(TCHAR*)RSTRING_PTR(v_return)){
         case 'I': case 'L': case 'P': case 'V': case 'S':
            break;
         default:
            rb_raise(cAPIProtoError, "Illegal return type '%s'",
               RSTRING_PTR(v_return)
            );
      }
   }

   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", SIZET2NUM((LPARAM)find_callback(self,RSTRING_LEN(v_proto))));
   ActiveCallback = self;

   return self;
}

Instance Attribute Details

#addressObject (readonly)

The numeric address of the function pointer

#prototypeObject (readonly)

The prototype, returned as an array of characters

#return_typeObject (readonly)

The return type, returned as a single character, S, P, L, I, V or B