Forum: Ruby @@@@HELP......WANTED........NEW TO RUBY........@@@@@

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Beginner (Guest)
on 2006-01-04 15:40
(Received via mailing list)
1. Beginner
 Jan 3, 4:43 pm   show options

Newsgroups: comp.lang.ruby
From: "Beginner" <removed_email_address@domain.invalid> - 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 W. (Guest)
on 2006-01-04 19:12
(Received via mailing list)
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 (Guest)
on 2006-01-04 19:12
(Received via mailing list)
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 K. (Guest)
on 2006-01-04 20:21
(Received via mailing list)
From: "Beginner" <removed_email_address@domain.invalid>
>
>
> 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 (Guest)
on 2006-01-04 20:36
(Received via mailing list)
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 K. (Guest)
on 2006-01-04 22:24
(Received via mailing list)
Hi Mankan,

From: "Beginner" <removed_email_address@domain.invalid>
>
> 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.
[...]
> 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 (Guest)
on 2006-01-04 23:49
(Received via mailing list)
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 K. (Guest)
on 2006-01-05 01:14
(Received via mailing list)
Hi Mankan,

From: "Beginner" <removed_email_address@domain.invalid>
> 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.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 (Guest)
on 2006-01-05 18:44
(Received via mailing list)
Bill,

Thank you very much for this help

rgds,
Mankan
David V. (Guest)
on 2006-01-11 19:59
(Received via mailing list)
Ye gods, PLEASE use sane and at least remotely informative e-mail
subjects.

David V.
Stephen W. (Guest)
on 2006-01-11 20:12
(Received via mailing list)
David V. 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 K. jumped in and was able to solve his problem.

--Steve
This topic is locked and can not be replied to.