The following are the procedures I use to set and get from the shared memory area. I threw the structure in as a reference. Ignore the red +s as they indicate a line wrap.
-Mike
//***********************************************************
//* *
//* Structure Definitions *
//* *
//***********************************************************
struct shmInfoEntry
{
char shmBufferName[25];
DWORD shmBufferSize;
DWORD shmDataSize;
HANDLE shmHMapFile;
LPVOID shmLpMapAddress;
};
//***********************************************************
//*Name: SetSharedMem *
//*Function: SetSharedMem sets the value of the shared *
//* memory area reference by shareIndex *
//*Return Vals: 0 = Operation Failed *
//* 1 = Operation Succeeded *
//***********************************************************
int __export __stdcall SetSharedMem(const int shareIndex, const void*
+lpszBuf, const DWORD bufLength)
{
LPVOID recordOffset;
struct shmInfoEntry tempShmEntry;
// DEGUG FOR SEEING IF lpszBuf is writing out the proper stuff
// FILE *crapfile;
//
// // Write out file
// crapfile=fopen("crapfile.txt","wb");
// fwrite(lpszBuf,bufLength,1,crapfile);
// fclose(crapfile);
if (shareIndex > lastShmIndex)
{
// Have To Use A Existing Share
return 0;
}
// Get Record Offset To Struct Of Info
recordOffset = (DWORD) lpvMem + (sizeof(int) + (shareIndex * sizeO
+fInfoStruct));
// Copy Struct From Shared Memory
memcpy(&tempShmEntry,recordOffset,sizeOfInfoStruct);
if (bufLength > tempShmEntry.shmBufferSize)
{
//NOTE: THIS IS BAD, RESIZE BUFFER OR FAIL
return 0;
}
// Set Data Size
tempShmEntry.shmDataSize = bufLength;
// Save Data Size
memcpy(recordOffset,&tempShmEntry,sizeOfInfoStruct);
// lpMapAddress should be the first memory byte of the shared memo
+ry struct
memcpy(tempShmEntry.shmLpMapAddress,lpszBuf,bufLength);
return 1;
}
//***********************************************************
//*Name: GetSharedMem *
//*Function: GetSharedMem gets the value of the shared *
//* memory area reference by shareIndex *
//*Return Vals: 0 = Operation Failed *
//* 1 = Operation Succeeded *
//***********************************************************
int __export __stdcall GetSharedMem(const int shareIndex,void* lpszBuf
+, const DWORD bufLength)
{
LPVOID recordOffset;
struct shmInfoEntry tempShmEntry;
// FILE *crapfile;
if (shareIndex > lastShmIndex)
{
return 0;
}
// Get Record Offset To Struct Of Info
recordOffset = (DWORD) lpvMem + (sizeof(int) + (shareIndex * sizeO
+fInfoStruct));
// Copy Struct From Shared Memory
memcpy(&tempShmEntry,recordOffset,sizeOfInfoStruct);
if (bufLength > tempShmEntry.shmBufferSize)
{
//NOTE: THIS IS BAD, RESIZE BUFFER OR FAIL
return 0;
}
// Grab Stuff Out Of Buffer
memcpy(lpszBuf,tempShmEntry.shmLpMapAddress,bufLength);
// Debugging what should be returned
// crapfile=fopen("crapfile.txt","wb");
// fwrite(lpszBuf,bufLength,1,crapfile);
// fclose(crapfile);
return 1;
}
|