我一直在回顾 Pavel Yosifovich 的书籍和视频。我尝试编写一个纯 C 程序来创建一个内核对象并允许文本字符串从一个进程传递到另一个进程。我的代码如下,但由于某种原因,读取数据的程序对 OpenFileMapping 的调用始终返回 NULL,并且最后一个错误为 2,即未找到文件。我已运行 WinObj 并且 MySharedMemory 对象显示在 \Sessions \BaseNamedObjects\MySharedMemory 中。我不认为这是一个安全问题,因为错误是找不到文件,我尝试将源更改为指向 Local\MySharedMemory,但这没有什么区别。我也尝试过在管理员命令提示符下运行,但仍然不起作用。看起来它必须与路径有关,因为它说找不到文件。以下是这两个程序的代码。
制作人
#include <windows.h>
#include <stdio.h>
#include <string.h>
int main() {
HANDLE hFileMapping;
LPVOID lpFileMap;
char buffer[] = "Windows System Programming is fun!";
hFileMapping = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
sizeof(buffer),
L"MySharedMemory"
);
if (hFileMapping == NULL) {
printf("Error creating file mapping: %d\n", GetLastError());
return 1;
}
lpFileMap = MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, 0);
if (lpFileMap == NULL) {
printf("Error mapping view of file: %d\n", GetLastError());
CloseHandle(hFileMapping);
return 1;
}
strcpy((char*)lpFileMap, buffer);
printf("Data written to shared memory. Press Enter to exit...\n");
getchar();
UnmapViewOfFile(lpFileMap);
CloseHandle(hFileMapping);
return 0;
}
消费者
#include <windows.h>
#include <stdio.h>
int main() {
HANDLE hFileMapping;
LPVOID lpFileMap;
hFileMapping = OpenFileMapping(
FILE_MAP_READ,
FALSE,
L"MySharedMemory"
);
if (hFileMapping == NULL) {
printf("Error opening file mapping: %d\n", GetLastError());
getchar();
return 1;
}
lpFileMap = MapViewOfFile(
hFileMapping,
FILE_MAP_READ,
0,
0,
0,
0
);
if (lpFileMap == NULL) {
printf("Error mapping view of file: %d\n", GetLastError());
CloseHandle(hFileMapping);
getchar();
return 1;
}
printf("Received message: %s\n", (char*)lpFileMap);
UnmapViewOfFile(lpFileMap);
getchar();
CloseHandle(hFileMapping);
return 0;
}
最新代码,但还是不行
#include <windows.h>
#include <stdio.h>
#include <string.h>
int main() {
HANDLE hFileMapping;
LPVOID lpFileMap;
char buffer[] = "Windows System Programming is fun!";
hFileMapping = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
1 << 12,
L"MySharedMemory"
);
if (hFileMapping == NULL) {
printf("Error creating file mapping: %d\n", GetLastError());
return 1;
}
lpFileMap = MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, 0);
if (lpFileMap == NULL) {
printf("Error mapping view of file: %d\n", GetLastError());
CloseHandle(hFileMapping);
return 1;
}
strcpy((char*)lpFileMap, buffer);
printf("Data written to shared memory. Press Enter to exit...\n");
getchar();
UnmapViewOfFile(lpFileMap);
CloseHandle(hFileMapping);
return 0;
}
#include <windows.h>
#include <stdio.h>
int main() {
HANDLE hFileMapping;
LPVOID lpFileMap;
//hFileMapping = OpenFileMapping(
// FILE_MAP_READ,
// FALSE,
// L"MySharedMemory"
// );
hFileMapping = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
1 << 12,
L"MySharedMemory"
);
if (hFileMapping == NULL) {
printf("Error opening file mapping: %d\n", GetLastError());
getchar();
return 1;
}
lpFileMap = MapViewOfFile(
hFileMapping,
FILE_MAP_READ,
0,
0,
1 << 12
);
if (lpFileMap == NULL) {
printf("Error mapping view of file: %d\n", GetLastError());
CloseHandle(hFileMapping);
getchar();
return 1;
}
printf("Received message: %s\n", (char*)lpFileMap);
UnmapViewOfFile(lpFileMap);
getchar();
CloseHandle(hFileMapping);
return 0;
}