我有一个项目使用内存映射文件让两个应用程序彼此共享数据。 生产者应用程序是用C#编写的,消费者应用程序使用简单的老C语言。两者都使用VS2010。C#BinaryWriter长度前缀 - UTF7编码
MSDN说“BinaryWriter.Write方法(字符串)”用UTF-7编码的无符号整数前置数据,然后写入有效载荷。这正是我卡住的地方。如果我写了一个长度为256个字符的字符串 ,则C应用程序的调试器会显示此字节序列:0x80 0x2 <有效负载字符数的256倍。将长度前缀转换为我可以安全地在消费者应用程序中使用的最佳方式是什么?
监制应用:
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, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not open file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPCSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
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); // print mapped data
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
BR, 克里斯
'0x80 0x02'是无符号整数... –
是的,几乎忘了;)如果<127个字节被写入,则长度前缀恰好是具有该值的第一个字节。否则,前两个字节似乎表示有效载荷长度,但我无法弄清楚如何使用它们 - 请参阅我的文章中的解释文字。 – user2286339