diff --git a/ext/fiddle/function.c b/ext/fiddle/function.c index d15a54bfa6..274d181d17 100644 --- a/ext/fiddle/function.c +++ b/ext/fiddle/function.c @@ -378,9 +378,10 @@ function_call(int argc, VALUE argv[], VALUE self) { int errno_keep = errno; #if defined(_WIN32) + DWORD error = WSAGetLastError(); int socket_error = WSAGetLastError(); rb_funcall(mFiddle, rb_intern("win32_last_error="), 1, - INT2NUM(errno_keep)); + ULONG2NUM(error)); rb_funcall(mFiddle, rb_intern("win32_last_socket_error="), 1, INT2NUM(socket_error)); #endif diff --git a/test/fiddle/test_function.rb b/test/fiddle/test_function.rb index a5284e093a..ea5f054dd2 100644 --- a/test/fiddle/test_function.rb +++ b/test/fiddle/test_function.rb @@ -9,6 +9,7 @@ module Fiddle def setup super Fiddle.last_error = nil + Fiddle.win32_last_error = nil if WINDOWS end def test_default_abi @@ -94,6 +95,19 @@ module Fiddle refute_nil Fiddle.last_error end + if WINDOWS + def test_win32_last_error + kernel32 = dlopen('kernel32') + args = [kernel32['SetLastError'], [TYPE_LONG], TYPE_VOID] + args << Function::STDCALL if Function.const_defined?(:STDCALL) + set_last_error = Function.new(*args) + assert_nil(Fiddle.win32_last_error) + n = 1 << 29 | 1 + set_last_error.call(n) + assert_equal(n, Fiddle.win32_last_error) + end + end + def test_strcpy f = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP) buff = +"000"