Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

@@@@HELP......WANTED........NEW TO RUBY........@@@@@

10 views
Skip to first unread message

Beginner

unread,
Jan 4, 2006, 8:33:39 AM1/4/06
to
1. Beginner
Jan 3, 4:43 pm show options

Newsgroups: comp.lang.ruby
From: "Beginner" <sman...@ueidaq.com> - Find messages by this author
Date: 3 Jan 2006 13:43:15 -0800
Local: Tues, Jan 3 2006 4:43 pm
Subject: New to Ruby needs help
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse

Dear Gurus,


I would like to call a Win32API dll from ruby and I did the following:


require 'Win32API'
dwtest = "" * 8
shell1=Win32API.new("my.dll", "myfunc",['P','P','P'],'L')


myfunc takes 3 parameters, &dwerror, &dwnum, &dwtest. Return value is 0

or 1.


I would like to print or test "&dwtest" which is the 3rd parameter, but

I don't know what mistake am I doing, it never prints the right value.
I even did "unpack" using dwtest.unpack('L').


Could you pls modify the above code?


Any help is appreciated.


thx,
Mankan

Stephen Waits

unread,
Jan 4, 2006, 10:24:03 AM1/4/06
to

On Jan 4, 2006, at 5:37 AM, Beginner wrote:

> require 'Win32API'
> dwtest = "" * 8
> shell1=Win32API.new("my.dll", "myfunc",['P','P','P'],'L')
>
> myfunc takes 3 parameters, &dwerror, &dwnum, &dwtest. Return value
> is 0
> or 1.
>

> Could you pls modify the above code?

Mankan,

I think it's a little difficult to understand exactly what you're
doing, with what you've given us. Can you give us the source code to
your 'myfunc' (in whatever language), or, at minimum, a C header file
so we can see how the function's declared?

Additionally, in the future you find you'll get better help if you
use a more meaningful subject line, such as, "help with win32api dll
function call". That way, anyone can look at your subject line and
immediately have an idea as to whether or not they may be able to help.

--Steve

Beginner

unread,
Jan 4, 2006, 10:45:52 AM1/4/06
to
Steve,

Thank you for the reply. I will take care of subject line in future.
Here is myfunc

DWORD dwerror
DWORD dwnum
DWORD dwtest

int myfunc(&dwerror, &dwnum, &dwtest)

Pls let me know if you need any further information.

thx,
Mankan

Bill Kelly

unread,
Jan 4, 2006, 1:18:32 PM1/4/06
to
From: "Beginner" <sma...@ueidaq.com>

>
> I would like to call a Win32API dll from ruby and I did the following:
>
>
> require 'Win32API'
> dwtest = "" * 8
> shell1=Win32API.new("my.dll", "myfunc",['P','P','P'],'L')
>
> myfunc takes 3 parameters, &dwerror, &dwnum, &dwtest. Return value is 0
> or 1.
>
>
> I would like to print or test "&dwtest" which is the 3rd parameter, but
>
> I don't know what mistake am I doing, it never prints the right value.
> I even did "unpack" using dwtest.unpack('L').
[...]

> Here is myfunc
>
> DWORD dwerror
> DWORD dwnum
> DWORD dwtest
>
> int myfunc(&dwerror, &dwnum, &dwtest)

The DWORD's are passed in as references? Does that mean you
are hoping to allow myfunc() to change these values, and see
the changes in Ruby?

My Win32API experience is quite limited, but I'm not sure if
it can handle that.

I know that SWIG ( http://www.swig.org/ ) can do it.

With SWIG, you would make a "mydll.i" file listing the functions
you want to wrap. Something like:

~~~~~~mydll.i~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%module MYDLL
%include "typemaps.i"

%{
#include "mydll.h"
}

typedef long DWORD;

int myfunc( DWORD& OUTPUT, DWORD& OUTPUT, DWORD& OUTPUT );

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

And then run:

swig -includeall -ignoremissing -c++ -ruby mydll.i

And it will generate Ruby binding code to your mydll functions.
Of course, you then have to compile that into a ruby extension
library. This is usually done by making an extconf.rb file:

~~~~~~extconf.rb~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
require 'mkmf'

create_makefile("mydll")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

And then running:

ruby extconf.rb
make # or "nmake" on windows


Hmm... Well obviously this is a bit more involved than using
Win32API. :-/

Hopefully someone more knowledgeable about Win32API can help you
get that working.


Regards,

Bill


Beginner

unread,
Jan 4, 2006, 1:31:02 PM1/4/06
to
Bill,

You are right, I would like to change the parameter content by passing
them as pointer(or reference) and I would like to see their values in
ruby so that based on that value I will be running a for loop.

Like if ruby returns dwtest=3, then next step will be to run a for loop
for 3 times.

thx,
Mankan

Bill Kelly

unread,
Jan 4, 2006, 3:22:49 PM1/4/06
to
Hi Mankan,

[...]


> require 'Win32API'
> dwtest = "" * 8
> shell1=Win32API.new("my.dll", "myfunc",['P','P','P'],'L')
>
> myfunc takes 3 parameters, &dwerror, &dwnum, &dwtest. Return value is 0
> or 1.
>
> I would like to print or test "&dwtest" which is the 3rd parameter, but
>
> I don't know what mistake am I doing, it never prints the right value.
> I even did "unpack" using dwtest.unpack('L').

How about this:

shell1 = Win32API.new("my.dll", "myfunc",['P','P','P'],'L')

dwerror_ = [0].pack("l")
dwnum_ = [0].pack("l")
dwtest_ = [0].pack("l")

result = shell1.call(dwerror_, dwnum_, dwtest_)

dwerror = dwerror_.unpack("l").first
dwnum = dwnum_.unpack("l").first
dwtest = dwtest_.unpack("l").first


Just a guess...

Hope this helps,

Bill


Beginner

unread,
Jan 4, 2006, 4:45:02 PM1/4/06
to
Bill,

That was a great guess...it worked........thank you very much.....but
could you pls explain what the following statement does:

How a pointer is treated in ruby..I mean what happens to this statement


",['P','P','P'],'L')

and does these below statement means:

dwerror_ = [0].pack("l")
dwnum_ = [0].pack("l")
dwtest_ = [0].pack("l")

dwerror = dwerror_.unpack("l").first


dwnum = dwnum_.unpack("l").first
dwtest = dwtest_.unpack("l").first

orelse give me some point to ruby documentation, that is fine.

Bill Kelly

unread,
Jan 4, 2006, 6:12:57 PM1/4/06
to
Hi Mankan,

From: "Beginner" <sma...@ueidaq.com>
>

If you have "ri" installed on your system, from a DOS prompt,
try: ri pack
or: ri unpack

Alternately, goto http://www.ruby-doc.org/core/
and click on the Array class, then on the methods "pack" or
"unpack".

The code above is packing an integer into a string, as a "long",
in native byte order. When this string is passed through
Win32API as a 'P'ointer, it's in the representation of the
DWORD reference your DLL code was expecting. Your DLL code
modifies the DWORD reference, which changes the packed string
in Ruby's memory. We then unpack that string back into an
integer.

Hope this helps,

Regards,

Bill


Beginner

unread,
Jan 5, 2006, 8:29:06 AM1/5/06
to
Bill,

Thank you very much for this help

rgds,
Mankan

David Vallner

unread,
Jan 11, 2006, 12:58:23 PM1/11/06
to
Ye gods, PLEASE use sane and at least remotely informative e-mail subjects.

David Vallner


Stephen Waits

unread,
Jan 11, 2006, 1:11:38 PM1/11/06
to
David Vallner wrote:
> Ye gods, PLEASE use sane and at least remotely informative e-mail subjects.

Hi David,

We mentioned this to Mankan once already, and if you'll follow the
thread, you'll see that he quickly changed his subject to be meaningful.
After that, Bill Kelly jumped in and was able to solve his problem.

--Steve


0 new messages