|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
DisclaimerI'm working on this essay. However, the information that is already here is sufficiently useful that I decided to post in, in spite of the fact that I've only covered a small part of handles. Over the next few months, I'll be enhancing it. So be patient; more is coming. File HandlesI have a There are several representations of handles for files at the application level. There are the file handles delivered by the C library, there are They are not necessarily interchangeable on an instant's notice, because of issues such as buffering. Thus if you reach into a More typically, you have a fresh, newly-opened handle of one type and wish to now associate it with a representation more suitable for your task. For example, the C library function C library "handles" are small integers that index into a table in the C runtime library. The C library has traditionally limited the number of simultaneously open files to some very restrictive number of handles, such as 16. This is no longer true in Win32. The C library in Win32 now supports up to 2048 low-level handles. The default is to allow a maximum of 512 If you don't see the transformation you need in the table below, you will need to use a combination of the transformations. For example, to transform a HANDLE h = ::CreateFile(...); fstream f; f.attach(_open_osfhandle(h)); Summary of Direct File Handle Transformations
Win32 HANDLE to C Library handle<io.h> int _open_osfhandle(long oshandle, int flags) This takes a int h = _open_osfhandle((long) myhandle, 0); It is not clear at this point to me what Microsoft will do in Win64 for this library call, because handles in Win64 (I think) will be 64 bits wide. C Library handle to FILE *Given a C library file handle, you can convert it to a <stdio.h><code> int _fdopen(int filehandle, const char * mode) where FILE * to C Library handleGive a <stdio.h> <code>int _fileno(FILE * f) C Library handle to Win32 Handle<io.h> long _get_osfhandle(int filehandle) This takes a C library file handle and returns the underlying Win32 CFile to HANDLEUnderlying a HANDLE to CFileTo associate a CFile::CFile(HANDLE h) Whether you do this in a stack variable or use heap allocation depends on the nature of your application. CFile file(myhandle); or CFile * file = new CFile(myhandle);
CStdioFile to FILE *The FILE * to CStdioFileTo associate a CStdioFile::CStdioFile(FILE * f) Whether you do this in a stack variable or use heap allocation depends on the nature of your application. <code>CStdioFile file(myfile); or CStdioFile * file = new CStdioFile(myfile);
stdxxx to HANDLEIf you need a handle to C Library handle to fstreamIf you have a C Library handle (the small integer from fstream(filedesc fd) You can also use the fstream f; f.attach(fd); For this to work, there must not be a file descriptor already attached to the
To obtain the underlying C library handle from an fstream f; f.attach(fd) ASSERT(fd == f.fd()) Inheritable handlesOK, someday soon... Other HandlesLikewise... Send mail to newcomer@flounder.com with questions or comments about this article.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Oct 2001 Editor: Smitha Vijayan |
Copyright 2001 by Joseph M. Newcomer Everything else Copyright © CodeProject, 1999-2008 Web16 | Advertise on the Code Project |