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

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

unix.superglobalmegacorp.com

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