Class: Win32::API

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

Defined Under Namespace

Classes: Callback, Error

Constant Summary collapse

VERSION =

The version of this library, returned as a String

1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#Win32::API.new(function, prototype = 'V') ⇒ Object

Creates and returns a new Win32::API object. The function is the name of the Windows function.

The prototype is the function prototype for function. This can be a string or an array of characters. The possible valid characters are ‘I’ (integer), ‘L’ (long), ‘V’ (void), ‘P’ (pointer), or ‘K’ (callback). The default is void (‘V’).

The return argument is the return type for the function. The valid characters are the same as for the prototype. The default is ‘L’ (long).

The dll is the name of the DLL file that the function is exported from. The default is ‘kernel32’.

If the function cannot be found then an API::Error is raised (a subclass of RuntimeError).



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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'ext/win32/api.c', line 143

static VALUE api_init(int argc, VALUE* argv, VALUE self)
{
   HMODULE hLibrary;
   FARPROC fProc;
   Win32API* ptr;
   int i;
   VALUE v_proc, v_proto, v_return, v_dll, v_bool, v_name;
   
   rb_scan_args(argc, argv, "13", &v_proc, &v_proto, &v_return, &v_dll);
   
   Data_Get_Struct(self, Win32API, ptr);

   /* Convert a string prototype to an array of characters */
   if(rb_respond_to(v_proto, rb_intern("split")))
      v_proto = rb_str_split(v_proto, "");

   /* Set an arbitrary limit of 16 parameters */
   if(16 < RARRAY(v_proto)->len)
      rb_raise(rb_eArgError, "too many parameters: %d\n", RARRAY(v_proto)->len);

   /* Convert a nil or empty prototype to 'V' (void) automatically */
   if(NIL_P(v_proto) || RARRAY(v_proto)->len == 0){
      v_proto = rb_ary_new();
      rb_ary_push(v_proto, rb_str_new2("V"));
   }
   
   /* Set the default dll to 'kernel32' */
   if(NIL_P(v_dll))
   	v_dll = rb_str_new2("kernel32");
   	
   /* Set the default return type to 'L' (DWORD) */
   if(NIL_P(v_return))
   	v_return = rb_str_new2("L");

   SafeStringValue(v_dll);
   SafeStringValue(v_proc);

   hLibrary = LoadLibrary(TEXT(RSTRING(v_dll)->ptr));

   /* The most likely cause of failure is a bad DLL load path */
   if(!hLibrary){
      rb_raise(cAPIError, "LoadLibrary() function failed for '%s': %s",
         RSTRING(v_dll)->ptr,
         StringError(GetLastError())
      );
   }

   ptr->library = hLibrary;

   /* Attempt to get the function.  If it fails, try again with an 'A'
    * appended.  If that fails, try again with a 'W' appended.  If that
    * still fails, raise an API::Error.
    */
   fProc = GetProcAddress(hLibrary, TEXT(RSTRING(v_proc)->ptr));

   if(!fProc){
      VALUE v_ascii = rb_str_new3(v_proc);
      v_ascii = rb_str_cat(v_ascii, "A", 1);
      fProc = GetProcAddress(hLibrary, TEXT(RSTRING(v_ascii)->ptr));

      if(!fProc){
         VALUE v_unicode = rb_str_new3(v_proc);
         v_unicode = rb_str_cat(v_unicode, "W", 1);
         fProc = GetProcAddress(hLibrary, TEXT(RSTRING(v_unicode)->ptr));

         if(!fProc){
            rb_raise(
               cAPIError,
               "GetProcAddress() failed for '%s', '%s' and '%s': %s",
               RSTRING(v_proc)->ptr,
               RSTRING(v_ascii)->ptr,
               RSTRING(v_unicode)->ptr,
               StringError(GetLastError())
            );
         }
      }
   }

   ptr->function = fProc;

   /* Push the numeric prototypes onto our int array for later use. */
   for(i = 0; i < RARRAY(v_proto)->len; i++){
      SafeStringValue(RARRAY(v_proto)->ptr[i]);
      switch(*(char*)StringValuePtr(RARRAY(v_proto)->ptr[i])){
         case 'L':
            ptr->prototype[i] = _T_LONG;
            break;
         case 'P':
            ptr->prototype[i] = _T_POINTER;
            break;
         case 'I': case 'B':
            ptr->prototype[i] = _T_INTEGER;
            break;
         case 'V':
            ptr->prototype[i] = _T_VOID;
            break;
         case 'K':
            ptr->prototype[i] = _T_CALLBACK;
            break;
         default:
            rb_raise(cAPIError, "Illegal prototype '%s'", RARRAY(v_proto)->ptr[i]);
      }
   }

   /* Store the return type for later use.  Automatically convert empty
    * strings or nil to type void.
    */
   if(NIL_P(v_return) || RSTRING(v_return)->len == 0){
      v_return = rb_str_new2("V");
      ptr->return_type = _T_VOID;
   }
   else{
      SafeStringValue(v_return);
      switch(*RSTRING(v_return)->ptr){
         case 'L':
            ptr->return_type = _T_LONG;
            break;
         case 'P':
            ptr->return_type = _T_POINTER;
            break;
         case 'I': case 'B':
            ptr->return_type = _T_INTEGER;
            break;
         case 'V':
            ptr->return_type = _T_VOID;
            break;
         default:
            rb_raise(cAPIError, "Illegal prototype '%s'", RARRAY(v_proto)->ptr[i]);
      }
   }

   rb_iv_set(self, "@dll_name", v_dll);
   rb_iv_set(self, "@function_name", v_proc);
   rb_iv_set(self, "@prototype", v_proto);
   rb_iv_set(self, "@return_type", v_return);

   return self;
}

Instance Attribute Details

#dll_nameObject (readonly)

The name of the DLL that exports the API function

#function_nameObject (readonly)

The name of the function

#prototypeObject (readonly)

The prototype, returned as an array of characters

#return_typeObject (readonly)

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

Instance Method Details

#Win32::APIObject

Calls the function pointer with the given arguments (if any). Note that, while this method will catch some prototype mismatches (raising a TypeError in the process), it is not fulproof. It is ultimately your job to make sure the arguments match the prototype specified in the constructor.

For convenience, nil is converted to NULL, true is converted to TRUE (1) and false is converted to FALSE (0).



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'ext/win32/api.c', line 372

static VALUE api_call(int argc, VALUE* argv, VALUE self){
   VALUE v_proto, v_args, v_arg, v_return;
   Win32API* ptr;
   unsigned long return_value;
   int i = 0;

   struct{
      unsigned long params[16];
   } param;

   Data_Get_Struct(self, Win32API, ptr);

   rb_scan_args(argc, argv, "0*", &v_args);

   v_proto = rb_iv_get(self, "@prototype");

   /* For void prototypes, allow either no args or an explicit nil */
   if(RARRAY(v_proto)->len != RARRAY(v_args)->len){
      char* c = StringValuePtr(RARRAY(v_proto)->ptr[0]);
      if(!strcmp(c, "V")){
         rb_ary_push(v_args, Qnil);
      }
      else{
         rb_raise(rb_eArgError,
            "wrong number of parameters: expected %d, got %d",
            RARRAY(v_proto)->len, RARRAY(v_args)->len
         );
      }
   }

   for(i = 0; i < RARRAY(v_proto)->len; i++){
      v_arg = RARRAY(v_args)->ptr[i];

      /* Convert nil to NULL.  Otherwise convert as appropriate. */
      if(NIL_P(v_arg))
         param.params[i] = (unsigned long)NULL;
      else if(v_arg == Qtrue)
         param.params[i] = TRUE;
      else if(v_arg == Qfalse)
         param.params[i] = FALSE;
      else
         switch(ptr->prototype[i]){
            case _T_LONG:
               param.params[i] = NUM2ULONG(v_arg);
               break;
            case _T_INTEGER:
               param.params[i] = NUM2INT(v_arg);
               break;
            case _T_POINTER:
               if(FIXNUM_P(v_arg)){
                  param.params[i] = NUM2ULONG(v_arg);
               }
               else{
                  StringValue(v_arg);
                  rb_str_modify(v_arg);
                  param.params[i] = (unsigned long)StringValuePtr(v_arg);
               }
               break;
            case _T_CALLBACK:
               ActiveCallback = v_arg;
               param.params[i] = (LPARAM)CallbackFunction;	
               break;
            default:
               param.params[i] = NUM2ULONG(v_arg);
         }
   }

   /* Call the function, get the return value */
   return_value = ptr->function(param);

   /* Return the appropriate type based on the return type specified
    * in the constructor.
    */
   switch(ptr->return_type){
      case _T_INTEGER:
         v_return = INT2NUM(return_value);
         break;
      case _T_LONG:
         v_return = LONG2NUM(return_value);
         break;
      case _T_VOID:
         v_return = Qnil;
         break;
      case _T_POINTER:
         v_return = rb_str_new2((TCHAR*)return_value);
         break;
      default:
         v_return = INT2NUM(0);
   }

   return v_return;
}