Anyone using RegisterClass from Win32API?

Hello yet again,

Is anyone using RegisterClass from Win32API? To Register a class you need to supply the pointer to a callback function. To achieve this, I created a small class in C to provide me with the pointer to a function that I can use in my Ruby code for obtaining the callbacks from windows. This works great on Windows 2000 and 95 when using cygwin, but fails on XP, and will not likely work for those installing the windows version of Ruby. The C code to do this is provided below, incase anyone is interested. I am looking for a more creative way to provide RegisterClass with a function pointer that does not involve any C code. Any suggestions are greatly appreciated. Is there a way to get the function pointer of a Ruby method from within Ruby that DLL functions can call back to?

Thanks, Michael Davis

Here is the small C class for getting the pointer to a function that windows calls. I just have to create a Ruby method called Win32API_MessageHandler(hwnd, iMsg, wParam, lParam) and voila, my Ruby applet can process window events.

#if !defined _MSC_VER && !defined NT
#include <windows.h>
#include <stdio.h>

#include “ruby.h”
#include “rubysig.h”

locana_mswin32_MessageHandler(hwnd, uMsg, wParam, lParam) // This is a pass through function to the Ruby method called ‘Win32API_MessageHandler’ that will process windows messages
HWND hwnd;
UINT uMsg;
WPARAM wParam;
LPARAM lParam;
VALUE rc = Qnil;
ID proc_id;
proc_id = (ID)GetWindowLong(hwnd, (int)GWL_USERDATA); // let’s see if the user has registered a proc name
//printf(“Win32API_MessageHandler(%d, …) - proc_id:’%d’\n”, (int)hwnd, proc_id);
if (!proc_id)
proc_id = rb_intern(“Win32API_MessageHandler”); // the default message handler is a Ruby method that you must create called Win32API_MessageHandler
if (proc_id)
rc = rb_funcall(rb_cObject, proc_id, 4, INT2NUM((long)hwnd), INT2NUM((long)uMsg), INT2NUM((long)wParam), INT2NUM((long)lParam));
if (rc == Qnil)
return DefWindowProc(hwnd,uMsg,wParam,lParam);
return (LRESULT)NUM2LONG(rc);

static VALUE
locana_mswin32_GetMessageHandler() // RegisterClass needs the C function used for processing messages
return INT2NUM((unsigned long)(locana_mswin32_MessageHandler));

VALUE cWin32Ext = rb_define_class(“Locana_gui_mswin32_ext”, rb_cObject);
rb_define_module_function(cWin32Ext, “get_message_handler”, locana_mswin32_GetMessageHandler, 0);

“Michael Davis” wrote in message

Hello yet again,

Is anyone using RegisterClass from Win32API?

Yes, swin is.
You seem to be rewriting chunks of it :slight_smile:

The C source for swin is very readable.
A compile of this gives the DLL base which
vruby calls (vruby is all in Ruby).

I’d bang-on about it more but it’s not
multi-platform so we have to use it
without the lights on.

Huge compliments to the authors of
visual-u-ruby (tempname - no mis-spelling).