HOWTO: Get a List of All the NetBIOS Names on a Lana

ID: Q124960

The information in this article applies to:

SUMMARY

You can get a list of NetBIOS names for a lana by using the Adapter Status NetBIOS request and using the "*" character as the call name. However, on Windows NT, this method lists only the names added by the current process.

If you want to list all of the NetBIOS names on the lana, use a unique local name as the call name. This method causes the Adapter Status to be treated as a remote call, which will disable the "filtering" of names added by other processes. The sample code below demonstrates this technique.

SAMPLE CODE

   /* The following makefile may be used to build this sample:

    !include <ntwin32.mak>

    PROJ = test.exe
    DEPS = test.obj
    LIBS_EXT = netapi32.lib

    .c.obj:
      $(cc) /YX $(cdebug) $(cflags) $(cvars) $<

    $(PROJ) : $(DEPS)
      $(link) @<<
    $**
    -out:$@
    $(conlibs)
    $(conlflags)
    $(ldebug)
    $(LIBS_EXT)
    <<

   */ 

   #include <windows.h>
   #include <stdio.h>
   #include <string.h>

   /*
    * LANANUM and LOCALNAME should be set as appropriate for
    * your system
    */ 
   #define LANANUM     0
   #define LOCALNAME   "MAKEUNIQUE"

   #define NBCheck(x)  if (NRC_GOODRET != x.ncb_retcode) { \ 
                           printf("Line %d: Got 0x%x from NetBios()\n", \ 
                                  __LINE__, x.ncb_retcode); \ 
                       }

   void MakeNetbiosName (char *achDest, LPCSTR szSrc);
   BOOL NBAddName (int nLana, LPCSTR szName);
   BOOL NBReset (int nLana, int nSessions, int nNames);
   BOOL NBListNames (int nLana, LPCSTR szName);
   BOOL NBAdapterStatus (int nLana, PVOID pBuffer, int cbBuffer,
                         LPCSTR szName);

   void
   main ()
   {
       if (!NBReset (LANANUM, 20, 30))
           return;

       if (!NBAddName (LANANUM, LOCALNAME))
           return;

       if (!NBListNames (LANANUM, LOCALNAME))
           return;

       printf ("Succeeded.\n");
   }

   BOOL
   NBReset (int nLana, int nSessions, int nNames)
   {
       NCB ncb;

       memset (&ncb, 0, sizeof (ncb));
       ncb.ncb_command = NCBRESET;
       ncb.ncb_lsn = 0;            /* Allocate new lana_num resources */ 
       ncb.ncb_lana_num = nLana;
       ncb.ncb_callname[0] = nSessions;  /* max sessions */ 
       ncb.ncb_callname[2] = nNames;  /* max names */ 

       Netbios (&ncb);
       NBCheck (ncb);

       return (NRC_GOODRET == ncb.ncb_retcode);
   }

   BOOL
   NBAddName (int nLana, LPCSTR szName)
   {
       NCB ncb;

       memset (&ncb, 0, sizeof (ncb));
       ncb.ncb_command = NCBADDNAME;
       ncb.ncb_lana_num = nLana;

       MakeNetbiosName (ncb.ncb_name, szName);

       Netbios (&ncb);
       NBCheck (ncb);

       return (NRC_GOODRET == ncb.ncb_retcode);
   }

   /*
    * MakeNetbiosName - Builds a name padded with spaces up to
    * the length of a NetBIOS name (NCBNAMSZ).
    */ 
   void
   MakeNetbiosName (char *achDest, LPCSTR szSrc)
   {
       int cchSrc;

       cchSrc = lstrlen (szSrc);
       if (cchSrc > NCBNAMSZ)
           cchSrc = NCBNAMSZ;

       memset (achDest, ' ', NCBNAMSZ);
       memcpy (achDest, szSrc, cchSrc);
   }

   BOOL
   NBListNames (int nLana, LPCSTR szName)
   {
       int cbBuffer;
       ADAPTER_STATUS *pStatus;
       NAME_BUFFER *pNames;
       int i;

       // Allocate the largest buffer we might need
       cbBuffer = sizeof (ADAPTER_STATUS) + 255 * sizeof (NAME_BUFFER);
       pStatus = (ADAPTER_STATUS *) HeapAlloc (GetProcessHeap (), 0,
                                               cbBuffer);
       if (NULL == pStatus)
           return FALSE;

       if (!NBAdapterStatus (nLana, (PVOID) pStatus, cbBuffer, szName))
       {
           HeapFree (GetProcessHeap (), 0, pStatus);
           return FALSE;
       }

       // The list of names immediately follows the adapter status
       // structure.
       pNames = (NAME_BUFFER *) (pStatus + 1);

       for (i = 0; i < pStatus->name_count; i++)
           printf ("\t%.*s\n", NCBNAMSZ, pNames[i].name);

       HeapFree (GetProcessHeap (), 0, pStatus);

       return TRUE;
   }

   BOOL
   NBAdapterStatus (int nLana, PVOID pBuffer, int cbBuffer, LPCSTR szName)
   {
       NCB ncb;

       memset (&ncb, 0, sizeof (ncb));
       ncb.ncb_command = NCBASTAT;
       ncb.ncb_lana_num = nLana;

       ncb.ncb_buffer = (PUCHAR) pBuffer;
       ncb.ncb_length = cbBuffer;

       MakeNetbiosName (ncb.ncb_callname, szName);

       Netbios (&ncb);
       NBCheck (ncb);

       return (NRC_GOODRET == ncb.ncb_retcode);
   }

Additional query words: Keywords : kbnetwork kbAPI kbNetBIOS kbNTOS310 kbNTOS350 kbSDKPlatform kbGrpNet

Last Reviewed: September 7, 1998