我有一个项目使用内存映射文件让两个应用程序相互共享数据.生产者应用程序是用C#编写的,消费者应用程序用简单的旧C语言编写.两者都使用VS2010.
MSDN称"BinaryWriter.Write Method(String)"在数据前面加上UTF-7编码的无符号整数,然后写入有效负载.这正是我被困住的地方.如果我写了一个长度为256个字符的字符串,那么C app的调试器会显示这个字节序列:0x80 0x2 <256次有效负载char>.将长度前缀转换为我可以在消费者应用中安全使用的最佳方法是什么?
制片人应用:
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
消费者应用:
#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 …Run Code Online (Sandbox Code Playgroud) 我有一个按钮和一个带有一些默认文本的单行编辑控件,我想在单击按钮时突出显示所有编辑控件的文本。
出于某种原因, SendMessage 没有效果并且文本没有突出显示 - 我做错了什么?
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
hwndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("edit"), L"Default text",
WS_CHILD | WS_VISIBLE | ES_LEFT | ES_AUTOHSCROLL,
10, 10, 150, 24, hWnd, (HMENU)IDC_EDIT,
hInst, NULL);
hwndButton = CreateWindowEx(0, TEXT("button"), L"Mark text",
WS_CHILD | WS_VISIBLE,
100, 100, 75, 24, hWnd, (HMENU)IDC_BUTTON,
hInst, NULL);
break;
case WM_COMMAND:
{
// Parse the menu selections:
switch (LOWORD(wParam))
{
case IDC_BUTTON:
SendMessage(hwndEdit, EM_SETSEL, 0, -1);
break;
case …Run Code Online (Sandbox Code Playgroud)