Microsoft KB Archive/64126

{|
 * width="100%"|

Overview of How to Share Memory Between Applications

 * }

Q64126

-

The information in this article applies to:


 * Microsoft Windows Software Development Kit (SDK) versions 3.0, 3.1

-

SUMMARY
Listed below are three ways to share memory between Windows applications; these are the only methods that are guaranteed to work in all memory configurations and in future versions of Windows:


 * 1) Using the Clipboard
 * 2) Storing information in the data segment of a shared Dynamic-Link Library (DLL)
 * 3) Dynamic Data Exchange (DDE)

MORE INFORMATION
 The Clipboard is the easiest method to use, and is discussed in the Windows Software Development Kit (SDK) as well as in such books as Charles Petzold's &quot;Programming Windows.&quot; Sharing data in the data segment of a DLL is possible because there is only one data segment for all instances of the DLL; DLLs are not &quot;instanced.&quot; Because of this, it is possible to have the DLL do a LocalAlloc out of its local heap, which is part of its DGROUP and thus is limited to 64K. Programmers must determine the memory scheme that best suits their needs and what calls they will make to the DLL to copy/share that memory to other applications that call into it. DDE is designed to allow applications that follow the protocol to share/pass data back and forth. An &quot;envelope and letter&quot; analogy, which is listed below, provides an example of how this works:

If some information needs to be sent from one person/application to another person/application, do the following:

 Address the envelope: Call GlobalAlloc on a piece of global memory with the GMEM_DDESHARE flag. Write the letter on a piece of paper: Call GlobalLock and write to the global memory. Seal the letter: Call the GlobalUnlock function. Send the letter off to the other person: Use the PostMessage function with a WM_DDE_DATA message that has the hGlobalMemory in it.

To receive and read the letter, the other person/application does the following:

 Get the letter: A WM_DDE_DATA message is found in the message queue, along with the handle of the global memory, hGlobalMemory.</li> Open the envelope: Call GlobalLock (hGlobalMemory).</li> Make a copy of the letter and read it:

<ul> Create a new envelope: Call GlobalAlloc(hNewEnvelope) and use the GMEM_DDESHARE flag IF the letter needs to be sent back.</li> Open the new envelope: Call GlobalLock(hNewEnvelope).</li> Copy the contents of the old letter to the new letter, modifying the contents at this time if necessary.</li> Seal the envelope: Call GlobalUnlock.</li> Send the letter: Call PostMessage with the new handle to global memory.</li></ul> </li> When done with the old letter, throw it away: Use GlobalUnlock on the handle and then call GlobalFree.</li></ol>

According to the DDE specification, the rules for freeing the global memory object are as follows:

Receiver deletes memory unless either of the following is true:

 fRelease flag is zero.</li> The fRelease flag is 1; however, the receiving (client) application responds with a negative WM_DDE_ACK message.</li></ol>

For more information, please refer to Chapter 15 in the Windows SDK reference manual.</li></ol>

For more information on sharing memory, please refer to Chapters 15 and 16 of the &quot;Microsoft Windows Software Development Kit Guide to Programming.&quot;

For more information on DDE, please refer to Chapter 22 of the &quot;Microsoft Windows Software Development Kit Guide to Programming.&quot;

Additional query words: no32bit 3.00 3.10

Keywords : kb16bitonly

Issue type :

Technology : kbAudDeveloper kbWin3xSearch kbSDKSearch kbWinSDKSearch kbWinSDK300 kbWinSDK310