Class: Win32::API::Function
- Inherits:
-
Win32::API
- Object
- Win32::API
- Win32::API::Function
- Defined in:
- ext/win32/api.c
Constant Summary
Constants inherited from Win32::API
Instance Attribute Summary collapse
- #address ⇒ Object readonly
Attributes inherited from Win32::API
#dll_name, #effective_function_name, #function_name, #prototype, #return_type
Instance Method Summary collapse
-
#API::Function.new(address, prototype = 'V', return_type = 'L') ⇒ Object
constructor
Creates and returns an API::Function object.
Methods inherited from Win32::API
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)
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 464 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 |
# File 'ext/win32/api.c', line 432
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: %d\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)NUM2LONG(v_address);
// Push the numeric prototypes onto our int array for later use.
for(i = 0; i < RARRAY_LEN(v_proto); i++){
SafeStringValue(RARRAY_PTR(v_proto)[i]);
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{
SafeStringValue(v_return);
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;
}
|