[Win32utils-devel] Using strtok via win32/api

Park Heesob phasis at gmail.com
Thu Oct 25 08:22:26 EDT 2007


Hi,
----- Original Message ----- 
From: "Daniel Berger" <djberg96 at gmail.com>
To: "win32utils-devel" <win32utils-devel at rubyforge.org>
Sent: Thursday, October 25, 2007 11:44 AM
Subject: [Win32utils-devel] Using strtok via win32/api


> Hi all,
> 
> I'm having a little trouble with strtok:
> 
> require 'win32/api'
> include Win32
> 
> strtok = API.new('strtok', 'PP, 'P', 'msvcrt')
> 
> string = "A string\tof ,,tokens\nand some  more tokens";
> seps = " ,\t\n";
> 
> puts "Tokens:"
> 
> token = strtok.call(string, seps)
> 
> while token
>    puts token
>    token = strtok.call(nil, seps)
> end
> 
> This will work, except on the final call:
> 
> C:\>ruby strtok.rb
> Tokens:
> A
> string
> of
> tokens
> and
> some
> more
> tokens
> C:/ruby/lib/ruby/site_ruby/1.8/windows/api.rb:342:in `call': NULL 
> pointer given (ArgumentError)
>         from C:/ruby/lib/ruby/site_ruby/1.8/windows/api.rb:342:in `call'
>         from 
> C:/ruby/lib/ruby/site_ruby/1.8/windows/msvcrt/string.rb:69:in `strtok'
>         from strtok.rb:13
> 
> I can see why it's doing this, but I'd like to make it work without a 
> begin/rescue, if possible, in order to keep it fast. Any ideas?
> 
> Regards,
> 
> Dan
> 
I think it is a bug.

The source code line #457 of api.c in win32-api module

           v_return = rb_str_new2((TCHAR*)return_value);

shoule be

           v_return = (return_value==0) ? Qnil : rb_str_new2((TCHAR*)return_value);

Regards,

Park Heesob


More information about the win32utils-devel mailing list