In Files

Parent

Methods

Win32::API::Callback

Attributes

prototype[R]

The prototype, returned as an array of characters

return_type[R]

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

address[R]

The numeric address of the function pointer

Public Class Methods

} click to toggle source

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

   return self;
}

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.