mmfshare.c

来自「一本已经绝版的好书」· C语言 代码 · 共 184 行

C
184
字号
/*************************************************************
Module name: MMFShare.C
Notices: Copyright (c) 1995-1997 Jeffrey Richter
*************************************************************/


#include "..\CmnHdr.H"                  /* See Appendix C. */
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include "Resource.H"


//////////////////////////////////////////////////////////////


BOOL Dlg_OnInitDialog (HWND hwnd, HWND hwndFocus, 
   LPARAM lParam) {

   // Associate an icon with the dialog box.
   chSETDLGICONS(hwnd, IDI_MMFSHARE, IDI_MMFSHARE);

   // Initialize the edit control with some test data.
   Edit_SetText(GetDlgItem(hwnd, IDC_DATA), 
      __TEXT("Some test data"));

   // Disable the Close button because the file can't
   // be closed if it was never created or opened.
   Button_Enable(GetDlgItem(hwnd, IDC_CLOSEFILE), FALSE);
   return(TRUE);
}


//////////////////////////////////////////////////////////////


void Dlg_OnCommand (HWND hwnd, int id, HWND hwndCtl, 
   UINT codeNotify) {
   
   // Handle of the open memory-mapped file
   static HANDLE s_hFileMap = NULL;
   HANDLE hFileMapT;

   switch (id) {
      case IDC_CREATEFILE:
         if (codeNotify != BN_CLICKED)
            break;

         // Create an in-memory memory-mapped file that 
         // contains the contents of the edit control. The 
         // file is 4 KB at most and is named MMFSharedData.
         s_hFileMap = CreateFileMapping((HANDLE) 0xFFFFFFFF, 
            NULL, PAGE_READWRITE, 0, 4 * 1024, 
            __TEXT("MMFSharedData"));

         if (s_hFileMap != NULL) {

            if (GetLastError() == ERROR_ALREADY_EXISTS) {
               chMB(__TEXT("Mapping already exists - not created."));
               CloseHandle(s_hFileMap);

            } else {

               // File mapping created successfully.

               // Map a view of the file 
               // into the address space.
               LPVOID lpView = MapViewOfFile(s_hFileMap,
                  FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);

               if ((BYTE *) lpView != NULL) {
                  // View mapped successfully; put contents 
                  // of edit control into the memory-mapped
                  // file.
                  Edit_GetText(GetDlgItem(hwnd, IDC_DATA),
                     (LPTSTR) lpView, 4 * 1024);

                  // Unmap the view. This protects the
                  // data from wayward pointers.
                  UnmapViewOfFile((LPVOID) lpView);

                  // The user can't create 
                  // another file right now.
                  Button_Enable(hwndCtl, FALSE);

                  // The user closed the file.
                  Button_Enable(GetDlgItem(hwnd,
                     IDC_CLOSEFILE), TRUE);

               } else {
                  chMB(__TEXT("Can't map view of file."));
               }
            }

         } else {
            chMB(__TEXT("Can't create file mapping."));
         }
         break;

      case IDC_CLOSEFILE:
         if (codeNotify != BN_CLICKED)
            break;

         if (CloseHandle(s_hFileMap)) {
            // User closed the file. A new file can be
            // created, but the new file can't be closed.
            Button_Enable(GetDlgItem(hwnd, IDC_CREATEFILE), 
               TRUE);
            Button_Enable(hwndCtl, FALSE);
         }
         break;

      case IDC_OPENFILE:
         if (codeNotify != BN_CLICKED)
            break;

         // See if a memory-mapped file named
         // MMFSharedData already exists.
         hFileMapT = OpenFileMapping(
            FILE_MAP_READ | FILE_MAP_WRITE,
            FALSE, __TEXT("MMFSharedData"));

         if (hFileMapT != NULL) {
            // Memory-mapped file does exist.  Map a view
            // of it into the process's address space.
            LPVOID lpView = MapViewOfFile(hFileMapT,
               FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);

            if ((BYTE *) lpView != NULL) {

               // Put the contents of the 
               // file into the edit control.
               Edit_SetText(GetDlgItem(hwnd, IDC_DATA),
                  (LPTSTR) lpView);
               UnmapViewOfFile((LPVOID) lpView);

            } else {
               chMB(__TEXT("Can't map view."));
            }

            CloseHandle(hFileMapT);

         } else {
            chMB(__TEXT("Can't open mapping."));
         }
         break;

      case IDCANCEL:
         EndDialog(hwnd, id);
         break;
   }
}


//////////////////////////////////////////////////////////////


BOOL CALLBACK Dlg_Proc (HWND hwnd, UINT uMsg, 
   WPARAM wParam, LPARAM lParam) {
   
   switch (uMsg) {
      chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Dlg_OnInitDialog);
      chHANDLE_DLGMSG(hwnd, WM_COMMAND, Dlg_OnCommand);
   }
   return(FALSE);
}


//////////////////////////////////////////////////////////////


int WINAPI _tWinMain (HINSTANCE hinstExe,
   HINSTANCE hinstPrev, LPTSTR pszCmdLine, int nCmdShow) {

   chWARNIFUNICODEUNDERWIN95();
   DialogBox(hinstExe, MAKEINTRESOURCE(IDD_MMFSHARE), 
      NULL, Dlg_Proc);

   return(0);
}


/////////////////////// End Of File ///////////////////////

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?