SAMPLE: Using NetBIOS Under Microsoft Windows

Last reviewed: February 15, 1996
Article ID: Q84071
The information in this article applies to:
  • Microsoft Windows Device Driver Kit (DDK) for Windows versions 3.0 and 3.1

SUMMARY

In the Microsoft Windows operating system, an application can use NetBIOS functions in every Windows mode (real, standard, or 386 enhanced). However, the application must conform to some rules, which are briefly discussed in the text below.

For more information, see the WNBDEMO file in the Software/Data Library or the article by Alok Sinha and Ray Patch titled "An Introduction to Network Programming Using the NetBIOS Interface," page 61, in the March-April 1992 issue of the "Microsoft Systems Journal."

WNBDEMO contains sample source code and additional documentation for NetBIOS under Windows.

Download WNBDEMO.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download WNBDEMO.EXE (size: 54498 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the \softlib\mslfiles directory
          Get WNBDEMO.EXE (size: 54498 bytes) 
    

Microsoft C Optimizing Compiler version 6.0 or later and Microsoft Macro Assembler (MASM) version 5.1 or later are required to build the source code in WNBDEMO.

MORE INFORMATION

Windows provides access to NetBIOS through the NetBIOSCall function. This function is equivalent to issuing Interrupt 5Ch under MS-DOS. Although it is legal to call Interrupt 5Ch directly from within Windows, this is not the recommended practice; use NetBIOSCall.

An application should call the NetBIOSCall() function from assembly language to ensure that the registers are preserved for the call. The following is a short stub function that can be used to issue NetBIOS calls under Windows:

extrn     NETBIOSCALL : FAR
extrn     DOS3CALL : FAR

          assume  cs: _TEXT

_TEXT     SEGMENT WORD PUBLIC 'CODE'


; WORD FAR PASCAL nbNetBIOS(LPNCB lpNCB)

          PUBLIC  nbNetBIOS

nbNetBIOS proc FAR

          push    bp                    ; save bp
          mov     bp, sp                ; sp into bp for stack access
          push    es                    ; save es
          push    bx                    ; save bx

          mov     es, word ptr [bp + 8] ; put HIWORD into es
          mov     bx, word ptr [bp + 6] ; put LOWORD into bx

          call    NetBIOSCall           ; shazam!

          xor     ah, ah
          mov     al, byte ptr es:[bx + 1] ; return the return code

          pop     bx                    ; restore bx
          pop     es                    ; restore es
          mov     sp, bp                ; restore sp
          pop     bp                    ; restore bp
          ret     4                     ; return to caller
                                        ; fix-up stack

nbNetBIOS endp

_TEXT     ENDS

END

This function takes a FAR pointer to an initialized network control block (NCB) and returns the NCB's return code from the NetBIOS driver.

Unlike MS-DOS, Windows also has special memory requirements for NCBs and post routines. The general rule of thumb is to fix the NCBs and post routines in memory as much as possible to prevent paging, banking, or other time-consuming operations during the time that the application uses the NCBs and post routines.

For post routines, all code and data segments used by the routine must be placed into FIXED segments of a dynamic-link library (DLL). This causes the segments to be page locked, not banked, and to have other attributes appropriate to the mode in which Windows is running. Because the FIXED keyword is ignored for applications, this code must be placed in a DLL.

Remember that the post routine may call only the PostMessage(), PostAppMessage(), and NetBIOSCall() functions; it cannot call any other functions in Windows. To call the nbNetBIOS function given above from the post routine, nbNetBIOS must be placed into a FIXED segment of a DLL and the function must be reentrant.

NCBs should be allocated such that they are also page locked, not banked, and have other attributes appropriate to the mode in which Windows is running. The easiest way to do this is to use GlobalAlloc() to allocate memory for the NCB either as GMEM_FIXED or to call the GlobalWire() function to move the memory as low as possible in the address space (followed by a call to GlobalPageLock() in 386 enhanced mode). If the memory is allocated from a DLL with the GMEM_FIXED flag, then the memory will also be page locked--this is true only for allocations from DLLs.

As stated above, for further information, refer to the WNBDEMO file in the Software/Data Library.


Additional reference words: 3.00 3.10 softlib DDKNET
KBCategory: kbprg kbfile
KBSubcategory: CnaNNetbios


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 15, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.