11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13. Adding Special Features to Application<br />

CMainFrame::OnMacroPlayback() implements command Macro | Playback, and<br />

CMainFrame::OnFinishJournal(…) handles message WM_FINISHJOURNAL:<br />

void CMainFrame::OnMacroPalyback()<br />

{<br />

m_bEnableMenu=FALSE;<br />

SetJournalPlaybackHook(GetSafeHwnd(), hInstance);<br />

}<br />

void CMainFrame::OnMacroRecord()<br />

{<br />

m_bEnableMenu=FALSE;<br />

SetJournalRecordHook(GetSafeHwnd(), hInstance);<br />

}<br />

LONG CMainFrame::OnFinishJournal(WPARAM wParam, LPARAM lParam)<br />

{<br />

if((BOOL)wParam == FALSE)<br />

{<br />

AfxMessageBox("Recording Finished");<br />

m_bPlayAvailable=TRUE;<br />

}<br />

else<br />

{<br />

m_bPlayAvailable=FALSE;<br />

AfxMessageBox("Playback Finished");<br />

}<br />

m_bEnableMenu=TRUE;<br />

}<br />

return TRUE;<br />

To test the program, we can first execute Macro | Record command, then use mouse or keyboard to<br />

generate a series of events. Next, we can press CTRL+F3 to stop recording. Finally we can execute Macro<br />

| Playback command to see what has been recorded.<br />

13.12Memory Sharing Among Processes<br />

In sample applications created in section 13.8, we demonstrated how to share user defined messages<br />

among different processes. However, with this method, we can send only simple parameter (integer) with<br />

every message. Sometimes it is necessary to share complex data among different processes, in which case<br />

we must apply memory sharing method.<br />

Problem with Global Memory<br />

Is it possible to share buffers allocated by ::GlobalAlloc(…) function among different processes? If<br />

so, we can embed memory handle in the message parameters and send it to another process. Upon receiving<br />

the message, the corresponding process can obtain the global memory handle and call ::GlobalLock(…) to<br />

access all the memory buffers.<br />

Unfortunately, although it is a possible method to share data among different processes for Win16<br />

applications, it is not possible for us to do so for Win32 based applications. In a 32-bit operating system,<br />

virtual memory is used to map physical memory to logical memory for each separate process, so each<br />

process has its own memory space from 0 to infinity (ideally). Therefore, if the two processes have the<br />

same logical address, they actually indicate different physical addresses. So if we send memory address<br />

from one process to another, it will not indicate the original buffer.<br />

File Mapping<br />

To solve this problem, in Win32 platform, there is a new technique that allows different processes to<br />

share a same block of memory. This technique is called file mapping, and can be used to let different<br />

processes share either a file or a block of memory.<br />

423

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!