|
|
1.1.1.2 ! root 1: ! 2: /******************************************************************************\ ! 3: * This is a part of the Microsoft Source Code Samples. ! 4: * Copyright (C) 1993 Microsoft Corporation. ! 5: * All rights reserved. ! 6: * This source code is only intended as a supplement to ! 7: * Microsoft Development Tools and/or WinHelp documentation. ! 8: * See these sources for detailed information regarding the ! 9: * Microsoft samples programs. ! 10: \******************************************************************************/ ! 11: 1.1 root 12: /*************************************************************************\ 13: * PROGRAM: sharemem.c 14: * 15: * PURPOSE: 16: * 17: * To demonstrate the use of named shared memory between two independent 18: * processes. 19: * 20: * GLOBAL VARIABLES: 21: * 22: * HANDLE hInst: - Instance handle. 23: * 24: * HWND hWnd: - Client window handle. 25: * 26: * HWND hWndDlg: - Handle to the dialog box which is imposed over the 27: * parent window. 28: * 29: * LONG *MapView: - Address of where the file will be mapped to. 30: * 31: * HANDLE hMapObject: 32: * - Handle to the file mapping object. 33: * 34: * 35: * FUNCTIONS: 36: * 37: * WinMain() - Initializes the window, and process the message loop. 38: * MainWndProc() - To handle the messages to the main window. 39: * 40: * COMMENTS: 41: * 42: * Overview: 43: * This sample is accompanied by the Othrproc (Other Procedure) 44: * application. Basically this code traps WM_MOUSEMOVE messages 45: * and writes them to the shared memory. The Otherproc code 46: * reads the share memory and blts a small bitmap in the same 47: * relative coordinates. The effect is to have the mouse move 48: * in one window, while the bitmap moves in the same relative 49: * position in the other window. The mouse coordinates will appear 50: * in the edit fields of the dialog box at the top of the parent 51: * windows. 52: * 53: * To Use: 54: * First start Sharemem, then start Otherproc. The visual effect 55: * is better if the focus remains with Otherproc, while the mouse 56: * moves with the Sharemem window. 57: * 58: \*************************************************************************/ 59: 60: #include <windows.h> 61: #include <stdlib.h> 62: #include <stdio.h> 63: #include "sharemem.h" 64: 65: #define WERR(who,where) {sprintf(Buf,"ERROR: %s returned %u, line: %u", who, GetLastError(), __LINE__);\ 66: sprintf(Buf2,"From within %s", where);\ 67: MessageBox(hwnd, Buf, Buf2, MB_OK);} 68: 69: CHAR Buf[80]; 70: CHAR Buf2[80]; 71: 72: 73: HANDLE hInst; 74: HANDLE hMapObject; 75: HWND hWnd, hWndDlg; 76: LONG *MapView; 77: 78: 79: 80: /*************************************************************************\ 81: * 82: * FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int) 83: * 84: * PURPOSE: Initializes the window, processes message loop. 85: * 86: * COMMENTS: A fairly standard WinMain except that it creates a dialog 87: * box which is imposed on the upper part of the window. This 88: * dialog box is used to show the mouse coordinates. 89: * 90: \*************************************************************************/ 91: 92: int APIENTRY WinMain (HANDLE hInstance, 93: HANDLE hPrevInstance, 94: LPSTR lpCmdLine, 95: int nCmdShow) 96: 97: 98: { 99: 100: MSG msg; 101: WNDCLASS wc; 102: RECT rect; 103: 104: UNREFERENCED_PARAMETER( lpCmdLine ); 105: UNREFERENCED_PARAMETER( hPrevInstance ); 106: 107: hInst = hInstance; 108: 1.1.1.2 ! root 109: wc.style = 0; 1.1 root 110: wc.lpfnWndProc = (WNDPROC)MainWndProc; 111: wc.cbClsExtra = 0; 112: wc.cbWndExtra = 0; 113: wc.hInstance = hInstance; 114: wc.hIcon = LoadIcon (NULL, IDI_APPLICATION); 115: wc.hCursor = LoadCursor (NULL, IDC_ARROW); 116: wc.hbrBackground = GetStockObject (WHITE_BRUSH); 117: wc.lpszMenuName = ""; 118: wc.lpszClassName = "ShareMemWClass"; 119: 120: RegisterClass(&wc); 121: 122: hWnd = CreateWindow ("ShareMemWClass", 123: "Shared Memory Sample", 124: WS_OVERLAPPEDWINDOW, 125: CW_USEDEFAULT, 126: CW_USEDEFAULT, 127: CW_USEDEFAULT, 128: CW_USEDEFAULT, 129: NULL, 130: NULL, 131: hInstance, 132: NULL); 133: 134: hWndDlg = CreateDialog (hInst, "StatusDlg", hWnd, NULL); 135: 136: GetClientRect (hWnd, &rect); 137: SendMessage (hWnd, WM_SIZE, 0, (rect.right - rect.left)); 138: ShowWindow (hWndDlg, SW_SHOW); 139: ShowWindow (hWnd, nCmdShow); 140: 1.1.1.2 ! root 141: while (GetMessage (&msg, NULL, 0, 0)) 1.1 root 142: if (!IsDialogMessage (hWndDlg, &msg)) 143: { 144: DispatchMessage (&msg); // Dispatch message to window. 145: } 146: 147: return (msg.wParam); // Returns value from PostQuitMessage. 148: 149: } 150: 151: /*************************************************************************\ 152: * 153: * FUNCTION: MainWndProc (HWND, UINT, UINT, LONG) 154: * 155: * PURPOSE: To process messages. To create the shared memory. 156: * 157: * MESSAGES: 158: * 159: * WM_CREATE - Create the shared memory by calling 160: * CreateFileMapping(). This memory is the size 161: * of a DWORD, all the size that is needed to pass 162: * mouse coordinates. If the CreateFileMapping() 163: * call is successful, it then calls MapViewOfFile() 164: * to get the address of the memory. 165: * 166: * WM_MOUSEMOVE - Traps the mouse move messages, and uses structured 167: * exception handling to write the mouse coordinates 168: * to the share memory. It also sends the coordinates 169: * to the edit fields of the dialog box. 170: * 171: * WM_DESTROY - Closes the memory handle, and destroys the window. 172: * 173: * WM_SIZE - Updates the size of the dialog box to maintain 174: * its relative size to the client window. 175: * 176: * CALLED BY: 177: * 178: * WinMain(); 179: * 180: \*************************************************************************/ 181: 182: LONG APIENTRY MainWndProc (HWND hwnd, 183: UINT message, 184: UINT wParam, 185: LONG lParam) 186: { 187: 188: switch (message) 189: { 190: 191: case WM_CREATE: 192: hMapObject = CreateFileMapping ((HANDLE) 0xFFFFFFFF, 193: NULL, 194: PAGE_READWRITE, 195: 0, 196: sizeof(DWORD), 197: "shared_memory"); 198: if (!hMapObject) 199: { 200: WERR("CreateFileMapping","WM_CREATE"); 201: } 202: (LPVOID)MapView = MapViewOfFile (hMapObject, 203: FILE_MAP_WRITE, 204: 0, 0, 0); 205: if (!MapView) 206: MessageBox (hwnd, "ERROR: MapViewOfFile was not successful.", 207: "From within WM_CREATE", MB_OK); 208: return (0); 209: 210: 211: case WM_SIZE : 1.1.1.2 ! root 212: SetWindowPos (hWndDlg, NULL, 0,0, LOWORD(lParam), DIALOGHEIGHT, 0); 1.1 root 213: return (0); 214: 215: 216: case WM_MOUSEMOVE: 217: 1.1.1.2 ! root 218: __try 1.1 root 219: { 220: MapView[0] = lParam; 221: } 1.1.1.2 ! root 222: __except(EXCEPTION_EXECUTE_HANDLER) 1.1 root 223: { 224: MessageBox( hwnd, "Trapped an exception when writing to memory.", 225: "From within WM_MOUSEMOVE", MB_OK); 226: } 227: 228: SetDlgItemInt (hWndDlg, IDE_XCOORD, LOWORD(lParam), FALSE); 229: SetDlgItemInt (hWndDlg, IDE_YCOORD, HIWORD(lParam), FALSE); 230: return (0); 231: 232: case WM_DESTROY : 233: CloseHandle (hMapObject); 234: PostQuitMessage (0); 235: return (0); 236: 237: } 238: return DefWindowProc (hwnd, message, wParam, lParam); 239: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.