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

B

Beginner

1. Beginner
Jan 3, 4:43 pm show options

Newsgroups: comp.lang.ruby
From: "Beginner" <[email protected]> - 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
 
S

Stephen Waits

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
 
B

Beginner

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
 
B

Bill Kelly

From: "Beginner said:
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
 
B

Beginner

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
 
B

Bill Kelly

Hi Mankan,

From: "Beginner said:
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
 
B

Beginner

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.
 
B

Bill Kelly

Hi Mankan,

From: "Beginner said:
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.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
 
D

David Vallner

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

David Vallner
 
S

Stephen Waits

David said:
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
 

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,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top