Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Win32::GetLastError fails when first called #8896

Closed
p5pRT opened this issue May 11, 2007 · 8 comments
Closed

Win32::GetLastError fails when first called #8896

p5pRT opened this issue May 11, 2007 · 8 comments

Comments

@p5pRT
Copy link

p5pRT commented May 11, 2007

Migrated from rt.perl.org#42925 (status was 'resolved')

Searchable as RT42925$

@p5pRT
Copy link
Author

p5pRT commented May 11, 2007

From nobull@cpan.org

This is a bug report for perl from nobull@​cpan.org,
generated with the help of perlbug 1.35 running under perl v5.8.8.


The first time Win32​::GetLastError is called it always returns 2 ("The
system cannot find the file specified"). (This in turn this breaks
Win32​::TieRegistry etc but the underlying problem is very simple to
illustrate).

Note​: Win32​::GetLastError is _core_ it's not part of the Win32 module, and
indeed if you actually load Win32.pm you will _not_ see this error manifest.

#!perl
use strict;
use warnings;
#Win32​::GetLastError(); # Uncomment to see correct error below.
kill 0,1; # Cause Windows error 6 "The handle is invalid"
my $e = Win32​::GetLastError();
print $e," ",Win32​::FormatMessage($e),"\n";
__END__



Flags​:
  category=core
  severity=medium


Site configuration information for perl v5.8.8​:

Configured by SYSTEM at Tue Jan 23 15​:57​:26 2007.

Summary of my perl5 (revision 5 version 8 subversion 8) configuration​:
  Platform​:
  osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread
  uname=''
  config_args='undef'
  hint=recommended, useposix=true, d_sigaction=undef
  usethreads=define use5005threads=undef useithreads=define
usemultiplicity=define
  useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
  use64bitint=undef use64bitall=undef uselongdouble=undef
  usemymalloc=n, bincompat5005=undef
  Compiler​:
  cc='cl', ccflags ='-nologo -GF -W3 -MD -Zi -DNDEBUG -O1 -DWIN32
-D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT -DNO_HASH_SEED

-DUSE_SITECUSTOMIZE -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO
-DPERL_MSVCRT_READFIX',
  optimize='-MD -Zi -DNDEBUG -O1',
  cppflags='-DWIN32'
  ccversion='12.00.8804', gccversion='', gccosandvers=''
  intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
  d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10
  ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='__int64',
lseeksize=8
  alignbytes=8, prototype=define
  Linker and Libraries​:
  ld='link', ldflags ='-nologo -nodefaultlib -debug -opt​:ref,icf
-libpath​:"C​:\Perl\lib\CORE" -machine​:x86'
  libpth=\lib
  libs= oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib

oleaut32.lib netapi32.lib uuid.lib ws2_32.lib mpr.lib winmm.lib
version.lib odbc32.lib odbccp32.lib msvcrt.lib
  perllibs= oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib

oleaut32.lib netapi32.lib uuid.lib ws2_32.lib mpr.lib winmm.lib
version.lib odbc32.lib odbccp32.lib msvcrt.lib
  libc=msvcrt.lib, so=dll, useshrplib=yes, libperl=perl58.lib
  gnulibc_version=''
  Dynamic Linking​:
  dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
  cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib -debug
-opt​:ref,icf -libpath​:"C​:\Perl\lib\CORE" -machine​:x86'

Locally applied patches​:
  ACTIVEPERL_LOCAL_PATCHES_ENTRY
  Iin_load_module moved for compatibility with build 806
  Avoid signal flag SA_RESTART for older versions of HP-UX
  PerlEx support in CGI​::Carp
  Less verbose ExtUtils​::Install and Pod​::Find
  Patch for CAN-2005-0448 from Debian with modifications
  Rearrange @​INC so that 'site' is searched before 'perl'
  Partly reverted 24733 to preserve binary compatibility
  29930 win32.c typo in #define MULTIPLICITY
  29868 win32_async_check() can still loop indefinitely
  29690,29732 ANSIfy the PATH environment variable on Windows
  29689 Add error handling to win32_ansipath
  29675 Use short pathnames in $^X and @​INC
  29607,29676 allow blib.pm to be used for testing Win32 module
  29605 Implement killpg() for MSWin32
  29598 cwd() to return the short pathname
  29597 let readdir() return the alternate filename
  29590 Don't destroy the Unicode system environment on Perl startup
  29528 get ext/Win32/Win32.xs to compile on cygwin
  29509,29510,29511 Move Win32​::* functions into Win32 module
  29483 Move Win32 from win32/ext/Win32 to ext/Win32
  29481 Makefile.PL changes to compile Win32.xs using cygwin
  28671 Define PERL_NO_DEV_RANDOM on Windows
  28376 Add error checks after execing PL_cshname or PL_sh_path
  28305 Pod​::Html should not convert \"foo\" into ``foo''
  27833 Change anchor generation in Pod​::Html for '=item item 2'
  27832,27847 fix Pod​::Html​::depod() for multi-line strings
  27736 Make perl_fini() run with Sun WorkShop compiler
  27719 Document the functions htmlify() and anchorify() in Pod​::Html
  27619 Bug in Term​::ReadKey being triggered by a bug in Term​::ReadLine
  27549 Move DynaLoader.o into libperl.so
  27528 win32_pclose() error exit doesn't unlock mutex
  27527 win32_async_check() can loop indefinitely
  27515 ignore directories when searching @​INC
  27359 Fix -d​:Foo=bar syntax
  27210 Fix quote typo in c2ph
  27203 Allow compiling swigged C++ code
  27200 Make stat() on Windows handle trailing slashes correctly
  27194 Get perl_fini() running on HP-UX again
  27133 Initialise lastparen in the regexp structure
  27061 L<PerlIO> and Pod​::Html
  27034 Avoid \"Prototype mismatch\" warnings with autouse
  26970 Make Passive mode the default for Net​::FTP
  26921 Avoid getprotobyname/number calls in IO​::Socket​::INET
  26897,26903 Make common IPPROTO_* constants always available
  26670 Make '-s' on the shebang line parse -foo=bar switches
  26637 Make Borland and MinGW happy with change 26379
  26536 INSTALLSCRIPT versus INSTALLDIRS
  26379 Fix alarm() for Windows 2003
  26087 Storable 0.1 compatibility
  25861 IO​::File performace issue
  25084 long groups entry could cause memory exhaustion
  24699 ICMP_UNREACHABLE handling in Net​::Ping


@​INC for perl v5.8.8​:
  C​:/Perl/site/lib
  C​:/Perl/lib
  .


Environment for perl v5.8.8​:
  HOME (unset)
  LANG (unset)
  LANGUAGE (unset)
  LD_LIBRARY_PATH (unset)
  LOGDIR (unset)
  PATH=C​:\Program Files\Windows Resource

Kits\Tools\;C​:\PROGRA1\Borland\Delphi5\Projects\Bpl;C​:\PROGRA1\Borland\Delphi5\Bin;C​:\Perl\site\bin;C​:\Perl\bin;C​:\WINDOWS\

system32;C​:\WINDOWS;C​:\WINDOWS\System32\Wbem;;C​:\MSSQL\BINN;C​:\Program
Files\Common Files\Adaptec Shared\System;C​:\Program

Files\Gemplus\GemSafe Libraries User\BIN;C​:\PROGRA~1\Gemplus\GAC;C​:\Program
Files\Support Tools\;C​:\Program Files\Microsoft

SQL Server\80\Tools\Binn\;C​:\Program Files\Microsoft SQL
Server\90\Tools\binn\;C​:\Program Files\Microsoft SQL

Server\90\DTS\Binn\;C​:\Program Files\Microsoft SQL
Server\90\Tools\Binn\VSShell\Common7\IDE\;C​:\Program Files\Microsoft

Visual Studio 8\Common7\IDE\PrivateAssemblies\;R​:\bin
  PERL_BADLANG (unset)
  SHELL (unset)

@p5pRT
Copy link
Author

p5pRT commented May 11, 2007

From @jandubois

On Fri, 11 May 2007, Brian McCauley (via RT) wrote​:

-----------------------------------------------------------------
The first time Win32​::GetLastError is called it always returns 2 ("The
system cannot find the file specified"). (This in turn this breaks
Win32​::TieRegistry etc but the underlying problem is very simple to
illustrate).

Note​: Win32​::GetLastError is _core_ it's not part of the Win32 module, and
indeed if you actually load Win32.pm you will _not_ see this error manifest.

Thank you for the bug report! All the Win32​::* functions have been moved
to the Win32 module, and the core only contains forwarding stubs for those
functions that traditionally did not require a "use Win32" statement.

This forwarding stub doesn't preserve the value of the last error, which
is the reason that you see the wrong value when the code goes through the
forwarder. The attached patch fixes that.

#!perl
use strict;
use warnings;
#Win32​::GetLastError(); # Uncomment to see correct error below.

I disagree with the comment above​: There is nothing in the documentation
of the kill() function that promises specific values in Win32​::GetLastError()
when the function fails. Indeed you'll see the following error with the
latest implementation changes to the kill function​:

87 The parameter is incorrect.

But that is just a side-note; Win32​::GetLastError() should of course return
consistent results even when called via the forwarder.

kill 0,1; # Cause Windows error 6 "The handle is invalid"
my $e = Win32​::GetLastError();
print $e," ",Win32​::FormatMessage($e),"\n";
__END__

Cheers,
-Jan

Inline Patch
--- ext/Win32CORE/Win32CORE.c.orig	Thu Feb 22 02:12:25 2007
+++ ext/Win32CORE/Win32CORE.c	Fri May 11 16:45:27 2007
@@ -15,7 +15,9 @@
 forward(pTHX_ const char *function)
 {
     dXSARGS;
+    DWORD err = GetLastError();
     Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpvn("Win32",5), newSVnv(0.27));
+    SetLastError(err);
     SPAGAIN;
     PUSHMARK(SP-items);
     call_pv(function, GIMME_V);
End of Patch.

@p5pRT
Copy link
Author

p5pRT commented May 11, 2007

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented May 14, 2007

From @steve-m-hay

Jan Dubois wrote​:

On Fri, 11 May 2007, Brian McCauley (via RT) wrote​:

-----------------------------------------------------------------
The first time Win32​::GetLastError is called it always returns 2 ("The
system cannot find the file specified"). (This in turn this breaks
Win32​::TieRegistry etc but the underlying problem is very simple to
illustrate).

Note​: Win32​::GetLastError is _core_ it's not part of the Win32 module, and
indeed if you actually load Win32.pm you will _not_ see this error manifest.

Thank you for the bug report! All the Win32​::* functions have been moved
to the Win32 module, and the core only contains forwarding stubs for those
functions that traditionally did not require a "use Win32" statement.

This forwarding stub doesn't preserve the value of the last error, which
is the reason that you see the wrong value when the code goes through the
forwarder. The attached patch fixes that.

Thanks, applied as #31214 (with a test also applied as #31215).

--

@p5pRT
Copy link
Author

p5pRT commented May 14, 2007

From @jandubois

On Mon, 14 May 2007, Steve Hay wrote​:

Jan Dubois wrote​:

This forwarding stub doesn't preserve the value of the last error,
which is the reason that you see the wrong value when the code goes
through the forwarder. The attached patch fixes that.

Thanks, applied as #31214 (with a test also applied as #31215).

I would make sure that Win32 isn't already loaded when you call
Win32​::GetLastError(). Suggested patch attached.

Cheers,
-Jan

Inline Patch
--- ext/Win32CORE/t/win32core.t.orig	Mon May 14 09:58:14 2007
+++ ext/Win32CORE/t/win32core.t	Mon May 14 15:10:10 2007
@@ -10,11 +10,17 @@
 	}
     }
 
-    plan tests => 2;
+    plan tests => 4;
 };
 use_ok( "Win32CORE" );
 
+# Make sure the Win32 is not yet loaded
+ok(!defined &Win32::ExpandEnvironmentStrings);
+
 # [perl #42925] - Loading Win32::GetLastError() via the forwarder function
 # should not affect the last error being retrieved
 $^E = 42;
 is(Win32::GetLastError(), 42, 'GetLastError() works on the first call');
+
+# Now all Win32::* functions should be loaded
+ok(defined &Win32::ExpandEnvironmentStrings);
End of Patch

@p5pRT
Copy link
Author

p5pRT commented May 15, 2007

From @steve-m-hay

Jan Dubois wrote​:

On Mon, 14 May 2007, Steve Hay wrote​:

Jan Dubois wrote​:

This forwarding stub doesn't preserve the value of the last error,
which is the reason that you see the wrong value when the code goes
through the forwarder. The attached patch fixes that.
Thanks, applied as #31214 (with a test also applied as #31215).

I would make sure that Win32 isn't already loaded when you call
Win32​::GetLastError(). Suggested patch attached.

Thanks, applied as #31219.

--

@p5pRT
Copy link
Author

p5pRT commented May 15, 2007

From @steve-m-hay

Now resolved in bleadperl by changes #31214, 31215 and 31219.

@p5pRT p5pRT closed this as completed May 15, 2007
@p5pRT
Copy link
Author

p5pRT commented May 15, 2007

@steve-m-hay - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant