[Win32utils-devel] [ win32utils-Bugs-20722 ] Windows::Error.get_last_error only returns the first character (PATCH)

noreply at rubyforge.org noreply at rubyforge.org
Wed Jun 18 19:22:26 EDT 2008


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


More information about the win32utils-devel mailing list