Reputation: 1
I have two win32 programs, and they communicate with one DLL via IPC. I use MSVC ++ so it makes sense that I use CreateFileMapping and MapViewOfFile. I already have some codes done, the problem is that when my second program starts and loads a DLL and starts to work with DLLs, everything is fine and IPC communication is going well. When the first program starts and loads a DLL and starts to work with DLLs and the second program starts and loads a DLL and starts to work with DLLs, second program stops communicating with DLL via IPC. The IPC communication is probably running but probably in wrong way. It obtains the size and capacity, but the content is junk. The content of the shared memory is vector of pointers! What am I doing wrong? Is there anything I need to know to get those work fine?
//The sender writer
MainWindow::~MainWindow()
{
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
}
void MainWindow::SetData(vector <SubWindow*> &iMainWin)
{
hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(vector <SubWindow*>), TEXT("MyMappedMem"));
if (hMapFile == NULL)
return;
vector <SubWindow*> *pBuf = reinterpret_cast<std::vector<SubWindow*>*> (MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(vector <SubWindow*>)));
if (pBuf == NULL)
{
CloseHandle(hMapFile);
return;
}
memcpy(pBuf, &iMainWin, sizeof(iMainWin));
return;
}
//The reciever reader
void DataWork()
{
do{
if (strcmp(pe32.szExeFile, "Win32Project.exe") == 0)
{
bProcessFound = true;
break;
}
} while (Process32Next(hProcessSnap, &pe32));
if (!bProcessFound)
{
MessageBox(NULL, TEXT("ERROR Process32Next Openning DLL"), TEXT("ERROR"), MB_OK);
return;
}
else
{
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, TEXT("MyMappedMem"));
if (hMapFile == NULL)
{
MessageBox(NULL, TEXT("ERROR OpenFileMapping DLL"), TEXT("ERROR"), MB_OK);
return;
}
vector<SubWindow*> *pBuf = reinterpret_cast<std::vector<SubWindow*>*> (MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(vector <SubWindow*>)));
if (pBuf == NULL)
{
MessageBox(NULL, TEXT("ERROR MapViewOfFile DLL"), TEXT("ERROR"), MB_OK);
CloseHandle(hMapFile);
return;
}
for (int i = 0; i < buf->size(); i++)
(*buf)[i]->value_access;
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
}
}
Upvotes: 0
Views: 952
Reputation: 3917
Attempting to share a std::vector
using shared memory is almost certainly wrong. The std::vector
will allocate memory from the calling process, which will not be accessible via any other process. Furthermore, pointers pointing to local addresses will not be accessible any other process.
Additionally, you would need to coordinate access to the std::vector
using a mutex, which you aren't doing.
In this situation, I can't see any reason why using a text file wouldn't be sufficient.
Upvotes: 2
Reputation: 10415
Pointers in the first program are useless in the second program, and vice versa. You cannot use a pointer from program A in program B because they have different virtual memory spaces.
Upvotes: 2