noreply at rubyforge.org
2008-Jun-18 23:22 UTC
[Win32utils-devel] [ win32utils-Bugs-20722 ] Windows::Error.get_last_error only returns the first character (PATCH)
Bugs item #20722, was opened at 2008-06-18 15:16 You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=411&aid=20722&group_id=85 Category: windows-pr Group: Code Status: Open Resolution: None Priority: 3 Submitted By: John Whitley (whitley) Assigned to: Nobody (None) Summary: Windows::Error.get_last_error only returns the first character (PATCH) Initial Comment: Platform/version info: Windows XP SP3, ruby 1.8.6-p114, windows-pr-0.8.6 Repro code: require ''windows/error'' class ErrTest; include Windows::Error; end et = ErrTest.new puts et.get_last_error(100) Expected result (on english Win XP SP3): Cannot create another system semaphore. Actual result: C It seems that FormatMessage is being bound to the widechar variant (FormatMessageW) instead of the narrow variant (FormatMessageA). I''ve included a simple patch, below. For test_error.rb, I''d recommend adding a simple test that verifies the length of the returned message for a known error code is greater than two characters or so; that should work around localization issues causing different messages to be returned. Proposed patch: --- error.rb.orig 2008-06-18 15:04:38.768967600 -0700 +++ error.rb 2008-06-18 15:03:50.489880600 -0700 @@ -401,7 +401,8 @@ API.new(''GetLastError'', ''V'', ''L'') API.new(''SetLastError'', ''L'', ''V'') API.new(''SetErrorMode'', ''I'', ''I'') - API.new(''FormatMessage'', ''LLLLPLP'', ''L'') + API.new(''FormatMessageA'', ''LLLLPLP'', ''L'') + FormatMessage = FormatMessageA begin API.new(''SetLastErrorEx'', ''LL'', ''V'', ''user32'') ---------------------------------------------------------------------->Comment By: John Whitley (whitley)Date: 2008-06-18 16:22 Message: Actually, I rescind my patch above. The deeper problem seems to lie in win32-api, api.c, this code: // The order of ''A'' and ''W'' is reversed if $KCODE is set to ''UTF8''. if(!strcmp(rb_get_kcode(), "UTF8")){ first = "W"; second = "A"; } W functions accept/return UTF16 not UTF8, which breaks the rest of the win32-* modules hard, as the bug above exhibits. For example, Win32::Events are now created by passing UTF8 Ruby string data to an interface expecting UTF16 (see the name parameter). Setting $KCODE to UTF8 should not change the interface of these functions, as it breaks unrelated client code. As it stands ''W'' functions always require special conversion handling, and should be non-default. ---------------------------------------------------------------------- You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=411&aid=20722&group_id=85
Daniel Berger
2008-Jun-19 05:11 UTC
[Win32utils-devel] [ win32utils-Bugs-20722 ] Windows::Error.get_last_error only returns the first character (PATCH)
On Wed, Jun 18, 2008 at 5:22 PM, <noreply at rubyforge.org> wrote: <snip>>>Comment By: John Whitley (whitley) > Date: 2008-06-18 16:22 > > Message: > Actually, I rescind my patch above. The deeper problem > seems to lie in win32-api, api.c, this code: > > // The order of ''A'' and ''W'' is reversed if $KCODE is set > to ''UTF8''. > if(!strcmp(rb_get_kcode(), "UTF8")){ > first = "W"; > second = "A"; > } > > W functions accept/return UTF16 not UTF8, which breaks the > rest of the win32-* modules hard, as the bug above exhibits. > For example, Win32::Events are now created by passing UTF8 > Ruby string data to an interface expecting UTF16 (see the > name parameter). > > Setting $KCODE to UTF8 should not change the interface of > these functions, as it breaks unrelated client code. As it > stands ''W'' functions always require special conversion > handling, and should be non-default.Ow. Well, I _did_ ask about this when I first implemented it. I guess it wasn''t such a good idea after all. Suggestions? Fixable? Or remove it completely? Dan
Berger, Daniel
2008-Jun-19 20:03 UTC
[Win32utils-devel] [ win32utils-Bugs-20722 ]Windows::Error.get_last_error only returns the firstcharacter (PATCH)
Hi, Responding to myself here...> > W functions accept/return UTF16 not UTF8, which breaks the > rest of the > > win32-* modules hard, as the bug above exhibits. > > For example, Win32::Events are now created by passing UTF8 Ruby > > string data to an interface expecting UTF16 (see the name > parameter). > > > > Setting $KCODE to UTF8 should not change the interface of these > > functions, as it breaks unrelated client code. As it stands ''W'' > > functions always require special conversion handling, and should be > > non-default. > > Ow. > > Well, I _did_ ask about this when I first implemented it. I > guess it wasn''t such a good idea after all. > > Suggestions? Fixable? Or remove it completely?Actually, I don''t know that I agree with John (the OP). I think it just goes with the territory that if you''re going to use $KCODE/UTF8 then you have to deal with the wide character conversions on your end. That being said, I can''t say that I''ve tested many of our libraries with $KCODE set to see if they work as expected. As for get_last_error, I think it just requires this change: - buf.split(0.chr).first.chomp rescue ''Unknown error'' + buf.split(0.chr).join[ /^[^\0\r\n]*/ ] rescue ''Unknown error'' Thoughts? Thanks, Dan This communication is the property of Qwest and may contain confidential or privileged information. Unauthorized use of this communication is strictly prohibited and may be unlawful. If you have received this communication in error, please immediately notify the sender by reply e-mail and destroy all copies of the communication and any attachments.