Anyone using RegisterClass from Win32API?

M

Michael Davis

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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#endif

#include "ruby.h"
#include "rubysig.h"

static LRESULT CALLBACK
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);
else
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));
}

void
Init_locana_gui_mswin32_ext()
{
VALUE cWin32Ext = rb_define_class("Locana_gui_mswin32_ext", rb_cObject);
rb_define_module_function(cWin32Ext, "get_message_handler", locana_mswin32_GetMessageHandler, 0);
}
 
D

daz

Michael Davis said:
Hello yet again,

Is anyone using RegisterClass from Win32API?


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

http://raa.ruby-lang.org/list.rhtml?name=swin

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).


daz


<aside>
Can't a *n*x guru port swin (to, say 'sux'),
then change vruby calls to SUx::* rather than
SWin::* (when running on *n*x platforms)
so that the FreeRIDErs can use it instead of
fannying around with wxwindows ?
There'd be a real Ruby GUI.
A bit late for that, I suppose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top