HANDLE and FILE*
-
Hello everyone, I am using Windows API open (create) a new file by CreateFile, http://msdn2.microsoft.com/en-us/library/aa363858.aspx the return value is HANDLE, if I use fwrite to write content to the file, since fwrite requires FILE* as parameter. I think HANDLE is not the same as FILE*, right? Are there any ways (or methods) to transform HANDLE to FILE*? I have tried that passing HANDLE directly to fwrite (forcely convert from HANDLE from FILE*) will cause access violation exception. I think the reason is HANDLE is not the same as FILE*, is that correct? thanks in advance, George
-
Hello everyone, I am using Windows API open (create) a new file by CreateFile, http://msdn2.microsoft.com/en-us/library/aa363858.aspx the return value is HANDLE, if I use fwrite to write content to the file, since fwrite requires FILE* as parameter. I think HANDLE is not the same as FILE*, right? Are there any ways (or methods) to transform HANDLE to FILE*? I have tried that passing HANDLE directly to fwrite (forcely convert from HANDLE from FILE*) will cause access violation exception. I think the reason is HANDLE is not the same as FILE*, is that correct? thanks in advance, George
-
Hello everyone, I am using Windows API open (create) a new file by CreateFile, http://msdn2.microsoft.com/en-us/library/aa363858.aspx the return value is HANDLE, if I use fwrite to write content to the file, since fwrite requires FILE* as parameter. I think HANDLE is not the same as FILE*, right? Are there any ways (or methods) to transform HANDLE to FILE*? I have tried that passing HANDLE directly to fwrite (forcely convert from HANDLE from FILE*) will cause access violation exception. I think the reason is HANDLE is not the same as FILE*, is that correct? thanks in advance, George
As already suggested by Jonathan Wilkes: "you should be using WriteFile() with CreateFile()". Alternatively you can use
fopen
to open the file and thenfwrite
to write into the file itsef. Bottom line: be consistent. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Hello everyone, I am using Windows API open (create) a new file by CreateFile, http://msdn2.microsoft.com/en-us/library/aa363858.aspx the return value is HANDLE, if I use fwrite to write content to the file, since fwrite requires FILE* as parameter. I think HANDLE is not the same as FILE*, right? Are there any ways (or methods) to transform HANDLE to FILE*? I have tried that passing HANDLE directly to fwrite (forcely convert from HANDLE from FILE*) will cause access violation exception. I think the reason is HANDLE is not the same as FILE*, is that correct? thanks in advance, George
It seems easier to convert from
FILE *
toHANDLE
:#include <io.h> #include <fcntl.h> FILE * f = . . .; HANDLE h = (HANDLE)_get_osfhandle(fileno(f));
The other conversion looks longer:
HANDLE h = . . . FILE * f = _fdopen(_open_osfhandle((intptr_t)h, _O_RDONLY), "rb");
I hope this works. I am not sure mixing of operations with
FILE *
andHANDLE
cannot be avoided. -
It seems easier to convert from
FILE *
toHANDLE
:#include <io.h> #include <fcntl.h> FILE * f = . . .; HANDLE h = (HANDLE)_get_osfhandle(fileno(f));
The other conversion looks longer:
HANDLE h = . . . FILE * f = _fdopen(_open_osfhandle((intptr_t)h, _O_RDONLY), "rb");
I hope this works. I am not sure mixing of operations with
FILE *
andHANDLE
cannot be avoided.Viorel. wrote:
I hope this works. I am not sure mixing of operations with FILE * and HANDLE cannot be avoided
In fact the mix can usually be avoided (and should be avoided, at least to maintain the code coherence). There are, of course, exceptions... :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.