Class: Win32::API::Callback

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

Defined Under Namespace

Classes: Error

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)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'ext/win32/api.c', line 107

static VALUE callback_init(int argc, VALUE* argv, VALUE self)
{
   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(cCallbackError, "Illegal prototype '%c'",
               RSTRING(v_proto)->ptr[i]);
      }
   }

   if(NIL_P(v_return) || RARRAY(v_return)->len == 0)
      v_return = rb_str_new2("L");

   rb_iv_set(self, "@function", v_proc);
   rb_iv_set(self, "@prototype", v_proto);
   rb_iv_set(self, "@return_type", v_return);
    
   return self;
}

Instance Attribute Details

#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