Discussion:
how to create a ofstream from a Windows file handle?
(too old to reply)
Buboi Peng
2005-09-30 08:01:28 UTC
Permalink
Hi All,

I get the file handle using API GetStdHandle, but how to convert this handle
to an ofstream?
Thanks in advance.

Buboi
Ulrich Eckhardt
2005-09-30 08:34:58 UTC
Permalink
Post by Buboi Peng
I get the file handle using API GetStdHandle, but how to convert this
handle to an ofstream?
There is no way to do so directly, ostreams (why oFstream?) don't have an
API fo that. However, there is help:
- Many vendors include an extension to their IOStream lib that takes handles
or filedescriptors.
- You could wrap the handle in a streambuf and use that with any IOStream.
- You could take a look at Boost's latest IO library, which includes such
functionality.

Uli
m***@hotmail.com
2018-04-15 16:46:16 UTC
Permalink
What worked for me was Ulrich suggestion to use boost iostreams library:

My system config is Windows 7, GCC 5.1.0, Boost 1_64_0.

My single test.cpp file is:

#define STRICT 1
#include <windows.h>
#include <cstddef>
#include <cstdio>
#include <cstdint>

#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>

namespace io = boost::iostreams;

int main()
{
HANDLE hFile = CreateFile(
"filename.txt",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );

if ( hFile == INVALID_HANDLE_VALUE )
{
printf( "CreateFile failed %d\n", GetLastError() );

getchar();
return EXIT_FAILURE;
}

io::file_descriptor_sink fd_sink_device(
hFile,
io::never_close_handle );

io::stream_buffer<io::file_descriptor_sink> streambuf( fd_sink_device );
std::ostream os( &streambuf );

os << "File contents" << std::endl;

CloseHandle( hFile );

getchar();
return EXIT_SUCCESS;
}

the makefile I used was:
FLAGS = -std=c++14 -pedantic -Wextra -Wlogical-op
INCPATHBOOST = -I"$(BOOST)"
LIBPATHBOOST = -L"$(BOOST_LIB)"
LIBSBOOST = -l:libboost_iostreams-mgw51-mt-s-1_64.a
LIBSWIN = -l:libkernel32.a -l:libuser32.a -l:libshell32.a -l:libadvapi32.a\
-l:libws2_32.a -l:liboleaut32.a -l:libimm32.a -l:libwinmm.a -l:libole32.a\
-l:libuuid.a -l:libopengl32.a -l:libole32.a -l:libgdi32.a

all: test.exe

test.exe : test.o
g++ -static -mconsole $(LIBPATHBOOST) test.o $(LIBSBOOST) $(LIBSWIN) -o test.exe

test.o : test.cpp
g++ -c $(INCPATHBOOST) $(FLAGS) test.cpp

clean:
del test.o test.exe

Ok, you might argue not all of this is necessary to communicate. But when I speak for myself I've such an extreme dislike for any documentation that is incomplete that left me wondering how all of the concepts fit together to make an program work. This including the Boost iostreams documentation I've been fighting with today. So I hope my example will be useful for many people. And experts are very welcome to comment and improve.
Igor Tandetnik
2005-09-30 12:03:45 UTC
Permalink
Post by Buboi Peng
I get the file handle using API GetStdHandle, but how to convert this
handle to an ofstream?
Standard handles are already wrapped into streams - cin, cout, cerr.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
CheckAbdoul
2005-09-30 15:59:25 UTC
Permalink
See if the following link helps you

http://www.flounder.com/handles.htm
--
Cheers
Check Abdoul [VC++ MVP]
-----------------------------------
Post by Buboi Peng
Hi All,
I get the file handle using API GetStdHandle, but how to convert this handle
to an ofstream?
Thanks in advance.
Buboi
Loading...