home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / lpr32.zip / lpr32.c next >
C/C++ Source or Header  |  2001-06-18  |  28KB  |  895 lines

  1. #define  LINT_ARGS                     /* argument checking enabled          */
  2.  
  3. #define  INCL_DOS
  4. #define  INCL_GPI
  5. #undef   INCL_GPI
  6. #define  INCL_DEV
  7. #define  INCL_DOSMEMMGR                /* Include standard OS/2 support      */
  8. #define  INCL_DOSMODULEMGR             /* For DosLoadModule                  */
  9. #define  INCL_DOSPROCESS
  10. #define  INCL_GPILCIDS
  11. #define  INCL_WINCOMMON                /* Include Window Management support  */
  12. #define  INCL_WINDOWMGR
  13. #define  INCL_WINSWITCHLIST
  14. #define  INCL_WINPROGRAMLIST
  15. #define  INCL_WINMENUS
  16. #define  INCL_WINWINDOWMGR
  17. #define  INCL_WINMESSAGEMGR
  18. #define  INCL_WINDIALOGS
  19. #define  INCL_WINSTATICS
  20. #define  INCL_WINLISTBOXES
  21. #define  INCL_WINMENUS
  22. #define  INCL_WINSYS
  23. #define  INCL_WINFRAMEMGR
  24. #define  INCL_INCLWINACCELERATORS
  25. #define  INCL_WINPOINTERS
  26. #define  INCL_WINERRORS
  27. #define  INCL_WINSHELLDATA
  28.  
  29. #define  INCL_WINTYPES
  30. #define  INCL_WINACCELERATORS
  31. #define  INCL_WINBUTTONS
  32. #define  INCL_WINENTRYFIELDS
  33. #define  INCL_WINRECTANGLES
  34. #define  INCL_WINTIMER
  35. #define  INCL_WINSCROLLBARS
  36. #define  INCL_WINHEAP
  37. #define  INCL_SHLERRORS
  38. #define  INCL_WININPUT
  39. #define  INCL_WINHELP
  40. #define  INCL_WINSTDSPIN
  41.  
  42. #define  INCL_SPL
  43. #define  INCL_SPLP
  44. #define  INCL_SPLERRORS
  45. #define  INCL_SHLERRORS
  46. #define  INCL_DOSERRORS
  47. #define  INCL_WINHOOKS
  48.  
  49. int acrtused=1;                      /* Define variable to say this is a DLL */
  50.  
  51. #include    <os2.h>
  52.  
  53. #include    <stdlib.h>
  54. #include    <string.h>
  55. #include    <ctype.h>
  56. #include    <stdarg.h>
  57. #include    <stdio.h>
  58. #include    <stdlib.h>
  59. #include    <process.h>
  60. #include    "lpr32.h"
  61.  
  62. //
  63. // If port driver is not defined in INI file yet
  64. //   assume it exists in the boot drive's \OS2\DLL directory
  65. //
  66.  
  67. CHAR szDefaultPortDrvPath[] = { PATH_LPR32_PDR };
  68.  
  69.  
  70. //
  71. // Below definition of PORTNAMES structure should be defined in
  72. // common header file pmspl.h.
  73. //
  74.  
  75. typedef struct _PORTNAMES
  76. {
  77.    PSZ pszPortName;         /* -> name of port(ie "LPT1)                    */
  78.    PSZ pszPortDesc;         /* -> description of port(ie "Parallel Port 1") */
  79. } PORTNAMES, *PPORTNAMES;
  80.  
  81. char * lprtok (char *string,char *control)
  82. {
  83.         unsigned char *str;
  84.         const unsigned char *ctrl = control;
  85.  
  86.         unsigned char map[32];
  87.         int count;
  88.  
  89.         static char *nextoken;
  90.  
  91.         for (count = 0; count < 32; count++)
  92.                 map[count] = 0;
  93.  
  94.         do {
  95.                 map[*ctrl >> 3] |= (1 << (*ctrl & 7));
  96.         } while (*ctrl++);
  97.  
  98.         if (string)
  99.                 str = string;
  100.         else
  101.                 str = nextoken;
  102.  
  103.         while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
  104.                 str++;
  105.  
  106.         string = str;
  107.  
  108.         for ( ; *str ; str++ )
  109.                 if ( map[*str >> 3] & (1 << (*str & 7)) ) {
  110.                         *str++ = '\0';
  111.                         break;
  112.                 }
  113.  
  114.         nextoken = str;
  115.  
  116.         /* Determine if a token has been found. */
  117.         if ( string == str )
  118.                 return NULL;
  119.         else
  120.                 return string;
  121. }
  122. MRESULT EXPENTRY CommDlg( HWND hDlg, USHORT msg, MPARAM mp1, MPARAM mp2 )
  123. {
  124.     PLPRDATA    pLprData;
  125.     ULONG       ulTimeOut = 0 ;
  126.     CHAR        szDesc[ STR_LEN_PORTDESC];
  127.     CHAR        szTitle[ STR_LEN_TITLE + 1];
  128.     CHAR        szTemp[ STR_LEN_PORTDESC ];
  129.     USHORT      i;
  130.     ULONG       rc = 0;
  131.     PUCHAR      token;
  132.  
  133.     switch (msg)
  134.      {
  135.         case WM_INITDLG:
  136.           WinSendDlgItemMsg(hDlg,ID_BINARY,BM_SETCHECK,MPFROM2SHORT(1,0),NULL);
  137.             pLprData = (PLPRDATA)mp2;
  138.             WinSetWindowULong (hDlg, QWL_USER, (ULONG)pLprData);
  139.             if (PrfQueryProfileString (HINI_SYSTEMPROFILE,
  140.                                        pLprData->pszAppName,
  141.                                        KEY_DESCRIPTION,
  142.                                        NULL,
  143.                                        (PSZ)szDesc,
  144.                                        STR_LEN_PORTDESC))
  145.               {
  146.                  WinSetWindowText (WinWindowFromID (hDlg, (USHORT)IDD_LPR32),szDesc);
  147.                  rc = WinLoadString(pLprData->hAB,
  148.                                     pLprData->hModule,
  149.                                     PDR_ID_PROPERTIES,
  150.                                     STR_LEN_PORTDESC, szTemp);
  151.                  if (rc)
  152.                    {
  153.                      strcpy ( szTitle, pLprData->pszPortName );
  154.                      strcat ( szTitle, " - " );
  155.                      strcat ( szTitle, szTemp );
  156.                      WinSetWindowText (hDlg, szTitle);
  157.                    }
  158.               }
  159.             if (PrfQueryProfileString (HINI_SYSTEMPROFILE,
  160.                                         pLprData->pszAppName,
  161.                                         KEY_INITIALIZATION,
  162.                                         NULL,
  163.                                         szTemp,
  164.                                         sizeof(szTemp)))
  165.                {
  166.                    i = 0;
  167.                    token = lprtok(szTemp,"#");
  168.                    while (token != NULL)
  169.                     {
  170.                        switch(i)
  171.                         {
  172.                            case 0:
  173.                                   WinSetDlgItemText(hDlg,ID_IP,token);
  174.                            case 1:
  175.                                   if (token[ strlen(token) - 1 ] == ';')
  176.                                     token[ strlen(token)-1 ] = '\0';
  177.                                   WinSetDlgItemText(hDlg,ID_LPDQUEUE,token);
  178.                                   break;
  179.                      /*    case 2:
  180.                                   if (token[ strlen(token) - 1 ] == ';')
  181.                                     token[ strlen(token)-1 ] = '\0';
  182.                                   WinSetDlgItemText(hDlg,ID_SPOOL_PATH,token); */
  183.                                   break;
  184.                         }
  185.                        i++;
  186.                        token = lprtok(NULL,"#");
  187.                     }
  188.                }
  189.             break;
  190.  
  191.         case WM_COMMAND:
  192.             pLprData = (PLPRDATA)WinQueryWindowULong (hDlg, QWL_USER);
  193.             switch (SHORT1FROMMP(mp1))
  194.              {
  195.                case DID_OK:
  196.                    WinQueryDlgItemText (hDlg, ID_IP, sizeof(szTemp), szTemp );
  197.                    sprintf(pLprData->szSaveLprSetting,"%s",szTemp);
  198.                    WinQueryDlgItemText (hDlg, ID_LPDQUEUE, sizeof(szTemp), szTemp );
  199.                    strcat(pLprData->szSaveLprSetting,"#");
  200.                    strcat(pLprData->szSaveLprSetting,szTemp);
  201. /*                 WinQueryDlgItemText (hDlg, ID_SPOOL_PATH, sizeof(szTemp), szTemp );
  202.                    strcat(pLprData->szSaveLprSetting,"#");
  203.                    strcat(pLprData->szSaveLprSetting,szTemp); */
  204.                    if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
  205.                                                pLprData->pszAppName,
  206.                                                KEY_INITIALIZATION,
  207.                                                pLprData->szSaveLprSetting))
  208.                       WinDismissDlg(hDlg, MBID_CANCEL);
  209.  
  210.                   if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
  211.                                               APPNAME_PM_SPOOLER_PORT,
  212.                                               pLprData->pszPortName,
  213.                                               pLprData->szSaveLprSetting))
  214.                      WinDismissDlg(hDlg, MBID_CANCEL);
  215.                   WinDismissDlg(hDlg, TRUE);
  216.                   break;
  217.                case DID_CANCEL:
  218.                   WinDismissDlg(hDlg, MBID_CANCEL);
  219.                   break;
  220.              }
  221.             break;
  222.         default:
  223.             return WinDefDlgProc(hDlg, msg, mp1, mp2) ;
  224.             break;
  225.      }
  226.  return FALSE;
  227. }
  228. ULONG CalcStructLength ( HAB hab,
  229.                          HMODULE hModule,
  230.                          USHORT usID )
  231. {
  232.    ULONG cbRequired;
  233.    CHAR  chString[STR_LEN_PORTDESC];
  234.  
  235.    cbRequired = 0;
  236.  
  237.    WinLoadString(hab, hModule, usID, STR_LEN_PORTDESC, chString);
  238.    cbRequired += strlen (chString) + 1;
  239.    WinLoadString(hab, hModule, (USHORT)(usID + 1), STR_LEN_PORTDESC, chString);
  240.    cbRequired += strlen (chString) + 1;
  241.    cbRequired += sizeof (PORTNAMES);
  242.    return(cbRequired);
  243. }
  244.  
  245. ULONG CalcBufLength ( HAB hab,
  246.                       HMODULE hModule )
  247. {
  248.    ULONG  cbRequired;
  249.    USHORT usID;
  250.  
  251.    cbRequired = 0;
  252.  
  253.       /*
  254.       ** calculate length required to fit all the port info.
  255.       */
  256.    for (usID = PORT_ID_FIRST; usID <= PORT_ID_LAST; usID += 2)
  257.    {
  258.       cbRequired += CalcStructLength (hab, hModule, usID);
  259.    }
  260.  
  261.    return(cbRequired);
  262. }
  263.  
  264. ULONG NumPortsCanFit ( HAB hab,
  265.                        HMODULE hModule,
  266.                        ULONG cbBuf )
  267. {
  268.    ULONG  cbRequired;
  269.    USHORT usID;
  270.    ULONG  ulNumPort;
  271.  
  272.    cbRequired = 0;
  273.    ulNumPort = 0;
  274.  
  275.       /*
  276.       ** calculate how many ports we can fit in buf.
  277.       */
  278.    for (usID = PORT_ID_FIRST; usID <= PORT_ID_LAST; usID += 2)
  279.    {
  280.       cbRequired += CalcStructLength (hab, hModule, usID);
  281.       if (cbRequired > cbBuf)
  282.       {
  283.          return(ulNumPort);
  284.       }
  285.       ulNumPort++;
  286.    }
  287.  
  288.    return(ulNumPort);
  289. }
  290. VOID CopyStruct ( HAB hab,
  291.                   HMODULE hModule,
  292.                   USHORT usID,
  293.                   PCH pBuf,
  294.                   PULONG pulBeginStruct,
  295.                   PULONG pulBeginText )
  296. {
  297.    PPORTNAMES pPortNames;
  298.  
  299.    pPortNames = (PPORTNAMES)(pBuf + *pulBeginStruct);
  300.    *pulBeginStruct += sizeof (PORTNAMES);
  301.  
  302.       /*
  303.       ** copy port name in the structure
  304.       */
  305.    pPortNames->pszPortName = pBuf + *pulBeginText;
  306.    WinLoadString(hab, hModule, usID, STR_LEN_PORTDESC, pPortNames->pszPortName);
  307.    *pulBeginText += strlen (pPortNames->pszPortName) + 1;
  308.  
  309.       /*
  310.       ** copy port description to the structure
  311.       */
  312.    pPortNames->pszPortDesc = pBuf + *pulBeginText;
  313.    WinLoadString(hab, hModule, usID, STR_LEN_PORTDESC, pPortNames->pszPortDesc);
  314.    *pulBeginText += strlen (pPortNames->pszPortDesc) + 1;
  315. }
  316. VOID  CopyNPorts ( HAB hab,
  317.                    HMODULE hModule,
  318.                    PCH pBuf,
  319.                    ULONG ulReturned )
  320. {
  321.    USHORT usID;
  322.    ULONG  ulBeginText;
  323.    ULONG  ulBeginStruct;
  324.  
  325.    ulBeginText = ulReturned * sizeof (PORTNAMES);
  326.    ulBeginStruct = 0;
  327.  
  328.    for (usID = PORT_ID_FIRST;
  329.         usID <= PORT_ID_LAST && ulReturned;
  330.         usID += 2, --ulReturned)
  331.    {
  332.       CopyStruct (hab, hModule, usID, pBuf, &ulBeginStruct, &ulBeginText);
  333.    }
  334. }
  335.  
  336. ULONG OpenLprPortDlg ( HAB hab,
  337.                        HMODULE hModule,
  338.                        PSZ pszPortName,
  339.                        PSZ pszAppName )
  340. {
  341.    LPRDATA LprData;
  342.  
  343.    memset (&LprData, 0, sizeof (LPRDATA));
  344.    LprData.hAB = hab;
  345.    LprData.hModule = hModule;
  346.    LprData.pszPortName = pszPortName;
  347.    LprData.pszAppName = pszAppName;
  348.  
  349.    WinDlgBox  (HWND_DESKTOP,
  350.                HWND_DESKTOP,
  351.                (PFNWP)CommDlg,
  352.                (HMODULE)hModule,
  353.                IDD_LPR32,
  354.                &LprData);
  355.  
  356.    return LprData.lfModified;
  357. }
  358. BOOL GetPortDescription ( HAB hab,
  359.                           HMODULE hModule,
  360.                           PSZ pszPortName,
  361.                           PSZ pszPortDesc )
  362. {
  363.    USHORT usID;
  364.    CHAR   chBuf[STR_LEN_PORTDESC];
  365.  
  366.    for (usID = PORT_ID_FIRST; usID <= PORT_ID_LAST; usID += 2)
  367.    {
  368.       WinLoadString(hab, hModule, usID, STR_LEN_PORTDESC, chBuf);
  369.       if (!strcmp (pszPortName, chBuf))
  370.       {
  371.          strcpy (pszPortDesc, chBuf);
  372.          return(TRUE);
  373.       }
  374.    }
  375.    return(FALSE);
  376. }
  377. APIRET APIENTRY SplPdEnumPort ( HAB hab,
  378.                                 PVOID pBuf,
  379.                                 ULONG cbBuf,
  380.                                 PULONG pulReturned,
  381.                                 PULONG pulTotal,
  382.                                 PULONG pcbRequired )
  383.  
  384. {
  385.    HMODULE hModule;
  386.    ULONG   ulBootDrive;
  387.    ULONG   rcLoadMod;
  388.    CHAR    szPathName[260];
  389.  
  390.    if (!pulReturned ||
  391.        !pulTotal ||
  392.        !pcbRequired)
  393.    {
  394.       return(ERROR_INVALID_PARAMETER);
  395.    }
  396.  
  397.    if (!pBuf && cbBuf)
  398.    {
  399.       return(ERROR_INVALID_PARAMETER);
  400.    }
  401.  
  402.    DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
  403.                     sizeof (ULONG));
  404.    szDefaultPortDrvPath[0] = (CHAR)(ulBootDrive + 'A' - 1);
  405.  
  406.    PrfQueryProfileString (HINI_SYSTEMPROFILE,
  407.                           "PM_PORT_DRIVER",
  408.                           "LPR32",
  409.                           szDefaultPortDrvPath,
  410.                           szPathName,
  411.                           256 );
  412.  
  413.    rcLoadMod = DosLoadModule (NULL, 0, szPathName, &hModule);
  414.  
  415.    if (cbBuf == 0L)
  416.    {
  417.       *pulReturned = 0;
  418.       *pcbRequired = CalcBufLength (hab, hModule);
  419.       *pulTotal = 64;               /* Currently support LPRx x = 1 .. 9     */
  420.       if (!rcLoadMod)
  421.         DosFreeModule (hModule);
  422.         return(ERROR_MORE_DATA);
  423.    }
  424.  
  425.       /*
  426.       ** check number of ports info we can fit in supplied buffer
  427.       */
  428.    *pulTotal    = 64;               /* Currently support LPRx x= 1 .. 9      */
  429.    *pcbRequired = CalcBufLength (hab, hModule);
  430.    *pulReturned = NumPortsCanFit (hab, hModule, cbBuf);
  431.  
  432.       /*
  433.       ** return error if we can not fit one port.
  434.       */
  435.    if (!(*pulReturned))
  436.    {
  437.       if (!rcLoadMod)
  438.          DosFreeModule (hModule);
  439.       return(ERROR_INSUFFICIENT_BUFFER);
  440.    }
  441.  
  442.       /*
  443.       ** copy all the ports which we can store in the pBuf
  444.       */
  445.    CopyNPorts (hab, hModule, (PCH)pBuf, *pulReturned);
  446.  
  447.       /*
  448.       ** Free the module - we do not need it any more.
  449.       */
  450.    if (!rcLoadMod)
  451.      DosFreeModule (hModule);
  452.  
  453.       /*
  454.       ** copy all the ports which we can store in the pBuf
  455.       */
  456.    if (*pulReturned < *pulTotal)
  457.    {
  458.       return(ERROR_MORE_DATA);
  459.    }
  460.  
  461.    return(NO_ERROR);
  462. }
  463. APIRET APIENTRY SplPdInstallPort ( HAB hab,
  464.                                    PSZ pszPortName )
  465. {
  466.    CHAR    chBuf[STR_LEN_PORTNAME];
  467.    CHAR    chPortDesc[STR_LEN_PORTDESC];
  468.    ULONG   ulBootDrive;
  469.    HMODULE hModule;
  470.    CHAR    szPathName[260];    /* will contain full path to this port driver */
  471.  
  472.    if (!pszPortName)
  473.    {
  474.       return(ERROR_INVALID_PARAMETER);
  475.    }
  476.    strcpy(szDefaultPortDrvPath,PATH_LPR32_PDR);
  477.    DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
  478.                     sizeof (ULONG));
  479.    szDefaultPortDrvPath[0] = (CHAR)(ulBootDrive + 'A' - 1);
  480.  
  481.    PrfWriteProfileString (HINI_SYSTEMPROFILE,
  482.                           "PM_PORT_DRIVER",
  483.                           "LPR32",
  484.                           szDefaultPortDrvPath);
  485.    hModule = 0L ;                             /* Init module handle to null */
  486.    DosLoadModule (NULL, 0, szPathName, &hModule);
  487.    if (!GetPortDescription (hab, hModule, pszPortName, chPortDesc))
  488.    {
  489.       strcpy( chPortDesc, pszPortName );
  490.    }
  491.    DosFreeModule (hModule);
  492.    strcpy (chBuf, APPNAME_LEAD_STR);
  493.    strcat (chBuf, pszPortName);
  494.    if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
  495.                                chBuf,
  496.                                KEY_DESCRIPTION,
  497.                                chPortDesc))
  498.    {
  499.       return (WinGetLastError (hab));
  500.    }
  501.  
  502.    if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
  503.                                chBuf,
  504.                                KEY_INITIALIZATION,
  505.                                DEF_INITIALIZATION))
  506.    {
  507.       return (WinGetLastError (hab));
  508.    }
  509.  
  510.    if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
  511.                                chBuf,
  512.                                KEY_TERMINATION,
  513.                                DEF_TERMINATION))
  514.    {
  515.       return (WinGetLastError (hab));
  516.    }
  517.  
  518.    if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
  519.                                chBuf,
  520.                                KEY_PORTDRIVER,
  521.                                DEF_PORTDRIVER))
  522.    {
  523.       return (WinGetLastError (hab));
  524.    }
  525.  
  526.    if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
  527.                                chBuf,
  528.                                KEY_TIMEOUT,
  529.                                DEF_TIMEOUT))
  530.    {
  531.       return (WinGetLastError (hab));
  532.    }
  533.    if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
  534.                                APPNAME_PM_SPOOLER_PORT,
  535.                                pszPortName,
  536.                                DEF_INITIALIZATION))
  537.    {
  538.       return (WinGetLastError (hab));
  539.    }
  540.    return(NO_ERROR);
  541. }
  542. BOOL   APIENTRY SplPdGetPortIcon ( HAB hab,
  543.                                    PULONG idIcon )
  544. {
  545.    if (idIcon)
  546.    {
  547.       *idIcon = LPR32_ICON;
  548.    }
  549.    return(TRUE);
  550. }
  551. APIRET APIENTRY SplPdQueryPort ( HAB hab,
  552.                                  PSZ pszPortName,
  553.                                  PVOID pBufIn,
  554.                                  ULONG cbBuf,
  555.                                  PULONG cItems )
  556. {
  557.    HMODULE hModule;
  558.    CHAR    chString[STR_LEN_DESC];
  559.    USHORT  usNumLines;
  560.    USHORT  usLineID;
  561.    USHORT  usStrLength;
  562.    ULONG   ulBootDrive;
  563.    PCH     pBuf = pBufIn;
  564.    CHAR    szPathName[260];    /* will contain full path to this port driver */
  565.  
  566.    if (!cItems)
  567.    {
  568.       return(ERROR_INVALID_PARAMETER);
  569.    }
  570.  
  571.    if (!pBuf || !cbBuf)
  572.    {
  573.       return(ERROR_INVALID_PARAMETER);
  574.    }
  575.  
  576.  
  577.    DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
  578.                     sizeof (ULONG));
  579.    szDefaultPortDrvPath[0] = (CHAR)(ulBootDrive + 'A' - 1);
  580.  
  581.    PrfQueryProfileString (HINI_SYSTEMPROFILE,
  582.                           "PM_PORT_DRIVER",
  583.                           "LPR32",
  584.                           szDefaultPortDrvPath,
  585.                           szPathName,
  586.                           256 );
  587.  
  588.    hModule = 0L ;
  589.  
  590.    DosLoadModule (NULL, 0, szPathName, &hModule);
  591.  
  592.    chString[0] = '\0' ;
  593.  
  594.    WinLoadString(hab, hModule, (USHORT)ID_NUMBER_OF_DESC_LINES, STR_LEN_DESC,
  595.                  chString);
  596.    usNumLines = (USHORT)atoi (chString);
  597.    usLineID = ID_FIRST_DESC_LINES;
  598.    for (*cItems = 0; *cItems < usNumLines; *cItems++)
  599.    {
  600.       WinLoadString(hab, hModule, usLineID, STR_LEN_DESC, chString);
  601.       if (cbBuf >= (usStrLength = (USHORT)(strlen (chString) + 1)))
  602.       {
  603.          strcpy (pBuf, chString);
  604.          pBuf += usStrLength;
  605.          cbBuf -= usStrLength;
  606.       }
  607.       else
  608.       {
  609.          DosFreeModule (hModule);
  610.          return(ERROR_INSUFFICIENT_BUFFER);
  611.       }
  612.    }
  613.    DosFreeModule (hModule);
  614.    return(NO_ERROR);
  615. }
  616. APIRET APIENTRY SplPdSetPort ( HAB hab,
  617.                                PSZ pszPortName,
  618.                                PULONG flModified )
  619. {
  620.     CHAR    chBuf[STR_LEN_PORTNAME];
  621.     CHAR    chPortDriver[STR_LEN_PORTNAME];
  622.     ULONG   ulBootDrive;
  623.     HMODULE hModule;
  624.     CHAR    szPathName[260];   /* will contain full path to this port driver */
  625.  
  626.    if (!pszPortName || !flModified)
  627.    {
  628.       return(ERROR_INVALID_PARAMETER);
  629.    }
  630.  
  631.    strcpy (chBuf, APPNAME_LEAD_STR);
  632.    strcat (chBuf, pszPortName);
  633.  
  634.    if (!(PrfQueryProfileString (HINI_SYSTEMPROFILE, chBuf,
  635.                                 KEY_PORTDRIVER, NULL, chPortDriver,
  636.                                 STR_LEN_PORTNAME)))
  637.    {
  638.       return(ERROR_INVALID_PARAMETER);
  639.    }
  640.  
  641.    if (strcmp (chPortDriver, DEF_PORTDRIVER))
  642.    {
  643.       return(ERROR_INVALID_PARAMETER);
  644.    }
  645.  
  646.    DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
  647.                     sizeof (ULONG));
  648.    szDefaultPortDrvPath[0] = (CHAR)(ulBootDrive + 'A' - 1);
  649.  
  650.    PrfQueryProfileString (HINI_SYSTEMPROFILE,
  651.                           "PM_PORT_DRIVER",
  652.                           "LPR32",
  653.                           szDefaultPortDrvPath,
  654.                           szPathName,
  655.                           256 );
  656.  
  657.    hModule = 0L ;                              /* Init module handle to null */
  658.  
  659.    DosLoadModule (NULL, 0, szPathName, &hModule);
  660.  
  661.    *flModified = OpenLprPortDlg (hab, hModule, pszPortName, chBuf);
  662.  
  663.    DosFreeModule (hModule);
  664.    return(NO_ERROR);
  665. }
  666.  
  667.  
  668. APIRET APIENTRY SplPdRemovePort ( HAB hab,
  669.                                   PSZ pszPortName )
  670. {
  671.     CHAR chBuf[STR_LEN_PORTNAME];
  672.     CHAR chPortDriver[STR_LEN_PORTNAME];
  673.  
  674.    if (!pszPortName)
  675.    {
  676.       return(ERROR_INVALID_PARAMETER);
  677.    }
  678.  
  679.    strcpy (chBuf, APPNAME_LEAD_STR);
  680.    strcat (chBuf, pszPortName);
  681.  
  682.    if (!(PrfQueryProfileString (HINI_SYSTEMPROFILE, chBuf,
  683.                                 KEY_PORTDRIVER, NULL, chPortDriver,
  684.                                 STR_LEN_PORTNAME)))
  685.    {
  686.       return(ERROR_INVALID_PARAMETER);
  687.    }
  688.  
  689.    if (strcmp (chPortDriver, DEF_PORTDRIVER))
  690.    {
  691.       return(ERROR_INVALID_PARAMETER);
  692.    }
  693.  
  694.    PrfWriteProfileString (HINI_SYSTEMPROFILE, chBuf, NULL, NULL);
  695.  
  696.    PrfWriteProfileString (HINI_SYSTEMPROFILE,
  697.                           APPNAME_PM_SPOOLER_PORT,
  698.                           pszPortName,
  699.                           NULL);
  700.    return(NO_ERROR);
  701.  
  702. }
  703. ULONG APIENTRY SplPdOpen( PSZ     pszPortName,
  704.                           PHFILE  phFile,
  705.                           PULONG  pDeviceFlags,
  706.                           PVOID   pPrtOpenStruct)
  707. {
  708.    APIRET rc;
  709.    ULONG  ulAction       = 0;      /* Action taken by DosOpen */
  710.    ULONG  ulBytesRead    = 0;      /* Number of bytes read by DosRead */
  711.    ULONG  ulWrote        = 0;      /* Number of bytes written by DosWrite */
  712.    ULONG  ulLocal        = 0;      /* File pointer position after DosSetFilePtr */
  713.    CHAR   szTemp[ 256];
  714.    UCHAR  tmp[256];
  715.    ULONG  pcbWritten ;
  716.    USHORT  i;
  717.    HFILE   control;
  718.    UCHAR  filename[256];
  719.    DATETIME dt;
  720.    UCHAR  spool_dir[256];
  721.    PEAOP2 pEABuf = NULL;
  722.  
  723.   if (!phFile || !pDeviceFlags )
  724.   {
  725.       return(ERROR_INVALID_PARAMETER);
  726.   }
  727.   DosGetDateTime(&dt);
  728.   rc = PrfQueryProfileString (HINI_SYSTEMPROFILE,
  729.                               "PM_SPOOLER",
  730.                               "DIR",
  731.                               NULL,
  732.                               (PSZ)spool_dir,
  733.                               sizeof(spool_dir));
  734.   spool_dir[ strlen(spool_dir) - 1] = '\0';
  735.   sprintf(tmp,"%s\\LPR32",spool_dir);
  736.   DosCreateDir( tmp,pEABuf );
  737.   sprintf(filename,"%s\\LPR32\\%02d_%02d_%02d_%02d_%s",spool_dir,dt.hours,dt.minutes,dt.seconds,dt.hundredths,pszPortName);
  738.   rc = DosOpen(filename,
  739.                phFile,                         /* File handle */
  740.                &ulAction,                      /* Action taken */
  741.                100L,                           /* File primary allocation */
  742.                FILE_ARCHIVED | FILE_NORMAL,    /* File attribute */
  743.                OPEN_ACTION_CREATE_IF_NEW |
  744.                OPEN_ACTION_OPEN_IF_EXISTS,     /* Open function type */
  745.                OPEN_FLAGS_NOINHERIT |
  746.                OPEN_SHARE_DENYNONE  |
  747.                OPEN_ACCESS_READWRITE,          /* Open mode of the file */
  748.                0L);                            /* No extended attribute */
  749.   sprintf(szTemp,"PM_%s",pszPortName);
  750.   if (PrfQueryProfileString (HINI_SYSTEMPROFILE,
  751.                              szTemp,
  752.                              KEY_INITIALIZATION,
  753.                              NULL,
  754.                              szTemp,
  755.                              sizeof(szTemp)))
  756.    {
  757.       sprintf(tmp   ,"%s\\LPR32\\%d.control",spool_dir,*phFile);
  758.       rc = DosOpen( tmp    ,
  759.                    &control,                         /* File handle */
  760.                    &ulAction,                      /* Action taken */
  761.                    strlen(szTemp),                /* File primary allocation */
  762.                    FILE_ARCHIVED | FILE_NORMAL,    /* File attribute */
  763.                    OPEN_ACTION_CREATE_IF_NEW |
  764.                    OPEN_ACTION_OPEN_IF_EXISTS,     /* Open function type */
  765.                    OPEN_FLAGS_NOINHERIT |
  766.                    OPEN_SHARE_DENYNONE  |
  767.                    OPEN_ACCESS_READWRITE,          /* Open mode of the file */
  768.                    0L);                            /* No extended attribute */
  769.      rc = DosWrite( control,szTemp,strlen(szTemp),&pcbWritten);
  770.      rc = DosWrite( control,"#",1,&pcbWritten);
  771.      rc = DosWrite( control,filename,strlen(filename),&pcbWritten);
  772.      rc = DosWrite( control,"@",1,&pcbWritten);
  773.      rc = DosClose(control);
  774.    }
  775.  return rc;
  776.  
  777. }
  778. ULONG  APIENTRY SplPdQuery ( PSZ    pszDeviceName,
  779.                              ULONG  ulFlags,
  780.                              ULONG  ulCommand,
  781.                              PVOID  pInData,
  782.                              ULONG  cbInData,
  783.                              PVOID  pOutData,
  784.                              PULONG pcbOutData )
  785. {
  786. /* return ERROR_NOT_SUPPORTED; */
  787.    return NO_ERROR;
  788. }
  789. ULONG  APIENTRY SplPdSet   ( PSZ    pszDeviceName,
  790.                              ULONG  ulFlags,
  791.                              ULONG  ulCommand,
  792.                              PVOID  pInData,
  793.                              ULONG  cbInData )
  794. {
  795. /* return ERROR_NOT_SUPPORTED; */
  796.    return NO_ERROR;
  797. }
  798. ULONG  APIENTRY SplPdNewPage ( HFILE  hFile, ULONG ulPageNumber )
  799. {
  800.     return NO_ERROR;
  801. }
  802. ULONG APIENTRY SplPdAbortDoc( HFILE   hFile,
  803.                               PVOID   pchData,
  804.                               ULONG   cbData,
  805.                               ULONG   ulFlags )
  806. {
  807.     return NO_ERROR;
  808. }
  809. ULONG  APIENTRY SplPdClose( HFILE  hFile )
  810. {
  811.    APIRET      rc;
  812.    USHORT      i;
  813.    USHORT      j;
  814.    RESULTCODES rc_child;
  815.    ULONG       nbr_lu;
  816.    ULONG       ulAction;
  817.    UCHAR       szTemp[256];
  818.    HFILE       control;
  819.    UCHAR       arg[256];
  820.    UCHAR       filename[256];
  821.    UCHAR       ip_add[256];
  822.    UCHAR       queue_name[256];
  823.    USHORT      pos;
  824.    UCHAR       spool_dir[256];
  825.    ULONG       ulBootDrive;
  826.  
  827.    rc = PrfQueryProfileString (HINI_SYSTEMPROFILE,
  828.                               "PM_SPOOLER",
  829.                               "DIR",
  830.                               NULL,
  831.                               (PSZ)spool_dir,
  832.                               sizeof(spool_dir));
  833.    spool_dir[ strlen(spool_dir) - 1] = '\0';
  834.    sprintf(szTemp,"%s\\LPR32\\%d.control",spool_dir,hFile);
  835.    rc = DosOpen(szTemp,
  836.                 &control,
  837.                 &ulAction,
  838.                 0L,
  839.                 FILE_ARCHIVED | FILE_NORMAL,
  840.                 OPEN_ACTION_CREATE_IF_NEW |
  841.                 OPEN_ACTION_OPEN_IF_EXISTS,
  842.                 OPEN_FLAGS_NOINHERIT |
  843.                 OPEN_SHARE_DENYNONE  |
  844.                 OPEN_ACCESS_READWRITE,
  845.                 0L);
  846.    rc = DosRead( control,szTemp,sizeof(szTemp),&nbr_lu);
  847.    rc = DosClose( control );
  848.    sprintf(filename,"%s\\LPR32\\%d.control",spool_dir,hFile);
  849.    DosDelete(filename);
  850.  
  851.    i = 0;
  852.    j = 0;
  853.    pos = 0;
  854.    while (szTemp[i] != '@')
  855.      {
  856.        if (szTemp[i] == '#')
  857.          {
  858.            szTemp[i] = '\0';
  859.            switch(j)
  860.             {
  861.                case 0:strcpy(ip_add,&szTemp[pos]);
  862.                       break;
  863.                case 1:strcpy(queue_name,&szTemp[pos]);
  864.                       break;
  865.                 }
  866.                pos = i+1;
  867.                j++;
  868.             }
  869.            i++;
  870.         }
  871.    szTemp[i] = '\0';
  872.    strcpy(filename,&szTemp[pos]);
  873.  
  874.    rc = DosClose( hFile );
  875.    DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
  876.                     sizeof (ULONG));
  877.  
  878.    sprintf(arg,"%c:\\tcpip\\bin\\lpr32  -a 0 -b -s %s -p %s %s",ulBootDrive+'A'-1,ip_add,queue_name,filename);
  879.    arg[19] = '\0';
  880.    rc = DosExecPgm(NULL,0,EXEC_SYNC,arg,NULL,&rc_child,"lpr32.exe");
  881.    strcpy(filename,&szTemp[pos]);
  882.    DosDelete(filename);
  883.    return rc;
  884. }
  885. ULONG APIENTRY SplPdWrite( HFILE   hFile,
  886.                            PVOID   pchData,
  887.                            ULONG   cbData,
  888.                            PULONG  pcbWritten )
  889. {  APIRET rc;
  890.  
  891.    rc = DosWrite( hFile,pchData,cbData,pcbWritten);
  892.    rc = DosSleep(0);
  893.    return rc;
  894. }
  895.