One Way to Write VCRuntime-Independent Extensions on Windows

L

Lui Core

Solve (rather stupid):
Don't use ruby's includes and libs. Use Win32 API
GetProcAddress(hmodule, strFunction) to locate ruby's C-APIs.

The following is a simple experiment with VC2008 Express and
ruby191-mswin32_90 and ruby191-mingw32:

[ho.c]
--------------------------------------------------------
#include <stdio.h>
#include <windows.h>

void Init_ho() {
HMODULE h = 0;
FARPROC p = 0;

// why they create so many dll names ...?
if(!h) h = GetModuleHandle("msvcrt-ruby191.dll");
if(!h) h = GetModuleHandle("msvcrt60-ruby191.dll");
if(!h) h = GetModuleHandle("msvcrt70-ruby191.dll");
if(!h) h = GetModuleHandle("msvcrt80-ruby191.dll");
if(!h) h = GetModuleHandle("msvcrt90-ruby191.dll");
if(!h) h = GetModuleHandle("msvcrt100-ruby191.dll");
// and many others ...

if(!h) {
printf("failed");
return;
}

p = GetProcAddress(h, "rb_io_check_readable");
// and may others ...

/* if imported functions are the same in ruby1.8 and ruby1.9,
then a cross-version extension ? */

if(p) {
printf("loaded\n");
}
}
--------------------------------------------------------

[ho.def]
--------------------------------------------------------
EXPORTS
Init_ho
--------------------------------------------------------

building commands are (not using extconf.rb)
cl -c ho.c
cl -LD -MD -Feho.so ho.obj user32.lib -link -def:ho.def

some explanations of the commands:
-LD : link dll
-MD : statically link
-Fe : output file name
-link -def:ho.def : make entry point Init_ho searchable for ruby

require 'ho.so' will simply print "loaded" on console.
Both ruby191-mswin32_90 and ruby191-mingw32 can load it properly.
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top