LoadLibrary & GetProcAddress Problem

Hello one and all,

I’m new to Ruby and i have been trying to get this piece of code to call
MessageBox but can’t. I know I am missing something but I don’t know
what it is. Any help with this would be greatly appreciated!

Here is the code so far:

=============================================================================
require ‘Win32API’

LoadLibrary = Win32API.new(‘kernel32’,‘LoadLibrary’,‘P’,‘L’)
GetProcAddress = Win32API.new(‘kernel32’,‘GetProcAddress’,‘LP’,‘L’)

Load = LoadLibrary.call(‘user32.dll’)
Proc = GetProcAddress.call(Load,‘MessageBox’)

Proc.call(0,“Hello World!”,“MessageBox in Ruby”,0)

I know everything is ok except my “Proc.call”. What am I missing?

Huxley Stone wrote in post #1076392:

Hello one and all,

I’m new to Ruby and i have been trying to get this piece of code to call
MessageBox but can’t. I know I am missing something but I don’t know
what it is. Any help with this would be greatly appreciated!

Here is the code so far:

=============================================================================
require ‘Win32API’

LoadLibrary = Win32API.new(‘kernel32’,‘LoadLibrary’,‘P’,‘L’)
GetProcAddress = Win32API.new(‘kernel32’,‘GetProcAddress’,‘LP’,‘L’)

Load = LoadLibrary.call(‘user32.dll’)
Proc = GetProcAddress.call(Load,‘MessageBox’)

Proc.call(0,“Hello World!”,“MessageBox in Ruby”,0)

I know everything is ok except my “Proc.call”. What am I missing?

Why you don’t directly define MessageBox usign Win32API instead of using
LoadLibrary and GetProcAddress?

AFAIK that approach will not work as “Proc” will be a simple void*
(pointer) to the function address and knows nothing about the function
signature (besides you’re overriding Ruby’s own Proc with that)

If you want to use LoadLibrary, GetProcAddress, perhaps you want to
write your program in C, which let you play with function signatures.

If you don’t want to the troubles, use Win32API directly to load the
functions you need.

After all DL is already doing a lot of legwork for you.


Luis L.

Thank you so much for the help Mr. Lavena. You are right, I should’ve
just used Win32API instead of making this so hard on myself. Thanks for
explaining this to me.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs