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