Board index » cppbuilder » File handle to TFileStream?

File handle to TFileStream?


2007-07-26 10:20:07 PM
cppbuilder101
I have a file handle created
using Win32 API CreateFile().
Is it possible to construct
a TFileStream object from this handle?
Because I need to provide backward
compatibility in my application.
 
 

Re:File handle to TFileStream?

"Nataraj" < XXXX@XXXXX.COM >wrote in message
Quote
I have a file handle created
using Win32 API CreateFile().
Is it possible to construct
a TFileStream object from this handle?
Not TFileStream specifically, no. It does not provide access to do that.
Quote
Because I need to provide backward
compatibility in my application.
Does your app require TFileStream specifically, or just TStream in general?
If you can use any TStream, then you can construct a THandleStream
(TFileStream derives from THandleStream). THandleStream's constructor takes
a file handle as input, ie:
HANDLE hFile = CreateFile(...);
THandleStream *pStream = new THandleStream((int)hFile);
...
delete pStream;
CloseHandle(hFile);
If you must use a TFileStream, then you are probably out of luck. Normally,
I would suggest deriving your own class from TFileStream so that you can
access THandleStream's FHandle member directly. But your descendant class
can't bypass TFileStream's constructor, which requires TFileStream to open
the file. You *might* be able to construct a THandleStream directly and
then typecast it to a TFileStream, since TFileStream does not introduce any
new data members. But I'm not sure if that would work.
Gambit
 

Re:File handle to TFileStream?

Success. I could create a THandleStream from
hFile and do TFileStream operations on it.
No issues. Worked fine.
Thanks a lot for the suggestion!
-Nataraj
Quote

HANDLE hFile = CreateFile(...);
THandleStream *pStream = new THandleStream((int)hFile);
...
delete pStream;
CloseHandle(hFile);

If you must use a TFileStream, then you are probably out of luck. Normally,
I would suggest deriving your own class from TFileStream so that you can
access THandleStream's FHandle member directly. But your descendant class
can't bypass TFileStream's constructor, which requires TFileStream to open
the file. You *might* be able to construct a THandleStream directly and
then typecast it to a TFileStream, since TFileStream does not introduce any
new data members. But I'm not sure if that would work.


Gambit


 

{smallsort}