Annotation of q_a/samples/othrproc/othrproc.c, revision 1.1.1.2

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: othrproc.c
                     14: *
                     15: *  PURPOSE:
                     16: *
                     17: *    To assist in showing how to processes can share named shared memory.
                     18: *
                     19: *  GLOBAL VARIABLES:
                     20: *
                     21: *    HANDLE hInst:  - Instance handle.
                     22: *
                     23: *    HWND   hWnd:   - Client window handle.
                     24: *
                     25: *    HWND   hWndDlg:- Window handle to upper dialog box.
                     26: *
                     27: *    HANDLE hMapObject:
                     28: *                   - Handle to the file mapping object.
                     29: *
                     30: *    LONG *MapView: - Base address of the share memory.
                     31: *
                     32: *    CHAR Buf[80], Buf2[80]:
                     33: *                   - Error buffers.
                     34: *
                     35: *
                     36: *  FUNCTIONS:
                     37: *
                     38: *    WinMain()      - Initializes the window, and process the message loop.
                     39: *    MainWndProc()  - To handle the messages to the main window.
                     40: *    ReadProc()     - A thread which loops; reading the shared memory
                     41: *                     and drawing a bitmap in a location indicated by
                     42: *                     the values in the share memory.
                     43: *
                     44: *  COMMENTS:
                     45: *
                     46: *    Overview:
                     47: *      This sample is meant to be run with the Sharemem sample.  This
                     48: *      code basically reads the shared memory, and based on the values
                     49: *      found there (an X and Y coordinate representing the mouse
                     50: *      position in the window of the Sharemem sample), this code draws
                     51: *      a bitmap in the same relative position.
                     52: *
                     53: *    To Use:
                     54: *      Start the Sharemem sample first, and then this one.  Set the
                     55: *      focus to this application, and then move the mouse in the window
                     56: *      of the Sharemem application.  A bitmap will be drawn to represent
                     57: *      the relative mouse position in the other application, and the
                     58: *      mouse's X and Y coordinates will appear in the edit fields in
                     59: *      the dialog box imposed on the top of the client space.
                     60: *
                     61: \*************************************************************************/
                     62: 
                     63: #include <windows.h>
                     64: #include <stdlib.h>
                     65: #include <stdio.h>
                     66: #include "othrproc.h"
                     67: 
                     68: #define WERR(who,where)    {sprintf(Buf,"ERROR: %s returned %u, line: %u", who, GetLastError(), __LINE__);\
                     69:                             sprintf(Buf2,"From within %s", where);\
                     70:                             MessageBox(hwnd, Buf, Buf2, MB_OK);}
                     71: 
                     72: CHAR Buf[80];
                     73: CHAR Buf2[80];
                     74: 
                     75: HANDLE hInst;
                     76: HANDLE hMapObject;
                     77: HWND   hWnd, hWndDlg;
                     78: HANDLE hThread;
                     79: LONG   *MapView;
                     80: 
                     81: 
                     82: 
                     83: /*************************************************************************\
                     84: *
                     85: *  FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
                     86: *
                     87: *  PURPOSE:  To initialize the window, and process the message loop.
                     88: *
                     89: *  COMMENTS: A fairly standard WinMain except that it creates a dialog
                     90: *            box which is imposed on the upper part of the window.  This
                     91: *            dialog box is used to show the mouse coordinates.
                     92: *
                     93: \*************************************************************************/
                     94: 
                     95: int APIENTRY WinMain (HANDLE hInstance,
                     96:                       HANDLE hPrevInstance,
                     97:                       LPSTR  lpCmdLine,
                     98:                       int    nCmdShow)
                     99: 
                    100: 
                    101: {
                    102: 
                    103:   MSG  msg;
                    104:   WNDCLASS wc;
                    105:   RECT rect;
                    106: 
                    107:   UNREFERENCED_PARAMETER( lpCmdLine );
                    108:   UNREFERENCED_PARAMETER( hPrevInstance );
                    109: 
                    110:   hInst = hInstance;
                    111: 
1.1.1.2 ! root      112:   wc.style = 0;                          // Replaces CS_SIZEREDRAW.
1.1       root      113:   wc.lpfnWndProc = (WNDPROC)MainWndProc; // The client window procedure.
                    114:   wc.cbClsExtra = 0;                     // No room reserved for extra data.
                    115:   wc.cbWndExtra = 0;
                    116:   wc.hInstance = hInstance;
                    117:   wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
                    118:   wc.hCursor = LoadCursor (NULL, IDC_ARROW);
                    119:   wc.hbrBackground = GetStockObject (WHITE_BRUSH);
                    120:   wc.lpszMenuName = "";
                    121:   wc.lpszClassName = "OthrProcWClass";
                    122: 
                    123:   RegisterClass(&wc);
                    124: 
                    125:   hWnd = CreateWindow ("OthrProcWClass",
                    126:                        "Other Procedure",
                    127:                        WS_OVERLAPPEDWINDOW,
                    128:                        CW_USEDEFAULT,
                    129:                        CW_USEDEFAULT,
                    130:                        CW_USEDEFAULT,
                    131:                        CW_USEDEFAULT,
                    132:                        NULL,
                    133:                        NULL,
                    134:                        hInstance,
                    135:                        NULL);
                    136: 
                    137:   hWndDlg     = CreateDialog (hInst, "StatusDlg", hWnd, NULL);
                    138: 
                    139:   GetClientRect (hWnd, &rect);
                    140:   SendMessage (hWnd, WM_SIZE, 0, (rect.right - rect.left));
                    141:   ShowWindow  (hWndDlg,  SW_SHOW);
                    142:   ShowWindow  (hWnd, nCmdShow);
                    143: 
1.1.1.2 ! root      144:   while (GetMessage (&msg, NULL, 0, 0))
1.1       root      145:     if (!IsDialogMessage (hWndDlg, &msg))
                    146:       {
                    147:        DispatchMessage (&msg);   // Dispatch message to window.
                    148:       }
                    149: 
                    150:   return (msg.wParam);           // Returns value from PostQuitMessage.
                    151: 
                    152: }
                    153: 
                    154: /*************************************************************************\
                    155: *
                    156: *  FUNCTION:  MainWndProc (HWND, UINT, UINT, LONG)
                    157: *
                    158: *  PURPOSE:   To process messages.  To open and gain access to the shared
                    159: *             memory.
                    160: *
                    161: *  VARIABLES USED:
                    162: *
                    163: *    - ThreadID:
                    164: *             DWORD, used in the CreateThread call.
                    165: *
                    166: *  MESSAGES:
                    167: *
                    168: *    WM_DESTROY      - Cleans up handles and destroys the window.
                    169: *    WM_SIZE         - Sizes the dialog box to the client window.
                    170: *    WM_CREATE       - First it opens the shared memory using
                    171: *                      OpenFileMapping().  It then gets an address to
                    172: *                      the shared memory by using MapViewOfFile().
                    173: *                      If both calls were successful, this code then
                    174: *                      creates a thread which continually reads the
                    175: *                      shared memory and draws the bitmap.
                    176: *
                    177: *  CALLED BY:
                    178: *
                    179: *    WinMain();
                    180: *
                    181: *  CALLS TO:
                    182: *
                    183: *    ReadProc();
                    184: *
                    185: \*************************************************************************/
                    186: 
                    187: LONG APIENTRY MainWndProc (HWND hwnd,
                    188:                            UINT message,
                    189:                            UINT wParam,
                    190:                            LONG lParam)
                    191: {
                    192:   DWORD ThreadID;
                    193: 
                    194:   switch (message)
                    195:       {
                    196: 
                    197:         case WM_CREATE:
                    198:           hMapObject = OpenFileMapping (FILE_MAP_READ,
                    199:                                         FALSE,
                    200:                                         "shared_memory");
                    201: 
                    202:           if (!hMapObject)
                    203:             {
                    204:              WERR("OpenFileMapping","WM_CREATE");
                    205:              PostQuitMessage (0);
                    206:              return (0);
                    207:             }
                    208: 
                    209:           (LPVOID)MapView = MapViewOfFile (hMapObject,
                    210:                                            FILE_MAP_READ,
                    211:                                            0, 0, 0);
                    212:           if (!MapView)
                    213:             {
                    214:             MessageBox (hwnd, "ERROR: MapViewOfFile was not successful.",
                    215:                         "From within WM_CREATE", MB_OK);
                    216:             PostQuitMessage (0);
                    217:             return (0);
                    218:             }
                    219: 
                    220:           hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadProc,
                    221:                                  NULL, 0, &ThreadID);
                    222:           if (!hThread)
                    223:             {
                    224:              WERR("CreateThread", "WM_CREATE");
                    225:              PostQuitMessage(0);
                    226:             }
                    227: 
                    228:           return (0);
                    229: 
                    230:         case WM_SIZE :
1.1.1.2 ! root      231:           SetWindowPos (hWndDlg, NULL, 0,0, LOWORD(lParam), DIALOGHEIGHT, 0);
1.1       root      232:           return (0);
                    233: 
                    234:         case WM_DESTROY :
                    235:             CloseHandle (hMapObject);
                    236:             CloseHandle (hThread);
                    237:             PostQuitMessage (0);
                    238:             EndDialog(hWndDlg, 0);
                    239:             return (0);
                    240: 
                    241:        }
                    242:     return DefWindowProc (hwnd, message, wParam, lParam);
                    243: }
                    244: 
                    245: /*************************************************************************\
                    246: *
                    247: *  FUNCTION:  VOID ReadProc (VOID)
                    248: *
                    249: *  PURPOSE:   A thread which continually reads the shared memory for new
                    250: *             X and Y coordinates, and then draws the bitmap where indicated.
                    251: *
                    252: *  VARIABLES USED:
                    253: *
                    254: *    - hCrossHair:
                    255: *             A handle to the bitmap.
                    256: *
                    257: *    - lParam, oldParam:
                    258: *             LONGs used to hold the new and the old mouse coordinates.
                    259: *
                    260: *    - hDC, hDCMem:
                    261: *             The DC of the window, and a memory DC used as the source
                    262: *             of the bitmap.
                    263: *
                    264: *    - X, Y:  Integers representing the X and Y coordinates taken from
                    265: *             the shared memory and used to locate the bitmap.
                    266: *
                    267: *    - hBrush:
                    268: *             Handle to a brush, used to erase the old bitmap before
                    269: *             redrawing it in the new location.
                    270: *
                    271: *    - rect:  A RECT, used to blot out the old bitmap (erasing it);
                    272: *
                    273: *    - bm:    a BITMAP structure for the bitmap.
                    274: *
                    275: *    - ptSize, ptOrg:
                    276: *             POINTS used in transforming the coordinates for the bitmap.
                    277: *
                    278: *  COMMENTS:
                    279: *
                    280: *    This function first uses structured exception handling to try to
                    281: *    read the shared memory a first time.  If successful, it's assumed
                    282: *    that the rest of the read will not cause an exception.  The code then
                    283: *    sets a brush for erasing the bitmap, and loads the bitmap.  It then
                    284: *    begins to loop.  Each loop reads the memory.  If the value has
                    285: *    changed since the last read the old bitmap is erased by drawing a
                    286: *    rectangle over the old coordinates, and then the bitmap is moved
                    287: *    and redrawn using the new coordinates.
                    288: *
                    289: \*************************************************************************/
                    290: VOID ReadProc(VOID)
                    291: {
                    292:   HBITMAP hCrossHair;
                    293:   LONG lParam, oldParam;
                    294:   HDC hDC, hDCMem;
                    295:   int X, Y;
                    296:   HANDLE hBrush;
                    297:   RECT Rect;
                    298:   BITMAP bm;
                    299:   POINT ptSize, ptOrg;
                    300: 
1.1.1.2 ! root      301:   _try
1.1       root      302:     {
                    303:      lParam = MapView[0];
                    304:     }
1.1.1.2 ! root      305:   _except(EXCEPTION_EXECUTE_HANDLER)
1.1       root      306:     {
                    307:       MessageBox( hWnd, "Trying to read shared memory caused an exception.",
                    308:                   "From within WM_MOUSEMOVE", MB_OK);
1.1.1.2 ! root      309:       PostMessage(hWnd, WM_DESTROY, 0, 0);
1.1       root      310:     }
                    311: 
                    312:   hBrush = GetStockObject(WHITE_BRUSH);
                    313:   X = LOWORD(lParam);
                    314:   Y = HIWORD(lParam);
                    315: 
                    316:   hCrossHair = LoadBitmap(hInst, "CrossHair");
                    317: 
                    318:   while (1)
                    319:     {
                    320:     lParam = MapView[0];
                    321: 
                    322:     if (lParam != oldParam)
                    323:       {
                    324: 
                    325:       hDC = GetDC(hWnd);
                    326: 
                    327:       Rect.top = Y; Rect.left = X; Rect.bottom = Y+16; Rect.right = X+16;
                    328:       FillRect(hDC, &Rect, hBrush);
                    329: 
                    330: 
                    331:       X = LOWORD(lParam);
                    332:       Y = HIWORD(lParam);
                    333: 
                    334:       hDCMem = CreateCompatibleDC(hDC);
                    335:       SelectObject(hDCMem, hCrossHair);
                    336:       SetMapMode(hDCMem, GetMapMode(hDC));
                    337: 
                    338:       GetObject(hCrossHair, sizeof(BITMAP), &bm);
                    339: 
                    340:       ptSize.x = bm.bmWidth;
                    341:       ptSize.y = bm.bmHeight;
                    342:       DPtoLP (hDC, &ptSize, 1);
                    343: 
                    344:       ptOrg.x = 0;
                    345:       ptOrg.y = 0;
                    346:       DPtoLP (hDCMem, &ptOrg, 1);
                    347: 
                    348:       BitBlt(hDC, X, Y, ptSize.x, ptSize.y,
                    349:              hDCMem, ptOrg.x, ptOrg.y, SRCCOPY);
                    350: 
                    351:       oldParam = lParam;
                    352:       SetDlgItemInt (hWndDlg, IDE_XCOORD, X, FALSE);
                    353:       SetDlgItemInt (hWndDlg, IDE_YCOORD, Y, FALSE);
                    354:       ReleaseDC(hWnd, hDC);
                    355:       DeleteDC(hDCMem);
                    356: 
                    357:       Sleep(100);
                    358:       }
                    359: 
                    360:     }
                    361: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.