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)
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 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 |
# File 'ext/win32/api.c', line 397
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(v_proto)->len == 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(v_proto)->len)
rb_raise(rb_eArgError, "too many parameters: %d\n", RARRAY(v_proto)->len);
// 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(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;
case 'S':
ptr->prototype[i] = _T_STRING;
break;
default:
rb_raise(cAPIProtoError, "Illegal prototype '%s'",
StringValuePtr(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;
case 'S':
ptr->return_type = _T_STRING;
break;
default:
rb_raise(cAPIProtoError, "Illegal return type '%s'",
RSTRING(v_return)->ptr
);
}
}
rb_iv_set(self, "@address", v_address);
rb_iv_set(self, "@prototype", v_proto);
rb_iv_set(self, "@return_type", v_return);
return self;
}
|