I'm having problems with shared memory and I'm hoping someone could offer a hand.
My main issue is trying to decipher the MSDN 'help' site. It seems they prefer to describe things in the most obscure way and avoid plain English where-ever possible.
UnmapViewOfFile() - I can't work out what this is doing. The best I can guess is that it tells the application to 'forget' about the shared memory.
CloseHandle() - Is the similar to DBP's CLOSE FILE but for shared memory? If so, is CreateSharedMemory() the OPEN FILE equivalent?
From MSDN:
Quote: "Although an application may close the file handle used to create a file mapping object, the system holds the corresponding file open until the last view of the file is unmapped"
That statement suggests that my understanding of the above two functions is wrong and that UnmapViewOfFile() closes the file.
It seems that each application that uses shared memory needs to inform the system that it has finished with it so that the system can release it. If so, what is the DBP command for doing that? Is it IanM's DELETE BANK?
Finally, is anyone willing to take a look at my code and point out possible errors?
sharedMemory.h
#ifndef SHAREDMEMORY_H
#define SHAREDMEMORY_H
#include <Windows.h>
class sharedMemory
{
public:
sharedMemory(void) {};
~sharedMemory(void) {};
bool CreateSharedMemory( int bufferSize, LPCWSTR name );
template<typename T>
void putInSharedMemory( T item )
{
CopyMemory((PVOID)pBuf, item, sizeof(T));
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
}
private:
HANDLE hMapFile;
LPCTSTR pBuf;
};
#endif //SHAREDMEMORY_H
sharedMemory.cpp
#include "sharedMemory.h"
#include "utilities.h"
bool sharedMemory::CreateSharedMemory( int bufferSize, LPCWSTR name )
{
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
bufferSize, // maximum object size (low-order DWORD)
name); // name of mapping object
if (hMapFile == NULL)
{
utils::display( "Create File Mapping Failed!\n");
return 1;
}
pBuf = (LPTSTR) MapViewOfFile( hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
bufferSize);
if (pBuf == NULL)
{
utils::display( "Create File Mapping Failed!\n");
CloseHandle(hMapFile);
return 1;
}
return 0;
}
Filling the shared memory:
memStore.putInSharedMemory(&mWidth);
memStore.putInSharedMemory(&mHeight);
memStore.putInSharedMemory(&mDepth);
for( int y=0; y<mHeight; y++){
for( int x=0; x<mWidth; x++){
for( int k=0; k<3; k++){
memStore.putInSharedMemory(&mHeightMap[offset(x,y)]);
}
memStore.putInSharedMemory(&mAlpha);
}
}
Where memStore is a sharedMemory object; mWidth, mHeight & mDepth are long integers; mAlpha is am unsigned char and mHeightMap is a std::vector<unsigned char>
The DBP access of the shared memory and conversion to image
bank = find free bank()
memblock = find free memblock()
image = find free image()
map shared mem to bank "Shared_Memory", bank, 0
make memblock from bank memblock, bank
make image from memblock image, memblock
for k = 0 to qtyMethods -1
setpanelimage heightMapPanel(tab), 10000
next k
I should point out that the above C++ code compiles without error, as does the DBP code. However, when executed the C++ file 'hangs' when asked to fill the memory and the DBP file crashes without error.
Any help greatly appreciated
Thanks