home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / devtools / os2tk21j / c / samples / dllapi / dllapi.c__ / dllapi.c
Encoding:
C/C++ Source or Header  |  1993-03-12  |  23.8 KB  |  527 lines

  1. /*static char *SCCSID = "@(#)dllapi.c    6.13 92/02/18";*/
  2. /*==============================================================*\
  3.  *  DLLAPI.C - Sample PM application                            *
  4.  *                                                              *
  5.  *      (C) Copyright IBM Corporation 1992.                     *
  6.  *                                                              *
  7.  *--------------------------------------------------------------*
  8.  *                                                              *
  9.  *  This DLL application program consists of a DLL file that    *
  10.  *  provides a 32-bit DOS called functions, and an API's EXE    *
  11.  *  file that uses the dynamic linking library to handle some   *
  12.  *  operations. The DLL uses protected memory to protect its    *
  13.  *  shared data, and exception management to validate the API's *
  14.  *  pointer parameters, handle the errors returned.             *
  15.  *  This application also serves as a template that can be      *
  16.  *  easily modified by an application developer.  The source    *
  17.  *  files are organized so that the overhead code that should   *
  18.  *  be in all applications is located in the same files so      *
  19.  *  that these files do not need to be modified.  The routines  *
  20.  *  that deal with application specific code are also located   *
  21.  *  in their own modules.  An application developer need only   *
  22.  *  change these files in order to modify this template for     *
  23.  *  his application.                                            *
  24.  *                                                              *
  25.  *--------------------------------------------------------------*
  26.  *                                                              *
  27.  *  This source file contains the following functions:          *
  28.  *                                                              *
  29.  *      main() - main routine                                   *
  30.  *      MainWndProc(hwnd, msg, mp1, mp2) - main window procedure*
  31.  *      MainCommand(hwnd, mp1, mp2) - WM_COMMAND processing     *
  32.  *      MessageBox(hwnd nId, pszMsg, fsStyle, bBeep)            *
  33.  *                                                              *
  34. \*==============================================================*/
  35. /*--------------------------------------------------------------*\
  36.  *  Include files, macros, defined constants, and externs       *
  37. \*--------------------------------------------------------------*/
  38.  
  39. #include "dllapi.h"
  40.  
  41. /****************************************************************\
  42.  *  Main routine                                                *
  43.  *--------------------------------------------------------------*
  44.  *                                                              *
  45.  *  Name:    main()                                             *
  46.  *                                                              *
  47.  *  Purpose: Initializes the PM environment, calls the          *
  48.  *           initialization routine, creates the main window,   *
  49.  *           and polls the message queue.                       *
  50.  *                                                              *
  51.  *  Usage:                                                      *
  52.  *                                                              *
  53.  *  Method:  -obtains anchor block handle and creates message   *
  54.  *              queue                                           *
  55.  *           -calls the initialization routine                  *
  56.  *           -creates the main frame window which creates the   *
  57.  *              main client window                              *
  58.  *           -polls the message queue via Get/Dispatch Msg loop *
  59.  *           -upon exiting the loop, exits                      *
  60.  *                                                              *
  61.  *  Returns: 1 - if sucessful execution completed               *
  62.  *           0 - if error                                       *
  63.  *                                                              *
  64. \****************************************************************/
  65. INT main(VOID)
  66. {
  67.    QMSG qmsg;                                      /* message structure */
  68.    ULONG ctlData=FCF_STANDARD;               /* main frame control data */
  69.    EXCEPTIONREGISTRATIONRECORD ExceptionStruct;
  70.  
  71.    strcpy(szWindowText,"DLLAPI Sample");
  72.    hwndChild = (HWND)0;
  73.    hab = WinInitialize(0);
  74.    if(!hab)
  75.    {
  76.        DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  77.        DosExit(EXIT_PROCESS, RETURN_ERROR);
  78.    }
  79.    hmq = WinCreateMsgQueue(hab, 0);
  80.    if(!hmq)
  81.    {
  82.        DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  83.        WinTerminate(hab);
  84.        DosExit(EXIT_PROCESS, RETURN_ERROR);
  85.    }
  86.    if(!Init())
  87.    {
  88.        MessageBox(HWND_DESKTOP, IDMSG_INITFAILED, "Error !",
  89.                            MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  90.        DosExit(EXIT_PROCESS, RETURN_ERROR);
  91.    }
  92.                                               /* create the main window */
  93.    hwndMainFrame = WinCreateStdWindow(HWND_DESKTOP,
  94.                                       WS_VISIBLE,
  95.                                       (PVOID)&ctlData,
  96.                                       (PSZ)szAppName,
  97.                                       (PSZ)NULL,
  98.                                       WS_VISIBLE,
  99.                                       (HMODULE)NULL,
  100.                                       ID_RESOURCE,
  101.                                       (PHWND)&hwndMain);
  102.    if(!hwndMainFrame)
  103.    {
  104.        MessageBox(HWND_DESKTOP, IDMSG_MAINWINCREATEFAILED, "Error !",
  105.                            MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  106.        DosExit(EXIT_PROCESS, RETURN_ERROR);
  107.    }
  108.    InitHelp();
  109.    ExceptionStruct.ExceptionHandler = (_ERR *)&GPFHandler;
  110.    DosSetExceptionHandler(&ExceptionStruct) ;
  111.  
  112.    while(WinGetMsg(hmq, (PQMSG)&qmsg, 0L, 0L, 0L))
  113.            WinDispatchMsg(hmq, (PQMSG)&qmsg);
  114.  
  115.    DosUnsetExceptionHandler(&ExceptionStruct) ;
  116.                                            /* destroy the help instance */
  117.    DestroyHelpInstance();
  118.                                     /* will normally be put in ExitProc */
  119.    DosExit(EXIT_PROCESS, RETURN_SUCCESS);
  120.    return 0;
  121. }                                                              /* main() */
  122.  
  123. /****************************************************************\
  124.  *  Main client window procedure                                *
  125.  *--------------------------------------------------------------*
  126.  *                                                              *
  127.  *  Name:    MainWndProc(hwnd, msg, mp1, mp2)                   *
  128.  *                                                              *
  129.  *  Purpose: Processes the messages sent to the main client     *
  130.  *           window. This routine processes the basic messages  *
  131.  *           all client windows should process and passes all   *
  132.  *           others onto UserWndProc where the developer can    *
  133.  *           process any others.                                *
  134.  *                                                              *
  135.  *  Usage:   Called for each message placed in the main         *
  136.  *           window's message queue                             *
  137.  *                                                              *
  138.  *  Method:  a switch statement branches to the routines to be  *
  139.  *           performed for each message processed. Any messages *
  140.  *           not specifically process are passed to the user's  *
  141.  *           message processing procedure UserWndProc().        *
  142.  *                                                              *
  143.  *  Returns: Return values are determined by each message       *
  144.  *                                                              *
  145. \****************************************************************/
  146. MRESULT EXPENTRY MainWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  147. {
  148.    MRESULT sRC;
  149.  
  150.    switch(msg) {
  151.        case WM_PAINT:
  152.            MainPaint(hwnd);
  153.            break;
  154.  
  155.        case WM_COMMAND:
  156.            MainCommand(hwnd, mp1, mp2);
  157.            break;
  158.  
  159.    /*--------------------------------------------------*\
  160.     *      Any messages not processed are passed on    *
  161.     *      to the user's window proc.  It is           *
  162.     *      responsible for passing any messages it     *
  163.     *      doesn't handle onto WinDefWindowProc()      *
  164.    \*--------------------------------------------------*/
  165.        case HM_QUERY_KEYS_HELP:
  166.            return ((MRESULT)PANEL_KEYSHELP);
  167.  
  168.        default:
  169.            sRC = WinDefWindowProc(hwnd, msg, mp1, mp2);
  170.            return sRC;
  171.    }
  172.    return (MRESULT)0L; /*all window procedures should return 0 as a default*/
  173. }                                                        /* MainWndProc() */
  174.  
  175. /****************************************************************\
  176.  *  Main client painting routine                                *
  177.  *--------------------------------------------------------------*
  178.  *                                                              *
  179.  *  Name:    MainPaint(hwnd)                                    *
  180.  *                                                              *
  181.  *  Purpose: Paints the main client window.                     *
  182.  *                                                              *
  183.  *  Usage:   Routine is called whenver the client window        *
  184.  *           procedure receives a WM_PAINT message              *
  185.  *                                                              *
  186.  *  Method:  -begins painting by calling WinBeginPaint          *
  187.  *              and retriving the HPS for the window            *
  188.  *           -paint page info boxes                             *
  189.  *                                                              *
  190.  *           -ends painting by calling WinEndPaint              *
  191.  *                                                              *
  192.  *  Returns: VOID                                               *
  193.  *                                                              *
  194. \****************************************************************/
  195. VOID MainPaint(HWND hwnd)
  196. {
  197.    HPS    hps;                          /* Handle for painting           */
  198.    RECTL  rect;                         /* Rectangle struct for painting */
  199.    CHAR   szBuffer[BUFF_SIZE];
  200.    POINTL ptl;
  201.  
  202.    hps = WinBeginPaint(hwnd, 0L, (PRECTL)&rect);
  203.    WinFillRect(hps, (PRECTL)&rect, CLR_BACKGROUND);
  204.                               /* Fill update rectangle with window color */
  205.    WinQueryWindowRect(hwndMain, (PRECTL)&rect);
  206.    switch (sStatus) {
  207.    case IDM_SEARCH:
  208.       ptl.x = HORZ_INDENT;
  209.       ptl.y = rect.yTop - VERT_TEXT_POS;
  210.       memset(szBuffer, 0, sizeof(szBuffer));
  211.       strcpy(szBuffer, "Searching Path: [");
  212.       strcat(szBuffer, ObjInfo.szCurDriver);
  213.       strcat(szBuffer, ObjInfo.szCurPath);
  214.       strcat(szBuffer, "]");
  215.  
  216.       GpiCharStringAt(hps, &ptl, strlen(szBuffer), szBuffer);
  217.  
  218.       ptl.x = HORZ_INDENT;
  219.       ptl.y = rect.yTop - VERT_TEXT_POS * 2;
  220.       memset(szBuffer, 0, sizeof(szBuffer));
  221.       strcpy(szBuffer, "Searching Pattern: [");
  222.       strcat(szBuffer, ObjInfo.szCurPattern);
  223.       strcat(szBuffer, "]");
  224.  
  225.       GpiCharStringAt(hps, &ptl, strlen(szBuffer), szBuffer);
  226.  
  227.       break;
  228.    default:
  229.       break;
  230.    }                                                       /* endswitch */
  231.    WinEndPaint(hps);
  232.    return;
  233. }                                                        /* MainPaint() */
  234.  
  235. /****************************************************************\
  236.  *  Main window WM_COMMAND processing procedure                 *
  237.  *--------------------------------------------------------------*
  238.  *                                                              *
  239.  *  Name:   MainCommand(hwnd, mp1, mp2)                         *
  240.  *                                                              *
  241.  *  Purpose:Calls the appropriate procedures that deal with     *
  242.  *           the selected menu item.                            *
  243.  *                                                              *
  244.  *  Usage:  Routine is called whenever a WM_COMMAND message     *
  245.  *          is posted to the main window.                       *
  246.  *                                                              *
  247.  *  Method: a switch statement branches on the id of the        *
  248.  *          menu item that posted the message and the           *
  249.  *          appropriate action for that item is taken.  Any     *
  250.  *          menu ids that are not part of the standard menu     *
  251.  *          set are passed onto the user defined WM_COMMAND     *
  252.  *          processing procedure.                               *
  253.  *                                                              *
  254.  *  Returns: VOID                                               *
  255.  *                                                              *
  256. \****************************************************************/
  257. VOID MainCommand(HWND hwnd, MPARAM mp1, MPARAM mp2)
  258. {
  259.     switch(SHORT1FROMMP(mp1))
  260.     {
  261.         case IDM_GENERALHELP:
  262.            HelpExtended(mp2);
  263.            return;
  264.  
  265.         case IDM_USINGHELP:
  266.            HelpHelpForHelp(mp2);
  267.            return;
  268.  
  269.         case IDM_TUTORIAL:
  270.            HelpKeys(mp2);
  271.            return;
  272.  
  273.         case IDM_HELPINDEX:
  274.            HelpIndex(mp2);
  275.            return;
  276.  
  277.         case IDM_HELPPRODUCTINFO:
  278.            HelpAbout(mp2);
  279.            return;
  280.  
  281.     /*--------------------------------------------------*\
  282.      *      User command processing routine is called   *
  283.      *      here so any ids not procecessed here can be *
  284.      *      processed                                   *
  285.     \*--------------------------------------------------*/
  286.         default:
  287.             UserCommand(hwnd, mp1, mp2);
  288.             break;
  289.     }
  290.     return;
  291. }                                                /* MainCommand() */
  292.  
  293.  
  294. /****************************************************************\
  295.  *  Non-standard menu item command processing procedure         *
  296.  *--------------------------------------------------------------*
  297.  *                                                              *
  298.  *  Name:    UserCommand(hwnd, mp1, mp2)                        *
  299.  *                                                              *
  300.  *  Purpose: Process any WM_COMMAND messages send to hwndMain   *
  301.  *              that are not processed by MainCommand           *
  302.  *                                                              *
  303.  *  Usage:   Routine is called for each WM_COMMAND that is      *
  304.  *           not posted by a standard menu item                 *
  305.  *                                                              *
  306.  *  Method:  A switch statement branches control based upon     *
  307.  *           the id of the control which posted the message     *
  308.  *                                                              *
  309.  *  Returns: VOID                                               *
  310.  *                                                              *
  311. \****************************************************************/
  312. VOID UserCommand(HWND hwnd, MPARAM mp1, MPARAM mp2)
  313. {
  314.    HPS         hps;
  315.    RECTL       rect;
  316.    CHAR        szBuffer[BUFF_SIZE];
  317.    APIRET      Ret;
  318.    POINTL      ptl;
  319.  
  320.    switch (SHORT1FROMMP(mp1))
  321.    {
  322.        case IDM_SEARCH:
  323.            if (WinDlgBox(HWND_DESKTOP,
  324.                          hwnd,
  325.                          (PFNWP)GetPatternDlgProc,
  326.                          (HMODULE)0,
  327.                          (ULONG)IDD_GETPATTERN,
  328.                          (PVOID)&ObjInfo) )
  329.            {
  330.               sStatus = IDM_SEARCH;
  331.               hps = WinGetPS(hwnd);
  332.               WinQueryWindowRect(hwnd, &rect);
  333.               WinFillRect(hps, (PRECTL)&rect, CLR_BACKGROUND);
  334.               ptl.x = HORZ_INDENT;
  335.               ptl.y = rect.yTop - VERT_TEXT_POS;
  336.               memset(szBuffer, 0, sizeof(szBuffer));
  337.               strcpy(szBuffer, "Searching Path: [");
  338.               strcat(szBuffer, ObjInfo.szNewDriver);
  339.               strcat(szBuffer, ObjInfo.szNewPath);
  340.               strcat(szBuffer, "]");
  341.  
  342.               GpiCharStringAt(hps, &ptl, strlen(szBuffer), szBuffer);
  343.  
  344.               ptl.x = HORZ_INDENT;
  345.               ptl.y = rect.yTop - VERT_TEXT_POS * 2;
  346.               memset(szBuffer, 0, sizeof(szBuffer));
  347.               strcpy(szBuffer, "Searching Pattern: [");
  348.               strcat(szBuffer, ObjInfo.szNewPattern);
  349.               strcat(szBuffer, "]");
  350.  
  351.               GpiCharStringAt(hps, &ptl, strlen(szBuffer), szBuffer);
  352.               WinReleasePS(hps);
  353.  
  354.               Ret=SearchFile(&ObjInfo, &pFindFile);
  355.               if (Ret)
  356.               {
  357.                   switch(Ret) {
  358.                   case ERROR_FILE_NOT_FOUND:
  359.                        MessageBox(hwnd, IDMSG_FILENOTFOUND, "Warning !",
  360.                                 MB_OK | MB_INFORMATION | MB_MOVEABLE, TRUE);
  361.                        break;
  362.                   case ERROR_DRIVE_LOCKED:
  363.                        MessageBox(hwnd, IDMSG_DRIVELOCKED, "Error !",
  364.                                 MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  365.                        break;
  366.                   case ERROR_PATH_NOT_FOUND:
  367.                        MessageBox(hwnd, IDMSG_PATHNOTFOUND, "Error !",
  368.                                 MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  369.                        break;
  370.                   case RETURN_ERROR:
  371.                        MessageBox(hwnd, IDMSG_ERRORSETPATH, "Error !",
  372.                                 MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  373.                        break;
  374.                   case ERROR_BUFFER_OVERFLOW:
  375.                        MessageBox(hwnd, IDMSG_BUFFEROVERFLOW, "Error !",
  376.                                 MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  377.                        break;
  378.                   case ERROR_INVALID_DRIVE:
  379.                        MessageBox(hwnd, IDMSG_ERRORINVALIDDRIVE, "Error !",
  380.                                 MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  381.                        break;
  382.                   }                       /* End of switch(Ret)   */
  383.               }                           /* Endif of DisplayFile */
  384.               else
  385.               {
  386.                  memset(ObjInfo.szCurDriver,0,sizeof(ObjInfo.szCurDriver));
  387.                  memset(ObjInfo.szCurPath,0,sizeof(ObjInfo.szCurPath));
  388.                  memset(ObjInfo.szCurPattern,0,sizeof(ObjInfo.szCurPattern));
  389.                  strcpy(ObjInfo.szCurDriver,ObjInfo.szNewDriver);
  390.                  strcpy(ObjInfo.szCurPath,ObjInfo.szNewPath);
  391.                  strcpy(ObjInfo.szCurPattern,ObjInfo.szNewPattern);
  392.                  rect.yTop = rect.yTop-VERT_TEXT_POS*2-5;
  393.  
  394.                  if(WinDlgBox(HWND_DESKTOP,
  395.                            hwnd,
  396.                            (PFNWP)DispFileDlgProc,
  397.                            (HMODULE)0,
  398.                            (ULONG)IDD_DSPFILESTRUCT,
  399.                            (PVOID)NULL) )
  400.  
  401.                      DisplayFile(hwnd, rect, &pFlList);
  402.               }
  403.            }                           /* End of case(IDM_SEARCH) */
  404.            break;
  405.  
  406.        case IDM_OPEN:
  407.            if (WinDlgBox(HWND_DESKTOP,
  408.                          hwnd,
  409.                          (PFNWP)OpenDlgProc,
  410.                          (HMODULE)0,
  411.                          (ULONG)IDD_OPENFILE,
  412.                          (PVOID)&FileInfo) )
  413.            {
  414.               Ret=ReadFileProc(&FileInfo);
  415.               if(Ret)
  416.               {
  417.                  switch(Ret) {
  418.                  case ERROR_PATH_NOT_FOUND:
  419.                       MessageBox(hwnd, IDMSG_PATHNOTFOUND, "Error !",
  420.                                MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  421.                       break;
  422.                  case ERROR_OPEN_FAILED:
  423.                       MessageBox(hwnd, IDMSG_OPENFAILED, "Error !",
  424.                                MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  425.                       break;
  426.                  case ERROR_FILE_NOT_FOUND:
  427.                       MessageBox(hwnd, IDMSG_FILENOTFOUND, "Error !",
  428.                                MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  429.                       break;
  430.                  case ERROR_ACCESS_DENIED:
  431.                       MessageBox(hwnd, IDMSG_ACCESSDENIED, "Error !",
  432.                                MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  433.                       break;
  434.                  default:
  435.                       MessageBox(hwnd, IDMSG_RETURNERROR, "Error !",
  436.                                MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  437.                       break;
  438.                  }
  439.               }
  440.               else
  441.               {
  442.                  if(!WinDlgBox(HWND_DESKTOP,
  443.                               hwnd,
  444.                               (PFNWP)DisplayDlgProc,
  445.                               (HMODULE)0,
  446.                               (ULONG)IDD_DISPLAYTEXT,
  447.                               (PVOID)&FileInfo) )
  448.                  MessageBox(hwnd, IDMSG_DSPTEXTERROR, "Error !",
  449.                           MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  450.               }
  451.            }
  452.            break;
  453.        case IDM_CALCUL:
  454.            if (WinDlgBox(HWND_DESKTOP,
  455.                          hwnd,
  456.                          (PFNWP)MathDlgProc,
  457.                          (HMODULE)0,
  458.                          (ULONG)IDD_GETMATHINFO,
  459.                          (PVOID)&MathInfo) )
  460.            {
  461.               if(*(MathInfo.pszOperation) != '\000')
  462.               {
  463.                  CalCulProc(&MathInfo);
  464.  
  465.                  if(!WinDlgBox(HWND_DESKTOP,
  466.                               hwnd,
  467.                               (PFNWP)ResultDlgProc,
  468.                               (HMODULE)0,
  469.                               (ULONG)IDD_DISPLAYVALUE,
  470.                               (PVOID)&MathInfo) )
  471.                  MessageBox(hwnd, IDMSG_DSPTEXTERROR, "Error !",
  472.                           MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  473.               }
  474.            }
  475.            break;
  476.  
  477.        default:
  478.          break;
  479.    }
  480.     /* This routine currently doesn't use the mp2 parameter but       *\
  481.      *  it is referenced here to prevent an 'Unreferenced Parameter'  *
  482.     \*  warning at compile time.                                      */
  483.    return;
  484. }
  485.  
  486. /****************************************************************\
  487.  *  Message Box procedure                                       *
  488.  *--------------------------------------------------------------*
  489.  *                                                              *
  490.  *  Name:    MessageBox(hwndOwner,IdMsg,pszMsg,fsStyle,bBeep)   *
  491.  *                                                              *
  492.  *  Purpose: Displays the warning message box with the message  *
  493.  *              given in nIdMsg retrived from the message table *
  494.  *                                                              *
  495.  *  Usage:   Called whever an error occurs and a message wishes *
  496.  *           to be displayed to the user                        *
  497.  *                                                              *
  498.  *  Method:  - Message string is loaded from the process'       *
  499.  *              message table                                   *
  500.  *           - Alarm beep is sounded if desired                 *
  501.  *           - Message box with the message is displayed        *
  502.  *                                                              *
  503.  *  Returns: VOID                                               *
  504.  *                                                              *
  505. \****************************************************************/
  506. VOID MessageBox(HWND hwndOwner, LONG IdMsg, PSZ pszMsg, LONG fsStyle,
  507.                      BOOL bBeep)
  508. {
  509.    CHAR szText[MESSAGELEN];
  510.  
  511.    if(!WinLoadMessage(hab, (HMODULE)NULL, IdMsg, MESSAGELEN, (PSZ)szText))
  512.    {
  513.        WinAlarm(HWND_DESKTOP, WA_ERROR);
  514.        return;
  515.    }
  516.    if(bBeep)
  517.        WinAlarm(HWND_DESKTOP, WA_ERROR);
  518.  
  519.    WinMessageBox(HWND_DESKTOP,
  520.                  hwndOwner,
  521.                  szText,
  522.                  (PSZ)pszMsg,
  523.                  IDM_MSGBOX,
  524.                  fsStyle);
  525.    return;
  526. }                                                  /* MessageBox() */
  527.