home *** CD-ROM | disk | FTP | other *** search
- // GiveMem.lib - Creates a single memory block that can temporarily
- // ver.1 be given to another process based on its process ID
- // or a window handle. This allows WinSendMsg() calls
- // (and other calls) to set memory and give it to another
- // function. The routines in here are smart enough to
- // keep track of the processes that can access this memory.
- //
- // WARNINGS: Only 1 chunk of memory, almost 4K, is used here. The assumption
- // is that the same memory block will not be given to another
- // process until this first process is done with it. Also,
- // the memory block will not be really freed from memory/swap
- // until this process AND whatever processes the memory is
- // given to are freed.
- //
- //
- //**** GiveMemoryToProcess(): get chunk of memory shared with process ID
- // SYNTAX: pointer GiveMemoryToProcess(int ProcessID)
- // WHERE: ProcessID: a process identifier to share memory with; no checking on this ID
- // RETURN: pointer to the shared, allocated memory chunk
- //
- //
- //**** GiveMemoryToWindow(): get chunk of memory shared with process ID owning this window
- // SYNTAX: pointer GiveMemoryToWindow(int WindowHandle)
- // WHERE: WindowHandle: handle of any window--will give memory to process owning window
- // no checking is done on this except to not give memory if invalid
- // RETURN: pointer to the shared, allocated memory chunk
- //
- //
-
- #define OBJ_GIVEABLE 0x200
- #define OBJ_TILE 0x40
- #define PAG_COMMIT 0x10
- #define PAG_READ 0x1
- #define PAG_WRITE 0x2
- #define GM_SHARE_SIZE 4080 // almost 4K
-
- GiveMemoryToProcess(pPId)
- {
- if ( gGMPreviousID != pPId ) {
- gGMPreviousID = pPId;
-
- if ( !defined(gGMSharedMemoryPointer) )
- InitializeGMSharedMemoryPointer();
-
- // Check our array of given processes, and if already gave to that process
- // then do no more, else give to process and add to our given array
- lSharedProcessCount = defined(gGMSharedProcessIDs) ? 1 + GetArraySpan(gGMSharedProcessIDs) : 0 ;
- for ( lIdx = lSharedProcessCount; 0 <= --lIdx; ) {
- if ( pPId == gGMSharedProcessIDs[lIdx] )
- break;
- }
- if ( lIdx < 0 ) {
- // this process isn't in list of processes owning this memory
-
- // take this opportunity to prune out any id's in the gGMSharedProcessIDs[]
- // array to remove processes that no longer exist on the system
- if ( lSharedProcessCount && (lPList = ProcessList()) ) {
- lPListSpan = GetArraySpan(lPList);
- lNewCount = 0;
- for ( lTest = 0; lTest < lSharedProcessCount; lTest++ ) {
- lTestID = gGMSharedProcessIDs[lTest];
- for ( lPid = lPListSpan; lPid <= lPListSpan; lPid++ ) {
- if ( lPList[lPid] == lTestID ) {
- lNewArray[lNewCount++] = lTestID;
- break;
- }
- }
- }
- if ( lSharedProcessCount = lNewCount ) gGMSharedProcessIDs = lNewArray;
- else undefine(gGMSharedProcessIDs)
- }
-
- #define ORD_DOS32GIVESHAREDMEM 303
- DynamicLink( "DOSCALLS", ORD_DOS32GIVESHAREDMEM, BIT32, CDECL,
- gGMSharedMemoryPointer, gGMSharedProcessIDs[lSharedProcessCount] = pPId,
- PAG_READ | PAG_WRITE );
- }
- }
-
- return gGMSharedMemoryPointer;
- }
-
- GiveMemoryToWindow(pHwnd)
- {
- if ( gGMPreviousHwnd != pHwnd ) {
- gGMPreviousHwnd = pHwnd;
-
- // get process ID of this window, and then pass to GiveMemoryToProcess()
- #define ORD_WIN32QUERYWINDOWPROCESS 838
- if ( DynamicLink( "PMWIN", ORD_WIN32QUERYWINDOWPROCESS, BIT32, CDECL,
- pHwnd, lGMProcessID, lGMThreadID ) )
- GiveMemoryToProcess(lGMProcessID);
- }
- return gGMSharedMemoryPointer;
- }
-
- ////////////// Routines and globals used by above functions
-
- gGMSharedProcessIDs; // array of processes that have already been given access
- gGMSharedMemoryPointer; // the single block of memory that is giveable
-
- gGMPreviousID = -1; // avoid checking too often; if just did this then not again
- gGMPreviousHwnd = -1; // avoid checking too often; if just did this then not again
-
- InitializeGMSharedMemoryPointer()
- // Initialize pointer to shared memory that will be given to windowed
- // applications to receive their accelerator messages.
- {
- #define ORD_DOS32ALLOCSHAREDMEM 300
-
- assert( !DynamicLink( "DOSCALLS", ORD_DOS32ALLOCSHAREDMEM, BIT32, CDECL,
- gGMSharedMemoryPointer, NULL, GM_SHARE_SIZE,
- PAG_COMMIT | OBJ_GIVEABLE | PAG_READ | PAG_WRITE | OBJ_TILE ) );
- // Register function to free this memory when the current program exits
- atexit("TerminateGMSharedMemoryPointer");
- }
-
- TerminateGMSharedMemoryPointer() // called at exit to free gGMSharedMemoryPointer
- {
- #define ORD_DOS32FREEMEM 304
- assert( !DynamicLink( "DOSCALLS", ORD_DOS32FREEMEM, BIT32, CDECL, gGMSharedMemoryPointer ) );
- }
-