- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 this MSDN link 上的教程实现一种将数据从一个进程传输到另一个进程的方法。尽管有人建议我 earlier question要使用 Pipe 方法,由于某些限制,我别无选择,只能使用 CreateFileMapping 方法。
现在,我已经成功地在同一个解决方案中制作了两个单独的窗口窗体项目,并通过编辑两个窗体同时加载的一些属性。
此外,我已经设法将 MSDN 示例中给出的代码实现到第一个(生产者)和第二个(消费者)程序中,没有任何编译错误。
我现在遇到的问题是,当我运行第一个程序并尝试创建映射文件的句柄时,我得到一个错误,说它不成功,我不明白为什么会这样。
我已经添加了生产者和消费者代码文件来演示我正在尝试做什么。
制作人:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
//File header definitions
#define IDM_FILE_ROLLDICE 1
#define IDM_FILE_QUIT 2
#define BUF_SIZE 256
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
TCHAR szMsg[]=TEXT("Message from first process!");
void AddMenus(HWND);
LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
////Standard windows stuff - omitted to save space.
//////////////////////
// WINDOWS FUNCTION //
//////////////////////
LRESULT CALLBACK WindowFunc(HWND hMainWindow, UINT message,
WPARAM wParam, LPARAM lParam)
{
WCHAR buffer[256];
LPCTSTR pBuf;
struct DiceData storage;
HANDLE hMapFile;
switch(message)
{
case WM_CREATE:
{
// Create Menus
AddMenus(hMainWindow);
}
break;
case WM_COMMAND:
// Intercept menu choices
switch(LOWORD(wParam))
{
case IDM_FILE_ROLLDICE:
{
//Roll dice and store results in variable
//storage = RollDice();
////Copy results to buffer
//swprintf(buffer,255,L"Dice 1: %d, Dice 2: %d",storage.dice1,storage.dice2);
////Show via message box
//MessageBox(hMainWindow,buffer,L"Dice Result",MB_OK);
hMapFile = CreateFileMapping(
(HANDLE)0xFFFFFFFF, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (hMapFile == NULL)
{
MessageBox(hMainWindow,L"Could not create file mapping object",L"Error",NULL);
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
MessageBox(hMainWindow,L"Could not map view of file",L"Error",NULL);
CloseHandle(hMapFile);
return 1;
}
CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
_getch();
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
}
break;
case IDM_FILE_QUIT:
SendMessage(hMainWindow, WM_CLOSE, 0, 0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hMainWindow, message, wParam, lParam);
}
//
//Setup menus
//
消费者:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
//File header definitions
#define IDM_FILE_QUIT 1
#define IDM_FILE_POLL 2
#define BUF_SIZE 256
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
//Prototypes
void AddMenus(HWND);
LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
//More standard windows creation, again omitted.
//////////////////////
// WINDOWS FUNCTION //
//////////////////////
LRESULT CALLBACK WindowFunc(HWND hMainWindow, UINT message,
WPARAM wParam, LPARAM lParam)
{
HANDLE hMapFile;
LPCTSTR pBuf;
switch(message)
{
case WM_CREATE:
{
// Create Menus
AddMenus(hMainWindow);
break;
}
case WM_COMMAND:
{
// Intercept menu choices
switch(LOWORD(wParam))
{
case IDM_FILE_POLL:
{
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
if (hMapFile == NULL)
{
MessageBox(hMainWindow,L"Could not open file mapping object",L"Error",NULL);
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
MessageBox(hMainWindow,L"Could not map view of file",L"Error",NULL);
CloseHandle(hMapFile);
return 1;
}
MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
break;
}
case IDM_FILE_QUIT:
SendMessage(hMainWindow, WM_CLOSE, 0, 0);
break;
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hMainWindow, message, wParam, lParam);
}
//
//Setup menus
//
这绝不是整洁和最终的,但这只是一个开始,感谢您的帮助。
编辑:错误
Edit2:输出
最佳答案
你的制作人代码对我有用。您使用的是什么版本的 Windows?在较新的版本(如 Vista 和 7)中,对访问共享内存有额外的安全限制。在您上面引用的 MSDN 文章中有关于此的注释,说您必须是管理员才能在 Windows Vista/7 中创建全局共享内存对象。
您还应该调用 GetLastError() 以查看从 CreateFileMapping() 实际返回的错误代码,这可能有助于确定问题的根本原因。
关于c - 使用 CreateFileMapping 时出错 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2647051/
我正在尝试使用 2 个同时运行的线程,并且都使用 FileMapping 功能通过不同的句柄将文件映射到内存中。问题是 MapViewOfFile 函数有时会将两个线程上的映射分配到相同的内存偏移量。
我想将文本写入文件。文本长度未知。所以想不出设置要使用的mapped memory的大小,就设置为100。然后,问题出现了!字符串写入成功,但剩余的100字节空间被NULL填满!!我怎样才能避免它??
使用 win32api,我希望下面的程序创建两个进程并创建一个文件映射。 (使用 C++) 我不知道我应该在 Handle CreateFileMapping(....我试过: PROCCESS_IN
我正在创建一个在不同应用程序之间共享内存的 DLL。 创建共享内存的代码如下所示: #define NAME_SIZE 4 HANDLE hSharedFile; create(char[NAME_S
我在将部分卷映射到内存时遇到问题。卷已成功打开,我可以从中读取,但 CreateFileMapping() 失败。 GetLastError() 返回 87,其中有 following意思是:“参数不
函数CreateFileMapping可用于在页面文件中分配空间(如果第一个参数是INVALID_HANDLE_VALUE)。分配的空间稍后可以内存映射到进程虚拟地址空间。 为什么我要这样做而不是仅使
我正在尝试按照 MSDN 上的教程进行操作:Creating Named Shared Memory . 不幸的是,我从 CreateFileMapping() 得到了 NULL。文件 shmfile
我有两个用 C 编写的窗口窗体应用程序,一个包含一个由两个整数组成的结构,另一个将使用 CreateFileMapping 接收它。 虽然没有直接关系,但我想准备好三个事件,这样每个进程都可以相互“对
只是想知道我是否正在像这样在 Windows 平台上创建共享内存阵列: HANDLE hFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAG
我希望使用命名共享内存实现IPC。 为此,其中一个步骤是使用 CreateFileMapping() 获取映射内存对象的句柄。 我完全按照 MSDN 网站的建议进行操作:http://msdn.mic
我正在开发一个针对桌面系统的应用程序,该系统可能只有 256MB RAM(Windows 2000 及更高版本)。在我的应用程序中,我有一个大文件 (>256MB),其中包含大约 160 字节/每个的
我是第一次尝试使用 CreateFileMapping,当我使用 GetLastError() 时出现此错误: ERROR_INVALID_HANDLE: The handle is invalid.
我按照设计在多个进程之间共享内存数据。 根据设计,应用程序需要多个内存文件,就像每个进程需要一个内存映射文件一样。但是有些进程需要更少的内存映射文件,比如 10KB 或更小。有些进程需要超过 1mb
请你帮我理解在本地/全局命名空间中是否可以有 2 个同名的共享内存对象?我知道,如果我们第一次创建了一个共享内存对象,那么我们需要调用 OpenFileMapping/MapViewOfFile 来打
亲爱的 friend ,我需要你的帮助。 我需要使用 MMF(内存映射文件)将 .bmp 文件发送到另一个进程(对话框)并在那里显示它但问题是图像以相反的颜色和上下颠倒的方式显示。 这是源代码: 在第
我正在使用 this MSDN link 上的教程实现一种将数据从一个进程传输到另一个进程的方法。尽管有人建议我 earlier question要使用 Pipe 方法,由于某些限制,我别无选择,只能
我正在使用共享内存映射进行进程间通信。该映射需要在 session 之间可见,因此我试图在全局命名空间中创建它。我知道正在运行的用户需要 SeCreateGlobalPrivilege 才能使其正常工
我正在尝试使用 CreateFileMapping 和 OpenFileMapping 在进程之间共享内存。这没有像我希望的那样工作 - OpenFileMapping 返回 null 并且 GetL
背景:我正在尝试创建一个可以被多个进程访问的内存映射文件。在下面的代码中,我只放入了与我目前必须使事情变得更简单的问题有关的代码。根据 msdn,我应该能够创建一个文件映射,映射文件的 View 并关
我担心使用内存映射 IO 的危险,通过 CreateFileMapping ,在 FAT 文件系统上。具体场景是用户直接从 U 盘打开文档(是的,你尝试禁止他们这样做!)。 MSDN Managing
我是一名优秀的程序员,十分优秀!