From: daz Date: 2004-01-19T03:20:00+09:00 Subject: [OT] Re: NULL pointer given, Win32API "Daniel Berger" wrote: > > Oops, I think I spoke too soon. I'm getting false positives. > Consider: > > require "Win32API" > class Foo > GetFileAttributes = Win32API.new("kernel32","GetFileAttributes","P","I") > READONLY = 0x00000001 > > def read_only?(file) > rv = GetFileAttributes.call(file) & READONLY > puts Win32API.message unless Win32API.lasterr == 0 > return true if rv > 0 > false > end > end > > It appears that, even though GetFileAttributes.call works fine (it > returns true or false in experiments), Win32API.lasterr is returning > 127, "The specified procedure could not be found". What's up? For GetFileAttributes(), I'd do something like ... require "Win32API" class Foo GetFileAttributes = Win32API.new("kernel32","GetFileAttributes","P","I") READONLY = 0x00000001 def read_only?(file) rv = GetFileAttributes.call(file) (rv == -1) and raise(RuntimeError, "Win32API.message here") (rv & READONLY)==1 end end says: # Most functions in the Win32 API that set the thread's # last error code value set it when they fail; a few functions # set it when they succeed. On your system, it looks like SetLastError(0) is not (by design) being used after success. (Failure here is indicated by a function return value of 0xFFFFFFFF; the internal 'last error' is left unchanged. That must have been 127 before (or during) the run your example above.) If Win32API doesn't find "GetFileAttributes" ... it appends 'A' and tries "GetFileAttributesA" and the 'real last error code' would then be 127 ("proc not found") unless the second call sets 'last error' to zero on success. Calling GetLastError() is not the way to check for success/failure in Win32 API calls (as you know). Apologies if my trivial example suggested that calling Win32API.lasterr would be any different. Cheers, daz