Is there a Windows API or a method which can convert FILE* to HANDLE of a file or convert HANDLE to FILE* ??
-
Is there a Windows API or a method which can convert FILE* to HANDLE of a file or convert HANDLE to FILE* ??? The background of application :I want to monitor the I/O of a special file by hooking WriteFile and the file be opened by 'fopen'.In addition, I can`t modify the function form 'fopen' to 'CreateFile'. Therefore I can`t know which handle of file should be monitored according to the first parameter of WriteFile.So I hope there is a way that can convert HANDLE or FILE* to another. If you know it ,please tell me . Thank you.
-
Is there a Windows API or a method which can convert FILE* to HANDLE of a file or convert HANDLE to FILE* ??? The background of application :I want to monitor the I/O of a special file by hooking WriteFile and the file be opened by 'fopen'.In addition, I can`t modify the function form 'fopen' to 'CreateFile'. Therefore I can`t know which handle of file should be monitored according to the first parameter of WriteFile.So I hope there is a way that can convert HANDLE or FILE* to another. If you know it ,please tell me . Thank you.
I made a line
FILE *p = fopen ("foo", "rw");
and kept debugging into functions until I found the C runtime using an array of information called _osfhnd which stored the mapping between
stream
opened by_tsopen
andHANDLE
. This stream is the_file
member ofFILE
. _get_osfhandle http://msdn2.microsoft.com/en-us/library/ks2530z6.aspx[^] The below code omits any error checking whatsoever....HANDLE FileToHandle (FILE *f)
{
return (HANDLE)(f->_file);
}There are lots of reasons to be careful - if you're not in control of the FILE *, what's to stop them closing it without you knowing, etc. Enjoy! Iain.
-
I made a line
FILE *p = fopen ("foo", "rw");
and kept debugging into functions until I found the C runtime using an array of information called _osfhnd which stored the mapping between
stream
opened by_tsopen
andHANDLE
. This stream is the_file
member ofFILE
. _get_osfhandle http://msdn2.microsoft.com/en-us/library/ks2530z6.aspx[^] The below code omits any error checking whatsoever....HANDLE FileToHandle (FILE *f)
{
return (HANDLE)(f->_file);
}There are lots of reasons to be careful - if you're not in control of the FILE *, what's to stop them closing it without you knowing, etc. Enjoy! Iain.
Dear Iain, Thank you. Your ideal is right. But there is a mistake in your demo code. Perhaps, you lose a function call. I believe your original thought is following:
HANDLE FileToHandle (FILE *f) { return (HANDLE)(**_get_osfhandle**(f->_file)); }
I have tried the method and verified it is right. Enjoy! June -
I made a line
FILE *p = fopen ("foo", "rw");
and kept debugging into functions until I found the C runtime using an array of information called _osfhnd which stored the mapping between
stream
opened by_tsopen
andHANDLE
. This stream is the_file
member ofFILE
. _get_osfhandle http://msdn2.microsoft.com/en-us/library/ks2530z6.aspx[^] The below code omits any error checking whatsoever....HANDLE FileToHandle (FILE *f)
{
return (HANDLE)(f->_file);
}There are lots of reasons to be careful - if you're not in control of the FILE *, what's to stop them closing it without you knowing, etc. Enjoy! Iain.
-
Dear Iain, Now I encounter another problem that is how to get file`s name by HANDLE of a file. Do you know it??? If you know it, please help me. Thank you. Enjoy! June.
Thanks for using the code I *should* have written to get the HANDLE. I suspect the filename will be stored in FILE * in a similar fashion. If you do the same thing I did, and step into the fopen code, you can find out the information too. Good luck, Iain.
Iain Clarke appearing by Special Request of CPallini.
-
Thanks for using the code I *should* have written to get the HANDLE. I suspect the filename will be stored in FILE * in a similar fashion. If you do the same thing I did, and step into the fopen code, you can find out the information too. Good luck, Iain.
Iain Clarke appearing by Special Request of CPallini.