I have a project using memory mapped files that allow two applications to exchange data with each other. The manufacturer application is written in C #, the consumer application speaks of a simple old C. Both use VS2010.
MSDN says that the “BinaryWriter.Write Method (String)” adds data using an UTF-7 encoded integer, and then writes the payload. This is exactly where I got stuck. If I write a string with a length of 256 characters, the application debugger C shows me this sequence of bytes: 0x80 0x2 and 256 times the char> payload. What is the best way to convert a length prefix to something that I can safely use in a consumer application?
Producer Application:
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Threading;
using System.Text;
using System.Linq;
class Program
{
static void Main(string[] args)
{
using (MemoryMappedFile mmf_read = MemoryMappedFile.CreateNew("mappedview", 4096))
{
using (MemoryMappedViewStream stream = mmf_read.CreateViewStream())
{
string str;
BinaryWriter writer = new BinaryWriter(stream);
str = string.Join("", Enumerable.Repeat("x", 256));
writer.Write(str);
}
}
}
}
:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")
#define BUF_SIZE 4096
TCHAR szName[]=TEXT("Global\\mappedview");
int _tmain()
{
HANDLE hMapFile;
LPCSTR pBuf;
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
FALSE,
szName);
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not open file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPCSTR) MapViewOfFile(hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
printf("Proc1: %s\n\n", pBuf);
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
,