- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个内存映射文件
和相应的MapViewOfFile
句柄。 内存映射文件
将包含两部分数据:
unsigned int
指定实际数据的长度例如如果我的实际数据的长度是 10
个字符,内存映射文件将是这样的:
10
abcdefghij
其中 abcdefghij
是我想要的数据,并且会有一个换行符 b/w 长度数字和数据。如果需要,我也可以自由使用任何其他分隔符。
现在我想先读取unsigned int
,以便能够读取实际数据。我正在考虑 memcpy
但我不确定如何才能获得准确的 unsigned int 值,因为我认为 memcpy
逐字符复制。如何从内容中提取 10
?
最佳答案
你可以这样做:
#include "windows.h"
#include <iostream>
int main() {
const size_t buffer_size = 4098;
const char* name = "my_shared_memory"; //sm name
HANDLE file = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, buffer_size, name);
LPVOID region = MapViewOfFile(file, FILE_MAP_ALL_ACCESS, 0, 0, buffer_size);
const char* const msg = "this is my data"; //this is the string you want to pass
const int msg_size = strlen(msg) + 1; //this is the size of the string data with \0
const int total_size = msg_size + sizeof(int); //the total size of your data: the size of the string and the string by itself
char* const data = (char*)malloc(total_size); //get some memory to place this data
((int*)data)[0] = msg_size; //set the first part of the data with the string msg size
memcpy(data + sizeof(int), msg, msg_size); //set the msg by it self just after the size
CopyMemory(region, data, total_size); //put on sm
const int retrieved_size = ((int*)region)[0]; //retrieves the size of the msg from the sm
char* const retrieved_msg = (char*)malloc(retrieved_size); //alloc some space for receive the msg with the retrieved size
memcpy(retrieved_msg, (char*)region + sizeof(int), retrieved_size); //set the msg in the previous allocated memory
std::cout << retrieved_msg << std::endl; //prints the msg
free(data);
free(retrieved_msg);
}
在此question您有一些解决方案来检查字节序。然后,如果需要,您可以再使用一个字节来存储此信息。如果字节顺序不同,您可以 swap bytes .
Josh 评论的解决方案:
对于 ASCII 编码的大小,您可以将其作为字符串保存,而不是将 int 二进制编码的大小存入内存:
const int max_digits_size = 3;
char number[max_digits_size + 1];
sprintf(number, "%d", msg_size);
//your total_size is now (max_digits_size + 1) + msg_size
//first you retrieve from sm the number as string in number_memcopied var, then you convert:
const int retrieved_size = strtol(number_memcopied, NULL, 10);
关于c++ - 以下场景如何复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58011965/
这个问题在这里已经有了答案: standalone parentheses in javascript [duplicate] (5 个答案) 关闭 8 年前。 我在学习JavaScript,有时会
我是mysql新手,我必须减少以下更新查询的执行时间 UPDATE temp_countcalculations, ( SELECT count(*) as insuffcounts,CRP_
def escape_html(s): for (i, o) in (("&","&"),(">", ">"),(" "变成 ">"等。 关于python - 以下 for 循环
if (read(read(cin, data1), data2)) 问题是C++ Primer 5th Edition 的练习。 read 函数定义如下: std::istream &read(st
我想创建两个宏。其中一个将扩展到函数原型(prototype)和函数内容,另一个将扩展到仅函数原型(prototype)。我正在考虑创建以下内容: #ifdef SOME_CONDITION #def
我正在使用 jongo API - org.jongo.MongoCollection 是类。 我有对象 ID 列表并转换为与 ObjectId[] 相同并尝试按如下方式查询 collection.f
有人可以解释以下正则表达式匹配什么吗? ^.*$ 谢谢! 最佳答案 或者整个字符串或者整行,取决于是否multiline mode被使用。 关于java - 以下 ^.*$ 正则表达式匹配什么?,我们
#include void main() { int a,b,c; for(b = c = 10; a = "- FIGURE?, UMKC,XYZHello Folks,TFy!QJ
我的代码段中的以下代码行被 Sonar 检测为问题。 代码段: final int Pending=1; Sonar 问题: Name 'Pending' must matc
Print name of all activities with neither maximum nor minimum number of participants 我尝试了以下查询,但出现错误:
这个问题在这里已经有了答案: What is this practice called in JavaScript? (7 个回答) 关闭8年前。 (function() { //do stuff
根据任务,我们必须通过 foldr 实现 foldl。通过比较函数签名和 foldl 实现,我得到了以下解决方案: myFoldl :: (a -> b -> a) -> a -> [b] -> a
这个问题在这里已经有了答案: Export an es6 default class inline with definition or at end of file? (1 个回答) 关闭 2 年
据我了解,以下是相同的: Person p{}; // Case 1 Person p = {}; // Case 1.5 我注意到 Person p = Person{}; // Case 2 产生
below i have given a javascript code picture `` can any one help me in this code. what do this code.
我想在标题和正文上搜索全文,并在答案计数上进行过滤。 我阅读了elasticsearch documentation for combining filters并构建了此查询。 "query": {
它是流动的 C 代码中的内存泄漏吗? #include int *a; int main() { a = malloc(sizeof(int)*10); return
这两个声明有什么区别: char (*ptr)[N]; 对比 char ptr[][N]; 谢谢。 最佳答案 (1)声明 char (*ptr)[N]; ptr 是指向大小为 N 的字符数组的指针 下
data II = I Int Int deriving (Show) instance II Show where show I a b = show (a+b) showt.hs:3:2: s
我从 clojuredoc 中阅读了关于 condp 的文档。在文档中我找到了以下代码: (condp 一些 [1 2 3 4] #{0 6 7} :>> 公司 #{4 5 9} :>> 十二月 #{
我是一名优秀的程序员,十分优秀!