Hello,
I am trying to pass data from one active process to another, so far I have managed to get both processes to communicate with each other however the message that is shown by the second process does not resemble the message from the first process.
I have a feeling it has something to do with the conversions i'm using but for the life of me I can't work out where the problem lies.
I have added the code for the Producer (Create/Sends data) and the Consumer (Reads/Displays) as well as a screenshot of the debugger in action.
The code is based on the sample given just below:
Creating Named Shared Memory (Windows)
Producer:
Code:
#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];
LPCWSTR 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 = (LPCWSTR) 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
//
Consumer:
Code:
#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;
LPCWSTR 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 = (LPCWSTR) 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
//
Debugger:
http://i113.photobucket.com/albums/n...Untitled-3.jpg
Thank you for the help!