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)


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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'ext/win32/api.c', line 126

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_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)CallbackTable[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