home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xwphescr.zip / XWPH0208.ZIP / src / helpers / lan.c < prev    next >
C/C++ Source or Header  |  2002-06-02  |  6KB  |  197 lines

  1.  
  2. /*
  3.  *@@sourcefile lan.c:
  4.  *      LAN helpers.
  5.  *
  6.  *      Usage: All OS/2 programs.
  7.  *
  8.  *      Function prefixes (new with V0.81):
  9.  *      --  nls*        LAN helpers
  10.  *
  11.  *      This file is new with 0.9.16.
  12.  *
  13.  *      Note: Version numbering in this file relates to XWorkplace version
  14.  *            numbering.
  15.  *
  16.  *@@header "helpers\nls.h"
  17.  *@@added V0.9.16 (2001-10-19) [umoeller]
  18.  */
  19.  
  20. /*
  21.  *      Copyright (C) 2001 Ulrich Möller.
  22.  *      This file is part of the "XWorkplace helpers" source package.
  23.  *      This is free software; you can redistribute it and/or modify
  24.  *      it under the terms of the GNU General Public License as published
  25.  *      by the Free Software Foundation, in version 2 as it comes in the
  26.  *      "COPYING" file of the XWorkplace main distribution.
  27.  *      This program is distributed in the hope that it will be useful,
  28.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30.  *      GNU General Public License for more details.
  31.  */
  32.  
  33. #define OS2EMX_PLAIN_CHAR
  34.     // this is needed for "os2emx.h"; if this is defined,
  35.     // emx will define PSZ as _signed_ char, otherwise
  36.     // as unsigned char
  37.  
  38. #define INCL_DOS
  39. #include <os2.h>
  40.  
  41. #define PURE_32
  42. #include <neterr.h>
  43. #include <netcons.h>
  44. #include <server.h>
  45.  
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48. #include <string.h>
  49.  
  50. #include "setup.h"                      // code generation and debugging options
  51.  
  52. #include "helpers\dosh.h"
  53. #include "helpers\lan.h"
  54. #include "helpers\standards.h"
  55.  
  56. #pragma hdrstop
  57.  
  58. /*
  59.  *@@category: Helpers\Network helpers
  60.  *      See lan.c.
  61.  */
  62.  
  63. /* ******************************************************************
  64.  *
  65.  *   Prototypes
  66.  *
  67.  ********************************************************************/
  68.  
  69. typedef API32_FUNCTION NET32WKSTAGETINFO(const unsigned char *pszServer,
  70.                                          unsigned long ulLevel,
  71.                                          unsigned char *pbBuffer,
  72.                                          unsigned long ulBuffer,
  73.                                          unsigned long *pulTotalAvail);
  74.  
  75. typedef API32_FUNCTION NET32SERVERENUM2(const unsigned char *pszServer,
  76.                                         unsigned long ulLevel,
  77.                                         unsigned char *pbBuffer,
  78.                                         unsigned long cbBuffer,
  79.                                         unsigned long *pcEntriesRead,
  80.                                         unsigned long *pcTotalAvail,
  81.                                         unsigned long flServerType,
  82.                                         unsigned char *pszDomain);
  83.  
  84. /* ******************************************************************
  85.  *
  86.  *   Globals
  87.  *
  88.  ********************************************************************/
  89.  
  90. HMODULE         hmodLan = NULLHANDLE;
  91.  
  92. NET32WKSTAGETINFO *pNet32WkstaGetInfo = NULL;
  93. NET32SERVERENUM2 *pNet32ServerEnum2 = NULL;
  94.  
  95. RESOLVEFUNCTION NetResolves[] =
  96.     {
  97.         "Net32WkstaGetInfo", (PFN*)&pNet32WkstaGetInfo,
  98.         "Net32ServerEnum2", (PFN*)&pNet32ServerEnum2
  99.     };
  100.  
  101. static ULONG    G_ulNetsResolved = -1;      // -1 == not init'd
  102.                                             // 0  == success
  103.                                             // APIRET == failure
  104.  
  105. /*
  106.  *@@ lanInit:
  107.  *
  108.  */
  109.  
  110. APIRET lanInit(VOID)
  111. {
  112.     if (G_ulNetsResolved == -1)
  113.     {
  114.         G_ulNetsResolved = doshResolveImports("NETAPI32",   // \MUGLIB\DLL
  115.                                               &hmodLan,
  116.                                               NetResolves,
  117.                                               ARRAYITEMCOUNT(NetResolves));
  118.     }
  119.  
  120.     printf(__FUNCTION__ ": arc %d\n", G_ulNetsResolved);
  121.  
  122.     return (G_ulNetsResolved);
  123. }
  124.  
  125. /*
  126.  *@@ lanQueryServers:
  127.  *      returns an array of SERVER structures describing
  128.  *      the servers which are available from this computer.
  129.  *
  130.  *      With PEER, this should return the peers, assuming
  131.  *      that remote IBMLAN.INI's do not contain "srvhidden = yes".
  132.  *
  133.  *      If NO_ERROR is returned, the caller should free()
  134.  *      the returned array
  135.  */
  136.  
  137. APIRET lanQueryServers(PSERVER *paServers,      // out: array of SERVER structs
  138.                        ULONG *pcServers)        // out: array item count (NOT array size)
  139. {
  140.     APIRET arc;
  141.     if (!(arc = lanInit()))
  142.     {
  143.         ULONG   ulEntriesRead = 0,
  144.                 cTotalAvail = 0;
  145.         PSERVER pBuf;
  146.         ULONG cb = 4096;            // set this fixed, can't get it to work otherwise
  147.         if (pBuf = (PSERVER)doshMalloc(cb,
  148.                                        &arc))
  149.         {
  150.             if (!(arc = pNet32ServerEnum2(NULL,
  151.                                           1,          // ulLevel
  152.                                           (PUCHAR)pBuf,       // pbBuffer
  153.                                           cb,          // cbBuffer,
  154.                                           &ulEntriesRead, // *pcEntriesRead,
  155.                                           &cTotalAvail,   // *pcTotalAvail,
  156.                                           SV_TYPE_ALL,    // all servers
  157.                                           NULL)))      // pszDomain == all domains
  158.             {
  159.                 *pcServers = ulEntriesRead;
  160.                 *paServers = pBuf;
  161.             }
  162.             else
  163.                 free(pBuf);
  164.         }
  165.     }
  166.  
  167.     printf(__FUNCTION__ ": arc %d\n", arc);
  168.  
  169.     return arc;
  170. }
  171.  
  172. #ifdef __LAN_TEST__
  173.  
  174. int main(void)
  175. {
  176.     ULONG ul;
  177.  
  178.     PSERVER paServers;
  179.     ULONG cServers;
  180.     lanQueryServers(&paServers, &cServers);
  181.  
  182.     for (ul = 0;
  183.          ul < cServers;
  184.          ul++)
  185.     {
  186.         printf("Server %d: \\\\%s (%s)\n",
  187.                ul,
  188.                paServers[ul].achServerName,
  189.                paServers[ul].pszComment);
  190.     }
  191.  
  192.     return 0;
  193. }
  194.  
  195. #endif
  196.  
  197.