[ANN] sys-win32etc 0.0.2

Hi all,

I’m happy to announce the release of sys-win32etc 0.0.2. This is an
ALPHA release.

What is it? This is my attempt to port the Etc module to Win32 systems
(well, NT, 2000 and XP anyway). If all goes well, I hope to get Matz to
include this as part of core Ruby.

HELP WANTED! See “Where I need help” below.

Changes since 0.0.1

···

===================

  • Replaced most calls to NetQueryDisplayInformation() with less unicode
    friendly, but easier, method calls.
  • The group() method now returns local group info as well (only global
    groups
    were returned before).
  • All methods except getlogin() now accept a server name as an optional
    argument. If provided, lookups are done against that server instead
    of the
    local machine.
  • Calling group() or passwd() in non-block form now returns nil, as it
    does
    on *nix.
  • Test suite and documentation changes to reflect changes in code.

Synopsis

require “win32etc”

puts "Login: " + Win32Etc.getlogin

p Win32Etc.getpwnam(“Guest”,"\some_server")
p Win32Etc.getpwuid(512)
p Win32Etc.getgrnam(“Guests”)
p Win32Etc.getgrgid(500,"\some_server")

Win32Etc.passwd{ |s|
p s
}

Win32Etc.group("\some_server"){ |g|
p g
}

Where I need help

Memory leaks. Don’t panic - these are only an issue if you do repeated
calls in a tight loop of some kind. Single calls are harmless. I don’t
know where they’re coming from or what I can do about it.

Remote server testing - I don’t have a machine to test against for
calling a method against a remote server instead of the local machine.
Testing much appreciated on that front.

Insight into why I should or shouldn’t use NetQueryDisplayInformation()
instead of NetGroupEnum(), etc. Hopefully, something beyond, “Because
the MSDN site says so”.

For more information

For general info on porting *nix to Windows see
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnucmg/html/ucmgch09.asp

See http://msdn.microsoft.com/library/default.asp to search for API
info.

Regards,

Dan

“Daniel Berger” djberge@qwest.com wrote in

  • Replaced most calls to NetQueryDisplayInformation() with less unicode
    friendly, but easier, method calls.

But I think this may be problematic because win32 API (e.g. NetUserEnum())
expects unicode.

  • All methods except getlogin() now accept a server name as an optional
    argument. If provided, lookups are done against that server instead
    of the local machine.

This part did not work for me.

Synopsis

require “win32etc”

puts "Login: " + Win32Etc.getlogin

p Win32Etc.getpwnam(“Guest”,“\some_server”)
^^^^^^^^^^^^
I think this should be “\\someserver”

Remote server testing - I don’t have a machine to test against for
calling a method against a remote server instead of the local machine.
Testing much appreciated on that front.

Like I said earlier, this did not work for me. I guess this is because of
lines such as

            szServer = (LPCWSTR)STR2CSTR(server);

in your code. I think STR2CSTR returns regular C strings (char *) not
unicode (wchar_t *) strings. Is there a way to convert regular strings to
unicode in Ruby ?

Let me know if you need to see my C code :wink:

Insight into why I should or shouldn’t use NetQueryDisplayInformation()
instead of NetGroupEnum(), etc. Hopefully, something beyond, "Because

Sorry, I have no idea why … can’t help you there.

By the way the link in RAA to your download is wrong, it should be:

http://prdownloads.sourceforge.net/ruby-sysutils/sys-win32etc-0.0.2.zip?download

Hope that helps.
– shanko

“Shashank Date” sdate@everestkc.net wrote in message

Like I said earlier, this did not work for me. I guess this is because of
lines such as

            szServer = (LPCWSTR)STR2CSTR(server);

in your code. I think STR2CSTR returns regular C strings (char *) not
unicode (wchar_t *) strings. Is there a way to convert regular strings to
unicode in Ruby ?

Ok, I changed your code
from :

if(server != Qnil){
szServer = (LPCWSTR)STR2CSTR(server);
}
else{
szServer = NULL;
}

to
if(server != Qnil){
wchar_t wszTemp[MAX_PATH];
mbstowcs(wszTemp, STR2CSTR(server), MAX_PATH);
szServer = (LPCWSTR)wszTemp;
}
else{
szServer = NULL;
}

And now it is working fine except this line:

p Win32Etc.getgrgid(500,“\\MYMACHINE”)

It gives error as follows:

C:/atest/tst_win_etc.rb:10:in `getgrgid’: can’t find group for 500
(ArgumentError)
from C:/atest/tst_win_etc.rb:10

What’s with group 500 ? I tried 500, 501, 510, etc but to no vail.

HTH,
– shanko

“Daniel Berger” djberg96@hotmail.com

Hmm…‘\’ should work with single quotes. Please try and let me
know. If so, I’ll update the docs.

Did not work with single quotes either. Had to use double-backslash twice.

Thank you very much for the feedback. Any idea on the memory leaks?
To see what I mean, try this bit of code and pull up the Task Manager
to watch the memory usage:

while 1
Win32Etc.group{ |g|
p g
}
sleep 3
end

Not that you would do this in real code, but it demonstrates the
problem.

Yes, I could recreate the problem but have not figured a way to avoid
it. Will take a look at it over the wekk-end (if possible).

– shanko

“Shashank Date” sdate@everestkc.net wrote in message

And now it is working fine except this line:

p Win32Etc.getgrgid(500,“\\MYMACHINE”)

It gives error as follows:

C:/atest/tst_win_etc.rb:10:in `getgrgid’: can’t find group for 500
(ArgumentError)
from C:/atest/tst_win_etc.rb:10

What’s with group 500 ? I tried 500, 501, 510, etc but to no vail.

Sorry ! Posted too early … should have tried 513 before giving up :wink:

Now everything is working great !

Good work, Daniel.
Thanks a lot.
– shanko

BTW, are you the same person who has interfaced VC++ to
Lego mindstorms? http://www.kyb.tuebingen.mpg.de/~berger

– shanko

Hi,

···

----- Original Message -----
From: “Shashank Date” sdate@everestkc.net
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, August 30, 2003 3:40 PM
Subject: Re: [ANN] sys-win32etc 0.0.2

“Daniel Berger” djberg96@hotmail.com

Hmm…‘\’ should work with single quotes. Please try and let me
know. If so, I’ll update the docs.

Did not work with single quotes either. Had to use double-backslash twice.

Thank you very much for the feedback. Any idea on the memory leaks?
To see what I mean, try this bit of code and pull up the Task Manager
to watch the memory usage:

while 1
Win32Etc.group{ |g|
p g
}
sleep 3
end

Not that you would do this in real code, but it demonstrates the
problem.

Yes, I could recreate the problem but have not figured a way to avoid
it. Will take a look at it over the wekk-end (if possible).

The memory leak is due to memory allocation of struct like these

VALUE gstruct = rb_struct_new(Group);

rb_struct_aset(gstruct,INT2NUM(0),rb_str_new2(dest));

A simple and stupid solution is replace

     if(rb_block_given_p()){
        rb_yield(gstruct);
     }

with
if(rb_block_given_p()){
rb_ensure(rb_yield,gstruct,obj_free,gstruct);
}

and define obj_free like this

static VALUE obj_free(VALUE obj)
{
rb_gc();
return Qnil;
}

Park Heesob