Dave Thomas <Dave / thomases.com> writes:

> This is probably a no-brainer to folks who mess about on Windows every 
> day, but it has me stumped...
> 
> We have some code (part of the installer) that updates the registry:
> 
>     result = RegCreateKey.call(top, subkey, 0, keyClass,
>       REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, retSecurity,
>       retHandle, retResult)
> 
> Where
> 
>   RegCreateKey = Win32API.new("advapi32", "RegCreateKeyEx",
>     ['i','p','i','p','i','i','p','p','p'],'i')
> 
> 
> This works fine in isolation. However, if before calling tihs code we
> call the following routine:
> 
>    def log(*msg)
>      File.open($logfile, "a+") {|f| f.puts *msg}
>    end
> 
> Then the RegCreateKey fails, saying it was passed an invalid
> parameter.
> 
> It's the open, not the puts, that seems to be causing the problem.
> 
> 
> Does anyone have any ideas? Are there any linkages between opening
> files are playing with the registry?

It's hard to know what goes on under the hood of the Win32 OSes, but
the Registry does exist on disk and presumably some sort of file I/O
is involved in e.g. RegCreateKey.

As a C[++] programmer on Win32 platforms I've had trouble with handles
getting lost by the runtime library or OS when my programs mixed
stdlib I/O with Win32 native filesystem calls that bypass e.g. fopen.
In my experience this kind trouble can happen even when each file is
treated consistently, i.e. only with stdlib calls or only with Win32
API calls.  I could certainly be wrong, but your problem smells
familiar.  (Note: my experience has been with the Borland compiler.)

It might be worthwhile to try changing the log method to use
CreateFile, WriteFile and CloseHandle.

/Lew
-- 
Lew Perin / perin / mail.med.cornell.edu / perin / acm.org
www.panix.com/~perin/