home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / ipxrcv.exe / IPXREC.C < prev    next >
C/C++ Source or Header  |  1995-08-25  |  16KB  |  416 lines

  1. /*
  2.  
  3. Copyright (c) 1992 Novell, Inc.  All Rights Reserved.
  4.  
  5. THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
  6. TREATIES.  USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE
  7. LICENSE AGREEMENT ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK)
  8. THAT CONTAINS THIS WORK.
  9.  
  10. Pursuant to the SDK License Agreement, Novell hereby grants to
  11. Developer a royalty-free, non-exclusive license to include the
  12. sample code IPXREC.C and derivative binaries in its product.
  13. Novell grants to Developer worldwide distribution rights to market,
  14. distribute or sell the sample code IPXREC.C and derivative
  15. binaries as a component of Developer's product(s).  Novell shall
  16. have no obligations to Developer or Developer's customers with
  17. respect to this code.
  18.  
  19. DISCLAIMER:
  20.  
  21.    Novell, Inc. makes no representations or warranties with respect
  22. to the contents or use of this code, and specifically disclaims any
  23. express or implied warranties of merchantability or fitness for any
  24. particular purpose.  Further, Novell, Inc. reserves the right to revise
  25. this publication and to make changes to its content, at any time,
  26. without obligation to notify any person or entity of such revisions or
  27. changes.
  28.  
  29.    Further, Novell, Inc. makes no representations or warranties with
  30. respect to any software, and specifically disclaims any express or
  31. implied warranties of merchantability or fitness for any particular
  32. purpose.  Further, Novell, Inc. reserves the right to make changes to
  33. any and all parts of the software, at any time, without obligation to
  34. notify any person or entity of such changes.
  35.  
  36. ***************************************************************************
  37.    IPXREC.C
  38. **************************************************************************/
  39.  
  40. /**************************************************************************
  41. ** Description:  Sample code that demonstrates the how to set up to receive
  42. **                  an IPX packet within Windows. The partner program to this
  43. **               one is IPXSND. 
  44. ** Compiler:     MSC Visual C++ v1.51 for 'C' code.
  45. **               MASM v6.00 for 'ASM' code.
  46. **
  47. ** Programmer :  Karl Bunnell
  48. ** Date:         08/24/1995
  49. **
  50. */
  51.  
  52. #include <windows.h>                    /* required for all Windows applications */  
  53. #include <stdlib.h>   
  54. #include <mmsystem.h>
  55. #include <string.h>
  56.  
  57. #define NWWIN
  58. #define BIG_MEMORY 
  59. #define PROTOTYPE 
  60. #define WINDOWS 
  61. #include <nwcalls.h>
  62. #include "nxtw.h"
  63. #include "ipxrec.h"                    /* specific to this program                     */
  64. #include <ctype.h>
  65.  
  66. #define  MAX_NUMBER_MESSAGES      50
  67.  
  68. void SizeTheWindow (short *, short *, short *, short *) ;
  69. void WndPaint (HWND,  HDC) ;
  70.  
  71. HANDLE hInst;                            /* current instance                             */
  72. HWND    myWinHandle;
  73.  
  74. WORD socket = 0x0000;
  75.  
  76. WORD MySegment;
  77.  
  78. HCURSOR hSaveCursor;                        /* handle to current cursor      */
  79. HCURSOR hHourGlass;                         /* handle to hourglass cursor    */
  80.  
  81.  
  82. ECB listenECB[MAX_LISTEN_ECBS];
  83. IPXHeader IPXReceive[MAX_LISTEN_ECBS];
  84. DISPLAYDATA  displayData[MAX_LISTEN_ECBS];
  85. DWORD IPXTaskID = 0x00000000;
  86. IPXAddress   myNetAddr;
  87. static char            displayMessage[1024];
  88. static char            processMessage[1024];
  89.  
  90. TEXTMETRIC    tm;
  91.  
  92.  
  93. /****************************************************************************
  94.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  95.     PURPOSE: calls initialization function, processes message loop
  96. ****************************************************************************/
  97. int PASCAL WinMain(
  98.     HANDLE hInstance,                             /* current instance             */
  99.     HANDLE hPrevInstance,                             /* previous instance             */
  100.     LPSTR lpCmdLine,                             /* command line                     */
  101.     int nCmdShow)                                   /* show-window type (open/icon) */
  102. {
  103.     MSG msg;                                    /* message                             */
  104.     int sizeOfQueue;
  105.     BOOL bQueueCreated;
  106.  
  107.     sizeOfQueue = MAX_NUMBER_MESSAGES;
  108.     while( ( sizeOfQueue > 0 ) && ( !bQueueCreated ) )
  109.     {
  110.         bQueueCreated = SetMessageQueue( sizeOfQueue );
  111.         sizeOfQueue--;
  112.     }
  113.     if( sizeOfQueue == 0 )
  114.         return( NULL );
  115.     
  116.     lpCmdLine = lpCmdLine;
  117.     if (!hPrevInstance)                         /* Other instances of app running? */
  118.         if (!InitApplication(hInstance))    /* Initialize shared things */
  119.             return (FALSE);                         /* Exits if unable to initialize     */
  120.  
  121.     if (!InitInstance(hInstance, nCmdShow))
  122.         return (FALSE);
  123.  
  124.         
  125.    /* Acquire and dispatch messages until a WM_QUIT message is received. */
  126.     while (GetMessage(&msg,                         /* message structure */
  127.         NULL,                                       /* handle of window receiving the message */
  128.         0,                                          /* lowest message to examine */
  129.         0))                                         /* highest message to examine */
  130.     {
  131.         TranslateMessage(&msg);                     /* Translates virtual key codes */
  132.         DispatchMessage(&msg);                      /* Dispatches message to window */
  133.     }
  134.     return (msg.wParam);                            /* Returns the value from PostQuitMessage */
  135. }
  136.  
  137. /****************************************************************************
  138.     FUNCTION: InitApplication(HANDLE)
  139.     PURPOSE: Initializes window data and registers window class
  140. ****************************************************************************/
  141. BOOL FAR InitApplication(
  142.     HANDLE hInstance)       /* current instance */
  143. {
  144.     WNDCLASS  wc;
  145.  
  146.     /* Fill in window class structure with parameters that describe the       */
  147.     /* main window.                                                           */
  148.     wc.style = NULL;                    /* Class style(s).                    */
  149.     wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for  */
  150.                                         /* windows of this class.             */
  151.     wc.cbClsExtra = 0;                  /* No per-class extra data.           */
  152.     wc.cbWndExtra = 0;                  /* No per-window extra data.          */
  153.     wc.hInstance = hInstance;           /* Application that owns the class.   */
  154.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  155.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  156.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  157.     wc.lpszMenuName =  NULL;   /* Name of menu resource in .RC file. */
  158.     wc.lpszClassName = "IPXWClass"; /* Name used in call to CreateWindow. */
  159.     hHourGlass = LoadCursor(NULL, IDC_WAIT);
  160.     /* Register the window class and return success/failure code. */
  161.     return (RegisterClass(&wc));
  162. }
  163.  
  164. /****************************************************************************
  165.     FUNCTION:  InitInstance(HANDLE, int)
  166.     PURPOSE:  Saves instance handle and creates main window
  167. ****************************************************************************/
  168. BOOL InitInstance(
  169.     HANDLE          hInstance,          /* Current instance identifier.       */
  170.     int             nCmdShow)           /* Param for first ShowWindow() call. */
  171. {
  172.      short       xStart, yStart, xClient, yClient ;
  173.  
  174.     /* Save the instance handle in static variable, which will be used in  */
  175.     /* many subsequence calls from this application to Windows.            */
  176.     hInst = hInstance;  
  177.  
  178.     SizeTheWindow (&xStart, &yStart, &xClient, &yClient) ;
  179.  
  180.   
  181.     /* Create a main window for this application instance.  */
  182.     myWinHandle = CreateWindowEx(
  183.         WS_EX_TOPMOST,          /* extended flag to make this window the topmost window */
  184.         "IPXWClass",                    /* See RegisterClass() call.          */
  185.         "Phone Stats",               /* Text for window title bar.         */
  186.         WS_POPUP | WS_DLGFRAME | WS_SYSMENU,            /* Window style.                      */
  187.        xStart,                  /* Default horizontal position.       */
  188.         yStart,                  /* Default vertical position.         */
  189.         xClient,                  /* Default width.                     */
  190.         yClient,                  /* Default height.                    */
  191.         NULL,                           /* Overlapped windows have no parent. */
  192.         NULL,                        /* Use the window class menu.         */
  193.         hInstance,                      /* This instance owns this window.    */
  194.         NULL                            /* Pointer not needed.                */
  195.     );
  196.  
  197.     /* If window could not be created, return "failure" */
  198.     if (!myWinHandle)
  199.         return (FALSE);
  200.  
  201.     /* Make the window visible; update its client area; and return "success" */
  202.     ShowWindow(myWinHandle, SW_SHOWNOACTIVATE);  /* Show the window                        */
  203.     UpdateWindow(myWinHandle);          /* Sends WM_PAINT message                 */
  204.     return (TRUE);               /* Returns the value from PostQuitMessage */
  205. }
  206.  
  207. /****************************************************************************
  208.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  209.     PURPOSE:  Processes messages
  210.     MESSAGES:
  211.         WM_COMMAND    - application menu (About dialog box)
  212.         WM_DESTROY    - destroy window
  213. ****************************************************************************/
  214. long FAR PASCAL MainWndProc(
  215.     HWND        hWnd,                        /* window handle                     */
  216.     unsigned    message,                     /* type of message                   */
  217.     WORD        wParam,                      /* additional information            */
  218.     LONG        lParam)                      /* additional information            */
  219. {
  220.       PAINTSTRUCT     ps;
  221.       HDC hDC;
  222.      static char     str[100];
  223.      WORD         cCode;
  224.       
  225.     switch (message) {
  226.  
  227.        case WM_LISTENESR:
  228.             ProcessPacket((ECB FAR *)lParam, hWnd);
  229.             break;
  230.       
  231.        case WM_CREATE:
  232.                   if(Init())
  233.                     PostMessage(hWnd, WM_DESTROY, 0, 0);
  234.                   
  235.                   
  236.                     __asm mov MySegment, ds;
  237.                    cCode =  GlobalPageLock (MySegment);
  238.                    if (cCode == 0)
  239.                     { /* GlobalPageLock failed */
  240.                        MessageBox (hWnd, "GlobalPageLock failed", "Error", MB_OK); 
  241.                        PostMessage(hWnd, WM_DESTROY, 0, 0);
  242.                     }
  243.                  
  244.                  cCode = GlobalPageLock((HGLOBAL) SELECTOROF(_ReceiveESRHandler));
  245.                    if (cCode == 0)
  246.                     { /* GlobalPageLock failed */
  247.                        MessageBox (hWnd, "GlobalPageLock failed", "Error", MB_OK); 
  248.                        PostMessage(hWnd, WM_DESTROY, 0, 0);
  249.                     }
  250.                  
  251.                  ListenForPacket();
  252.                  return 0;
  253.        
  254.         
  255.         case WM_LBUTTONDOWN:     
  256.                 PostMessage(hWnd, WM_SYSCOMMAND, SC_MOVE+2, 0);
  257.                 return 0;      
  258.                  
  259.                           
  260.         case WM_PAINT :
  261.                hDC = BeginPaint (hWnd, &ps) ;
  262.                WndPaint (hWnd, hDC) ;
  263.                EndPaint (hWnd, &ps) ;
  264.                return 0 ;
  265.                          
  266.         case WM_DESTROY:                  /* message: window being destroyed */
  267.             Terminate();
  268.             PostQuitMessage(0);
  269.             break;
  270.  
  271.         default:                          /* Passes it on if unproccessed    */
  272.             return (DefWindowProc(hWnd, message, wParam, lParam));
  273.     }
  274.     return (NULL);
  275. }
  276.  
  277.  
  278.  
  279. BOOL FAR PASCAL   ListenForPacket(void)
  280. {
  281.     int i;
  282.  
  283.     for (i=0; i<MAX_LISTEN_ECBS; i++)
  284.     {
  285.         /* prepare ipx header and allocate ecb structure */
  286.         _fmemset( (char *)&IPXReceive[i], '\0', sizeof(IPXHeader));
  287.         _fmemset( (char *)&listenECB[i], '\0', sizeof(ECB));
  288.  
  289.         /* set up ecb for listen */
  290.         listenECB[i].ESRAddress = (void far *)_ReceiveESRHandler;
  291.         listenECB[i].socketNumber = socket;
  292.         listenECB[i].fragmentCount = 2;
  293.         listenECB[i].fragmentDescriptor[0].address = (char far *)&(IPXReceive[i]);
  294.         listenECB[i].fragmentDescriptor[0].size = sizeof(IPXHeader);
  295.         listenECB[i].fragmentDescriptor[1].address = (char far *)&displayData[i];
  296.         listenECB[i].fragmentDescriptor[1].size = sizeof(DISPLAYDATA);
  297.  
  298.         /* listen for packet */
  299.         IPXListenForPacket(IPXTaskID,&listenECB[i]);
  300.     }
  301.     return (TRUE);
  302. }
  303.  
  304. BOOL FAR PASCAL Init()
  305. {
  306.    int ccode;
  307.     WORD psize;
  308.     IPXAddress    internetworkAddr;
  309.     DWORD IPXTaskID = 0L; 
  310.     
  311.     psize = IPXGetMaxPacketSize();
  312.     
  313.     ccode = IPXInitialize(&IPXTaskID, (WORD)MAX_LISTEN_ECBS, (WORD)psize);
  314.      if(ccode)
  315.          {
  316.         wsprintf(displayMessage, "IPXInitialize: %02X", ccode);
  317.         MessageBox(GetFocus(),displayMessage, "IPXInitialize", MB_ICONASTERISK | MB_OK);
  318.         return 1;
  319.          }                                                  
  320.          
  321.     ccode = IPXOpenSocket(IPXTaskID, &socket, 0xFF);
  322.     if(ccode && ccode !=0xFF)
  323.         {
  324.            wsprintf(displayMessage, "Open socket returned: %x", ccode);
  325.         MessageBox(GetFocus(),displayMessage, "Open Socket", MB_ICONASTERISK | MB_OK);
  326.         return 1;
  327.         } 
  328.     
  329.     IPXGetInternetworkAddress(IPXTaskID, (BYTE far *)&internetworkAddr);
  330.  
  331.  wsprintf(displayMessage, "   Address: %02X%02X%02X%02X:%02X%02X%02X%02X%02X%02X:%04X",
  332.     internetworkAddr.network[0],
  333.     internetworkAddr.network[1],
  334.     internetworkAddr.network[2],
  335.     internetworkAddr.network[3],
  336.     internetworkAddr.node[0],
  337.     internetworkAddr.node[1],
  338.     internetworkAddr.node[2],
  339.     internetworkAddr.node[3],
  340.     internetworkAddr.node[4],
  341.     internetworkAddr.node[5],
  342.     NWWordSwap(socket));
  343.     
  344. //       wsprintf(displayMessage, "Please send to the following address: %04X", NWWordSwap(socket));
  345.        MessageBox(GetFocus(),displayMessage, "Socket", MB_ICONASTERISK | MB_OK);
  346.         
  347.     return 0;        
  348. }
  349.  
  350.  
  351. BOOL FAR PASCAL Terminate()
  352. {
  353.     int ccode, i;
  354.  
  355.  
  356.     for (i=1; i<=MAX_LISTEN_ECBS; i++)
  357.      ccode = IPXCancelEvent(IPXTaskID,&listenECB[i]);
  358.  
  359.    IPXCloseSocket(IPXTaskID,socket);
  360.     ccode = IPXSPXDeinit(IPXTaskID); 
  361.     
  362.      GlobalPageUnlock(MySegment);
  363.      GlobalPageUnlock((HGLOBAL) SELECTOROF(_ReceiveESRHandler));
  364.     return TRUE;
  365. }
  366.  
  367. void far pascal ReceiveESR(ECB far *ecb)
  368. {
  369.  
  370.     PostMessage(myWinHandle, WM_LISTENESR, 0, (DWORD)ecb);
  371.     return;
  372. }
  373.  
  374. BOOL ProcessPacket(ECB FAR *ecb, HWND hwnd)
  375. {
  376.        DISPLAYDATA far *data; 
  377.     
  378.     data = (DISPLAYDATA far *)ecb->fragmentDescriptor[1].address;
  379.     
  380.     data->displayLineTwo[30] = '\0';
  381.     data->displayLineOne[30]  = '\0';
  382.               
  383.     wsprintf(processMessage, "%s\r\n%s ", data->displayLineOne  , data->displayLineTwo );
  384.  
  385.     IPXListenForPacket(IPXTaskID, ecb); 
  386.     InvalidateRect (hwnd, NULL, TRUE) ;
  387.         
  388.     return (TRUE);
  389. }
  390.  
  391.  
  392.  void WndPaint (HWND hwnd, HDC hdc)
  393.      {
  394.      RECT        rect ;
  395.           
  396.      GetClientRect (hwnd, &rect) ;
  397.      
  398.      DrawText (hdc, processMessage, -1, &rect, DT_CENTER | DT_NOCLIP) ;     
  399.      }
  400.  
  401. void SizeTheWindow (short *pxStart,  short *pyStart,
  402.                     short *pxClient, short *pyClient)
  403.      {
  404.      HDC        hdc ;
  405.      TEXTMETRIC tm ;
  406.  
  407.      hdc = CreateIC ("DISPLAY", NULL, NULL, NULL) ;
  408.      GetTextMetrics (hdc, &tm) ;
  409.      DeleteDC (hdc) ;
  410.  
  411.      *pxClient = 2 * GetSystemMetrics (SM_CXDLGFRAME) + 30*tm.tmAveCharWidth ;
  412.      *pxStart  =     GetSystemMetrics (SM_CXSCREEN)   - *pxClient ;
  413.      *pyClient = 2 * GetSystemMetrics (SM_CYDLGFRAME) + 2*tm.tmHeight ;
  414.      *pyStart  =     GetSystemMetrics (SM_CYSCREEN)   - *pyClient ;
  415.      }
  416.