[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Beginner

1/4/2006 1:34:00 PM

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

10 Answers

Stephen Waits

1/4/2006 3:24:00 PM

0


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

1/4/2006 3:46:00 PM

0

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

1/4/2006 6:19:00 PM

0

From: "Beginner" <smankan@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... ) 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

1/4/2006 6:31:00 PM

0

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

1/4/2006 8:23:00 PM

0

Hi Mankan,

From: "Beginner" <smankan@ueidaq.com>
>
> 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.
[...]
> 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

1/4/2006 9:45:00 PM

0

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

1/4/2006 11:13:00 PM

0

Hi Mankan,

From: "Beginner" <smankan@ueidaq.com>
>
> 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.

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

Alternately, goto http://www.ruby-doc...
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

1/5/2006 1:29:00 PM

0

Bill,

Thank you very much for this help

rgds,
Mankan

David Vallner

1/11/2006 5:58:00 PM

0

Ye gods, PLEASE use sane and at least remotely informative e-mail subjects.

David Vallner


Stephen Waits

1/11/2006 6:12:00 PM

0

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