# One Way to Write VCRuntime-Independent Extensions on Windows

**URL:** <https://www.ruby-forum.com/t/one-way-to-write-vcruntime-independent-extensions-on-windows/173712>\
**Category:** Ruby\
**Created:** [August 13, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/one-way-to-write-vcruntime-independent-extensions-on-windows/173712 "2009-08-13T18:07:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![night\_stalker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@night\_stalker](https://www.ruby-forum.com/u/night_stalker)\
**Post date:** [August 13, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/one-way-to-write-vcruntime-independent-extensions-on-windows/173712/1 "2009-08-13T18:07:23Z")

</div>

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.
