BOOL APIENTRY DllMain(HANDLE hModule, DWORD reason,
LPVOID reserved)
{
if (reason == DLL_PROCESS_ATTACH) {
memCounter++;
if (memCounter == 1) // 1st one makes the file
CreateMappedFile(MEMORY_LIMIT);
else
CreateMappedFile(0); // others only map to the file
}
else
memCounter--;
return true;
}
//
// Create and/or map the mapped file in the temp directory
BOOL CreateMappedFile(int size)
{
HANDLE h;
char szTmpFile[256];
char *testbase;
if (size == 0) { // ONLY MAP TO THE FILE IF SIZE IS 0.
h = OpenFileMapping (FILE_MAP_WRITE,TRUE,"embsqlmemory");
if (h != NULL) {
testbase = (char *)MapViewOfFileEx(h, FILE_MAP_WRITE,
0,0,0,sharedMemoryFile);
if (sharedMemoryFile != testbase)
return false;
hMapFile = h;
return true;
}
//
// Need to make the file AND map it
//
GetTempPath (256, szTmpFile);
sprintf(mapFileName,"%s%s",szTmpFile,"embsql.tmp");
// Create the file, stomp on whatever was there
h = CreateFile (mapFileName, GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_TEMPORARY, NULL);
if (h == (HANDLE)INVALID_HANDLE_VALUE)
return false;
// Create the mapping, this sets the shared memory regions
// object name so subsequent users can get at it
h = CreateFileMapping (h, NULL,PAGE_READWRITE, 0, size,
"embsqlmemory");
// Set the pointers into the shared memory region
sharedMemoryFile =
(char *)MapViewOfFile (h, FILE_MAP_WRITE, 0, 0, 0);
memoryLimitF = &sharedMemoryFile[size];
curPtrF = sharedMemoryFile;
hMapFile = h;
return true;
}