You are viewing a plain text version of this content. The canonical link for it is here.
Posted to apreq-dev@httpd.apache.org by "David N. Welton" <da...@dedasys.com> on 2002/12/30 19:33:54 UTC

HANDLE from FILE on windows

I have a question - it's not directly related to apreq, but could
potentially influence its API.

In mod_dtcl, there is some Tcl code that, on windows, deals in
HANDLE's, as (I guess - I'm a unix guy) that's the native windows
thing.

The problem is that apreq only gives us a FILE*, and we need a way to
get a HANDLE from it.

We found this on the microsoft site:

http://support.microsoft.com/default.aspx?scid=kb;en-us;99173

but it doesn't look all that great, as they suggest that actually
using the results might not be a good idea.

Any windows people out there with helpful suggestions?

Thanks,
-- 
David N. Welton
   Consulting: http://www.dedasys.com/
     Personal: http://www.dedasys.com/davidw/
Free Software: http://www.dedasys.com/freesoftware/
   Apache Tcl: http://tcl.apache.org/

Re: HANDLE from FILE on windows

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
They are just stating the obvious (true on Unix, too) that buffering and
other behaviors may exist in the FILE * (or the fd) that writes to the raw
handle may overwrite or be overwritten by buffered data, and buffered
data won't show up on a read against the handle.  Nothing you didn't
already expect.

Here's the code from httpd-2.0/server/mpm/winnt/nt_eventlog.c (rev 1.3)
that does exactly what MS says not to...

/* Flush stderr and unset its buffer, then commit and replace stderr.
 * This is typically a noop for Win2K/XP since services with NULL std
 * handles [but valid FILE *'s, oddly enough], but is required
 * for NT 4.0 and to use this code outside of services.
 */
fflush(stderr); 
setvbuf(stderr, NULL, _IONBF, 0); 
_commit(2 /* stderr */); 
fd = _open_osfhandle((long) hPipeWrite, _O_WRONLY | _O_BINARY); 
_dup2(fd, 2); 
_close(fd); 
_setmode(2, _O_BINARY); 
/* hPipeWrite was _close()'ed above, and _dup2()'ed
 * to fd 2 creating a new, inherited Win32 handle.
 * Recover that real handle from fd 2.
 */
hPipeWrite = (HANDLE)_get_osfhandle(2); SetStdHandle(STD_ERROR_HANDLE, hPipeWrite); 

Notice; we flush and remove the buffering from stderr altogether, and then
commit the fd before we even think of touching the handle.  So should you.

One more thing the above code hints at ... you will be looking at a raw
file (all win32 HANDLEs are raw) so you will have to deal with \r\n line
endings yourself, as applicable.

Bill

At 04:47 PM 12/30/2002, you wrote:
>"William A. Rowe, Jr." <wr...@rowe-clan.net> writes:
>
>> First off... with Apache 2.0 all of these problems go away (we told
>> you there was a reason to upgrade :-) The apr_os_file_get() fn will
>> turn any apr_file_t into a win32 handle, or a Unix fd, or whatnot on
>> another platform.
>
>We'll get to it when we get to it.  Meanwhile... I want to try and
>stabilize things on windows as much as possible.  If you want to help
>(more than you have already by providing lots of useful info), more
>committers are welcome;-)
>
>We're also waiting on apreq to be completely ok for 2.0.
>
>> IIRC, you are doing the 1.3 thing.  That reference is correct for
>> turning the fd into a HANDLE, but that isn't what you wanted.  The
>> full syntax, as demonstrated in
>> apache-1.3/src/main/http_main.c(6719) is;
> 
>>   FILE *file;
>>   HANDLE hFile;
>>   hFile = (HANDLE)_get_osfhandle(fileno(file));
>> 
>> Yes, the cast is needed because _get_osfhandle is defined to return
>> a long, iirc.  Best of luck.
>
>Sure, but it says to not actually use it for anything...  Should we
>disregard that?
>
>-- 
>David N. Welton
>   Consulting: http://www.dedasys.com/
>     Personal: http://www.dedasys.com/davidw/
>Free Software: http://www.dedasys.com/freesoftware/
>   Apache Tcl: http://tcl.apache.org/



Re: HANDLE from FILE on windows

Posted by "David N. Welton" <da...@dedasys.com>.
"William A. Rowe, Jr." <wr...@rowe-clan.net> writes:

> First off... with Apache 2.0 all of these problems go away (we told
> you there was a reason to upgrade :-) The apr_os_file_get() fn will
> turn any apr_file_t into a win32 handle, or a Unix fd, or whatnot on
> another platform.

We'll get to it when we get to it.  Meanwhile... I want to try and
stabilize things on windows as much as possible.  If you want to help
(more than you have already by providing lots of useful info), more
committers are welcome;-)

We're also waiting on apreq to be completely ok for 2.0.

> IIRC, you are doing the 1.3 thing.  That reference is correct for
> turning the fd into a HANDLE, but that isn't what you wanted.  The
> full syntax, as demonstrated in
> apache-1.3/src/main/http_main.c(6719) is;
 
>   FILE *file;
>   HANDLE hFile;
>   hFile = (HANDLE)_get_osfhandle(fileno(file));
> 
> Yes, the cast is needed because _get_osfhandle is defined to return
> a long, iirc.  Best of luck.

Sure, but it says to not actually use it for anything...  Should we
disregard that?

-- 
David N. Welton
   Consulting: http://www.dedasys.com/
     Personal: http://www.dedasys.com/davidw/
Free Software: http://www.dedasys.com/freesoftware/
   Apache Tcl: http://tcl.apache.org/

Re: HANDLE from FILE on windows

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
First off... with Apache 2.0 all of these problems go away (we told you
there was a reason to upgrade :-)  The apr_os_file_get() fn will turn any
apr_file_t into a win32 handle, or a Unix fd, or whatnot on another platform.

IIRC, you are doing the 1.3 thing.  That reference is correct for turning
the fd into a HANDLE, but that isn't what you wanted.  The full syntax,
as demonstrated in apache-1.3/src/main/http_main.c(6719) is;

  FILE *file;
  HANDLE hFile;
  hFile = (HANDLE)_get_osfhandle(fileno(file));

Yes, the cast is needed because _get_osfhandle is defined to return a
long, iirc.  Best of luck.

Bill

At 12:33 PM 12/30/2002, David N. Welton wrote:

>I have a question - it's not directly related to apreq, but could
>potentially influence its API.
>
>In mod_dtcl, there is some Tcl code that, on windows, deals in
>HANDLE's, as (I guess - I'm a unix guy) that's the native windows
>thing.
>
>The problem is that apreq only gives us a FILE*, and we need a way to
>get a HANDLE from it.
>
>We found this on the microsoft site:
>
>http://support.microsoft.com/default.aspx?scid=kb;en-us;99173
>
>but it doesn't look all that great, as they suggest that actually
>using the results might not be a good idea.
>
>Any windows people out there with helpful suggestions?
>
>Thanks,
>-- 
>David N. Welton
>   Consulting: http://www.dedasys.com/
>     Personal: http://www.dedasys.com/davidw/
>Free Software: http://www.dedasys.com/freesoftware/
>   Apache Tcl: http://tcl.apache.org/