0

I'm kind of new to cpp so maybe you guys can save my day...

I need to create a simple xml and save it into an existing memory mapped file. This is what I have so far (removed all error checking etc.):

HANDLE hFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, _identifier); 
LPCTSTR buffer = (LPCTSTR)MapViewOfFile(hFile, FILE_MAP_ALL_ACCESS, 0, 0, MAP_FILE_SIZE);
IStream *stream;
HRESULT res = CreateStreamOnHGlobal(NULL, TRUE, &stream);   
IXmlWriter *pWriter;    
res = CreateXmlWriter(__uuidof(IXmlWriter), (void**) &pWriter, NULL);   
res = pWriter->SetOutput(stream);
res = pWriter->WriteStartElement(NULL, _T("Root"), NULL);
res = pWriter->WriteString(_T("12345"));    
res = pWriter->Flush();

Now I have no idea what to do next. Can someone please help me getting the stream data into the mapped memory file?

Tsef
  • 1,018
  • 9
  • 22
  • Is there a reason you cannot use `SHCreateStreamOnFileEx` to create a stream that writes directly to the file? – Igor Tandetnik Nov 14 '13 at 19:11
  • Yes, it's a page file and when doing like you suggested I get an error that the system cannot find the file specified. – Tsef Nov 15 '13 at 08:30
  • Why do you want to serialize into memory (backed by a page file) only to copy to another block of memory (also backed by a page file)? What's the ultimate goal of the exercise? – Igor Tandetnik Nov 15 '13 at 13:29
  • It's for inter process communication between a C# application and a C++ process. We signal back and forth between them using reset events and we're using a memory mapped file as a means of passing data between them. – Tsef Nov 15 '13 at 14:01
  • I found a possible solution here:http://stackoverflow.com/questions/3037946/how-can-i-store-xml-in-buffer-using-xmlite but it seems kind of wasteful to write the stream and then ready back only to save it to the mapped file. – Tsef Nov 15 '13 at 14:04
  • Well, you could provide your own IStream implementation that writes to a given fixed memory buffer (and presumably errors out on overflow). I don't believe the OS provides such an implementation, but it shouldn't be hard to make your own. – Igor Tandetnik Nov 15 '13 at 14:16
  • I guess I could, doesn't seem that complicated. Although I was hoping to find something built in. – Tsef Nov 16 '13 at 15:28

0 Answers0