Class: Win32::API::Function

Inherits:
Win32::API show all
Defined in:
ext/win32/api.c

Constant Summary

Constants inherited from Win32::API

VERSION

Instance Attribute Summary collapse

Attributes inherited from Win32::API

#dll_name, #effective_function_name, #function_name, #prototype, #return_type

Instance Method Summary collapse

Methods inherited from Win32::API

#call

Constructor Details

#API::Function.new(address, prototype = 'V', return_type = 'L') ⇒ Object

Creates and returns an API::Function object. This object is similar to an API object, except that instead of a character function name you pass a function pointer address as the first argument, and there’s no associated DLL file.

Once you have your API::Function object you can then call it the same way you would an API object.

Example:

require 'win32/api'
include Win32

LoadLibrary = API.new('LoadLibrary', 'P', 'L')
GetProcAddress = API.new('GetProcAddress', 'LP', 'L')

# Play a system beep
hlib = LoadLibrary.call('user32')
addr = GetProcAddress.call(hlib, 'MessageBeep')
func = Win32::API::Function.new(addr, 'L', 'L')
func.call(0)


465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'ext/win32/api.c', line 465

static VALUE func_init(int argc, VALUE* argv, VALUE self){
   Win32API* ptr;
   int i;
   VALUE v_address, v_proto, v_return;

   rb_scan_args(argc, argv, "12", &v_address, &v_proto, &v_return);

   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, "");

   // Convert a nil or empty prototype to 'V' (void) automatically
   if(NIL_P(v_proto) || RARRAY_LEN(v_proto) == 0){
      v_proto = rb_ary_new();
      rb_ary_push(v_proto, rb_str_new2("V"));
   }

   // Set an arbitrary limit of 20 parameters
   if(20 < RARRAY_LEN(v_proto))
      rb_raise(rb_eArgError, "too many parameters: %li\n", RARRAY_LEN(v_proto));

   // Set the default return type to 'L' (DWORD)
   if(NIL_P(v_return))
      v_return = rb_str_new2("L");

   ptr->function = (FARPROC)NUM2SIZET(v_address);

   // Push the numeric prototypes onto our int array for later use.

   for(i = 0; i < RARRAY_LEN(v_proto); i++){
#if defined TAINTING_SUPPORT
      SafeStringValue(RARRAY_PTR(v_proto)[i]);
#endif
      switch(*(char*)StringValuePtr(RARRAY_PTR(v_proto)[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;
         case 'S':
            ptr->prototype[i] = _T_STRING;
            break;
         default:
            rb_raise(cAPIProtoError, "Illegal prototype '%s'",
               StringValuePtr(RARRAY_PTR(v_proto)[i])
            );
      }
   }

   // Store the return type for later use.

   // Automatically convert empty strings or nil to type void.
   if(NIL_P(v_return) || RSTRING_LEN(v_return) == 0){
      v_return = rb_str_new2("V");
      ptr->return_type = _T_VOID;
   }
   else{
#if defined TAINTING_SUPPORT
      SafeStringValue(v_return);
#endif
      switch(*RSTRING_PTR(v_return)){
         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;
         case 'S':
            ptr->return_type = _T_STRING;
            break;
         default:
            rb_raise(cAPIProtoError, "Illegal return type '%s'",
               RSTRING_PTR(v_return)
            );
      }
   }

   rb_iv_set(self, "@address", v_address);
   rb_iv_set(self, "@prototype", v_proto);
   rb_iv_set(self, "@return_type", v_return);

   return self;
}

Instance Attribute Details

#addressObject (readonly)