home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / Chip_2001-06_cd1.bin / zkuste / vbasic / Data / Zdroj / netbios.bas < prev    next >
BASIC Source File  |  1999-06-29  |  19KB  |  445 lines

  1. Attribute VB_Name = "vbNetBios"
  2. Option Explicit
  3.  
  4. ' This section contains the definitions
  5. ' for portable NetBIOS 3.0 support.
  6.  
  7. Public Const NCBNAMSZ = 16  '  absolute length of a net name
  8. Public Const MAX_LANA = 254  '  lana's in range 0 to MAX_LANA
  9.  
  10. ' NCB Command Codes (NetBios 3.0)
  11.  
  12. Public Const NCBCALL = &H10         ' Call
  13. Public Const NCBLISTEN = &H11       ' Listen
  14. Public Const NCBHANGUP = &H12       ' HangUp
  15. Public Const NCBSEND = &H14         ' Send
  16. Public Const NCBRECV = &H15         ' Receive
  17. Public Const NCBRECVANY = &H16      ' Receive Any
  18. Public Const NCBCHAINSEND = &H17    ' Chain Send
  19. Public Const NCBDGSEND = &H20       ' Send Datagram
  20. Public Const NCBDGRECV = &H21       ' Receive Datagram
  21. Public Const NCBDGSENDBC = &H22     ' Send Broadcast Datagram
  22. Public Const NCBDGRECVBC = &H23     ' Receive Broadcase Datagram
  23. Public Const NCBADDNAME = &H30      ' Add Name
  24. Public Const NCBDELNAME = &H31      ' Delete Name
  25. Public Const NCBRESET = &H32        ' Reset
  26. Public Const NCBASTAT = &H33        ' Adapter Status
  27. Public Const NCBSSTAT = &H34        ' Session Status
  28. Public Const NCBCANCEL = &H35       ' Cancel
  29. Public Const NCBADDGRNAME = &H36    ' Add Group Name
  30. Public Const NCBENUM = &H37         ' Enumerate Lana Numbers
  31. Public Const NCBUNLINK = &H70       ' Unlink
  32. Public Const NCBSENDNA = &H71       ' Send No Ack
  33. Public Const NCBCHAINSENDNA = &H72  ' Chain Send No Ack
  34. Public Const NCBLANSTALERT = &H73   ' LAN Status Alert
  35. Public Const NCBACTION = &H77       ' Action
  36. Public Const NCBFINDNAME = &H78     ' Find Name
  37. Public Const NCBTRACE = &H79        ' Trace
  38. Public Const ASYNCH = &H80          ' High bit set = Asynchronous
  39.  
  40. ' NCB Return Codes
  41.  
  42. Public Const NRC_GOODRET = &H0      ' Good return (also returned when
  43.                                     ' ASYNCH request accepted)
  44. Public Const NRC_BUFLEN = &H1       ' Illegal Buffer Length
  45. Public Const NRC_ILLCMD = &H3       ' Illegal Command
  46. Public Const NRC_CMDTMO = &H5       ' Command Timed Out
  47. Public Const NRC_INCOMP = &H6       ' Message Incomplete, Issue Another
  48.                                     ' Command
  49. Public Const NRC_BADDR = &H7        ' Illegal Buffer Address
  50. Public Const NRC_SNUMOUT = &H8      ' Session Number is Out Of Range
  51. Public Const NRC_NORES = &H9        ' No Resource Available
  52. Public Const NRC_SCLOSED = &HA      ' Session Closed
  53. Public Const NRC_CMDCAN = &HB       ' Command Cancelled
  54. Public Const NRC_DUPNAME = &HD      ' Duplicate Name
  55. Public Const NRC_NAMTFUL = &HE      ' Name Table Full
  56. Public Const NRC_ACTSES = &HF       ' No Deletions, Name has Active
  57.                                     ' Sessions
  58. Public Const NRC_LOCTFUL = &H11     ' Local Session Table Full
  59. Public Const NRC_REMTFUL = &H12     ' Remote Session Table Full
  60. Public Const NRC_ILLNN = &H13       ' Illegal Name Number
  61. Public Const NRC_NOCALL = &H14      ' No Call Name
  62. Public Const NRC_NOWILD = &H15      ' Cannot Put in NCB_NAME
  63. Public Const NRC_INUSE = &H16       ' Name in Use on Remote Adapter
  64. Public Const NRC_NAMERR = &H17      ' Name Deleted
  65. Public Const NRC_SABORT = &H18      ' Session Ended Abnormally
  66. Public Const NRC_NAMCONF = &H19     ' Name Conflict Detected
  67. Public Const NRC_IFBUSY = &H21      ' Interface Busy, IRET before
  68.                                     ' Retrying
  69. Public Const NRC_TOOMANY = &H22     ' Too Many Commands outstanding,
  70.                                     ' Retry later
  71. Public Const NRC_BRIDGE = &H23      ' ncb_lana_num field invalid
  72. Public Const NRC_CANOCCR = &H24     ' Command Completed While Cancel
  73.                                     ' Occurring
  74. Public Const NRC_CANCEL = &H26      ' Command Not Valid to Cancel
  75. Public Const NRC_DUPENV = &H30      ' Name Defined By Another Local
  76.                                     ' Process
  77. Public Const NRC_ENVNOTDEF = &H34   ' Envirnment Undefined. RESET
  78.                                     ' Required
  79. Public Const NRC_OSRESNOTAV = &H35  ' Required OS Resources Exhausted
  80. Public Const NRC_MAXAPPS = &H36     ' Max Number of Applications
  81.                                     ' Exceeded
  82. Public Const NRC_NOSAPS = &H37      ' No Saps Available for NetBIOS
  83. Public Const NRC_NORESOURCES = &H38 ' Requested Resources are Not
  84.                                     ' Available
  85. Public Const NRC_INVADDRESS = &H39  ' Invalid NCB Address, or
  86.                                     ' Length > Segment
  87. Public Const NRC_INVDDID = &H3B     ' Invalid NCB DDID
  88. Public Const NRC_LOCKFAIL = &H3C    ' Lock of User Area Failed
  89. Public Const NRC_OPENERR = &H3F     ' NetBIOS Not Loaded
  90. Public Const NRC_SYSTEM = &H40      ' System Error
  91. Public Const NRC_PENDING = &HFF     ' Asynchronous Command is Not Yet
  92.                                     ' Fulfilled
  93.  
  94. ' State Values
  95.  
  96. Public Const LISTEN_OUTSTANDING = &H1
  97. Public Const CALL_PENDING = &H2
  98. Public Const SESSION_ESTABLISHED = &H3
  99. Public Const HANGUP_PENDING = &H4
  100. Public Const HANGUP_COMPLETE = &H5
  101. Public Const SESSION_ABORTED = &H6
  102.  
  103. ' NCB User Defined Types (structures)
  104.  
  105. Type SESSION_HEADER
  106.     sess_name As Byte
  107.     num_sess As Byte
  108.     rcv_dg_outstanding As Byte
  109.     rcv_any_outstanding As Byte
  110. End Type
  111.  
  112. ' The structure returned to the NCB Command NCBSTAT is SESSION_HEADER
  113. ' followed by an array of SESSION_BUFFER structures.  If the NCB_NAME
  114. ' starts with an asterisk, then an array of these structures is returned
  115. ' containing the status for all names.
  116.  
  117. Public SESSION_HEADER As SESSION_HEADER
  118. Public PSESSION_HEADER As SESSION_HEADER
  119.  
  120. Type SESSION_BUFFER
  121.     lsn As Byte
  122.     state As Byte
  123.     local_name As String * NCBNAMSZ
  124.     remote_name As String * NCBNAMSZ
  125.     rcvs_outstanding As Byte
  126.     sends_outstanding As Byte
  127. End Type
  128.  
  129. ' Structure returned to the NCB command NCBFINDNAME is
  130. ' FIND_NAME_HEADER, followed by an array of FIND_NAME_BUFFER
  131. ' structures.
  132.  
  133. Public SESSION_BUFFER As SESSION_BUFFER
  134. Public PSESSION_BUFFER As SESSION_BUFFER
  135.  
  136. Type FIND_NAME_HEADER
  137.     node_count As Long
  138.     reserved As Byte
  139.     unique_group As Byte
  140. End Type
  141.  
  142. Public FIND_NAME_HEADER As FIND_NAME_HEADER
  143. Public PFIND_NAME_HEADER As FIND_NAME_HEADER
  144.  
  145. Type FIND_NAME_BUFFER
  146.     length As Byte
  147.     access_control As Byte
  148.     frame_control As Byte
  149.     destination_addr(0 To 5) As Byte
  150.     source_addr(0 To 5) As Byte
  151.     routing_info(0 To 17) As Byte
  152. End Type
  153.  
  154. Public FIND_NAME_BUFFER As FIND_NAME_BUFFER
  155. Public PFIND_NAME_BUFFER As FIND_NAME_BUFFER
  156.  
  157. Type ACTION_HEADER
  158.     transport_id As Long    ' ULONG
  159.     action_code As Integer  ' USHORT
  160.     reserved As Integer     ' USHORT
  161. End Type
  162.  
  163. ' Structures provided with NCBACTION.  The purpose of NCBACTION is
  164. ' to provide transport specific extensions to NetBIOS.
  165.  
  166. Public ACTION_HEADER As ACTION_HEADER
  167. Public PACTION_HEADER As ACTION_HEADER
  168.  
  169. ' Values for trasport_id in Type ACTION_HEADER
  170.  
  171. Public Const ALL_TRANSPORTS = "M\0\0\0"
  172. Public Const MS_NBF = "MNBF"
  173.  
  174. Type NCB
  175.     ncb_command As Byte                         '  1-byte   UCHAR
  176.     ncb_retcode As Byte                         '  1-byte   UCHAR
  177.     ncb_lsn As Byte                             '  1-byte   UCHAR
  178.     ncb_num As Byte                             '  1-byte   UCHAR
  179.     ncb_buffer As Long                          '  4-bytes  PUCHAR
  180.     ncb_length As Integer                       '  2-bytes  WORD
  181.     ncb_callname As String * NCBNAMSZ           ' 16-bytes
  182.     ncb_name As String * NCBNAMSZ               ' 16-bytes
  183.     ncb_rto As Byte                             '  1-byte
  184.     ncb_sto As Byte                             '  1-byte
  185.     ncb_post As Long                            '  4-bytes
  186.     ncb_lana_num As Byte                        '  1-byte
  187.     ncb_cmd_cplt As Byte                        '  1-byte
  188.     ncb_reserve(0 To 9) As Byte                 ' 10-bytes  Reserved for BIOS, must be 0
  189.     ncb_event As Long                           '  4-bytes
  190. End Type                                        ' 64-bytes
  191.  
  192. Public NCB As NCB
  193. Public PNCB As NCB
  194.  
  195. Type ADAPTER_STATUS
  196.     adapter_address(0 To 5) As Byte             '  6-bytes UCHAR Array
  197.     rev_major As Byte                           '  1
  198.     reserved0 As Byte                           '  1
  199.     adapter_type As Byte                        '  1
  200.     rev_minor As Byte                           '  1
  201.     duration As Integer                         '  2
  202.     frmr_recv As Integer                        '  2
  203.     frmr_xmit As Integer                        '  2
  204.     iframe_recv_err As Integer                  '  2
  205.     xmit_aborts As Integer                      '  2
  206.     xmit_success As Long                        '  4
  207.     recv_success As Long                        '  4
  208.     iframe_xmit_err As Integer                  '  2
  209.     recv_buff_unavail As Integer                '  2
  210.     t1_timeouts As Integer                      '  2
  211.     ti_timeouts As Integer                      '  2
  212.     Reserved1 As Long                           '  4
  213.     free_ncbs As Integer                        '  2
  214.     max_cfg_ncbs As Integer                     '  2
  215.     max_ncbs As Integer                         '  2
  216.     xmit_buf_unavail As Integer                 '  2
  217.     max_dgram_size As Integer                   '  2
  218.     pending_sess As Integer                     '  2
  219.     max_cfg_sess As Integer                     '  2
  220.     max_sess As Integer                         '  2
  221.     max_sess_pkt_size As Integer                '  2
  222.     name_count As Integer                       '  2
  223. End Type                                        ' 60-bytes total
  224.  
  225. ' Structure returned to the NCB command NCBASTAT is ADAPTER_STATUS,
  226. ' followed by an array of NAME_BUFFER structures.
  227.  
  228. Public ADAPTER_STATUS As ADAPTER_STATUS
  229. Public PADAPTER_STATUS As ADAPTER_STATUS
  230.  
  231. Type NAME_BUFFER                        ' NCBNAMSZ = 16
  232.     name_(0 To (NCBNAMSZ - 1)) As Byte  ' 16-bytes
  233.     name_num As Byte                    '  1-byte
  234.     name_flags As Byte                  '  1-byte
  235. End Type                                ' 18-bytes total
  236.  
  237. ' Values for name_flags bits
  238.  
  239. Public Const NAME_FLAGS_MASK = &H87
  240. Public Const GROUP_NAME = &H80
  241. Public Const UNIQUE_NAME = &H0
  242. Public Const REGISTERING = &H0
  243. Public Const DEREGISTERED = &H4
  244. Public Const DUPLICATE = &H5
  245. Public Const DUPLICATE_DEREG = &H7
  246.  
  247. Public NAME_BUFFER As NAME_BUFFER
  248. Public PNAME_BUFFER As NAME_BUFFER
  249.  
  250. Type LANA_ENUM
  251.     length As Byte                          '   1-byte (Number of valid entries in lana[]
  252.     lana(0 To (MAX_LANA - 1)) As Byte       ' 256-bytes
  253. End Type                                    ' 257-bytes total
  254.  
  255. ' Command NCBENUM returns:
  256.  
  257. ' On a system containing lana's 0,2 and 3, a structure with
  258. ' length=3, lan[0] = 0, lana[1] = 1 and lana[2] = 3 will be
  259. ' returned.
  260.  
  261. Public LANA_ENUM As LANA_ENUM
  262. Public PLANA_ENUM As LANA_ENUM
  263.  
  264. Type NET_STATUS
  265.     ADAPTER_STATUS As ADAPTER_STATUS                    ' 60-bytes for ADAPTER_STATUS
  266.     NAME_BUFFER(0 To (NCBNAMSZ + 1)) As NAME_BUFFER     ' 18-bytes for NAME_BUFFER UDT
  267. End Type                                                    ' 78-bytes total
  268.  
  269. Public NET_STATUS As NET_STATUS
  270.  
  271. Declare Function NetBios Lib "NetAPI32.dll" Alias "Netbios" (ByRef PNCB As NCB) As Long
  272.  
  273. ' The following lines of code were modified by Jim Huff on May 18, 1998 to include
  274. ' the location of the VarPtr function call for those who are using vb5.0
  275.  
  276. ' The following VarPtr function call is for vb5.
  277. ' Declare Function VarPtr Lib "MSVBVM50.DLL" (pVoid As Any) As Long
  278.  
  279. ' The following VarPtr function call is for vb4.
  280. Declare Function VarPtr Lib "VB40032.dll" (pVoid As Any) As Long
  281. Function vbGetMacAddress(Adapter As Integer, HostName As String) As String
  282.  
  283.     If Adapter >= MAX_LANA Then
  284.         MsgBox "ERROR:  Illegal lana has been designated.  Must be less than 254.", vbOKOnly, "vbNetBIOS.BAS Demo"
  285.         vbGetMacAddress = ""
  286.         Exit Function
  287.     End If
  288.  
  289.     ' This function returns the MAC Address to the calling procedure when provided with
  290.     ' the LanaEnum (Adapter) number.
  291.  
  292.     Dim lResult As Long
  293.     Dim MACAddr As String
  294.     Dim HexStringValue As String
  295.     Dim i As Integer
  296.  
  297.     MACAddr = ""
  298.  
  299.     NCB.ncb_command = NCBENUM                   ' Enumerate lana numbers
  300.     NCB.ncb_buffer = VarPtr(LANA_ENUM)
  301.     NCB.ncb_length = LenB(LANA_ENUM)
  302.  
  303.     lResult = NetBios(NCB)
  304.     DisplayError lResult
  305.  
  306.     Debug.Print "Enumerate lana Numbers Result: " & lResult
  307.  
  308.     NCB.ncb_command = NCBRESET                  ' Reset
  309.     NCB.ncb_lana_num = LANA_ENUM.lana(Adapter)
  310.  
  311.     lResult = NetBios(NCB)
  312.     DisplayError lResult
  313.  
  314.     NCB.ncb_command = NCBASTAT                  ' Adapter Status
  315.     NCB.ncb_lana_num = LANA_ENUM.lana(Adapter)
  316.  
  317.     ' Use the following lines to identify a remote host
  318.  
  319.     NCB.ncb_callname = UCase(HostName) + Space(16 - Len(HostName))
  320.  
  321.     NCB.ncb_buffer = VarPtr(NET_STATUS)
  322.     NCB.ncb_length = LenB(NET_STATUS)
  323.  
  324.     lResult = NetBios(NCB)
  325.     DisplayError lResult
  326.  
  327.     Debug.Print "Adapter Status Result: " & lResult
  328.  
  329.     For i = 0 To 5
  330.  
  331.         HexStringValue = Hex(NET_STATUS.ADAPTER_STATUS.adapter_address(i))
  332.  
  333.         If NET_STATUS.ADAPTER_STATUS.adapter_address(i) < 16 Then HexStringValue = "0" & HexStringValue
  334.  
  335.         MACAddr = MACAddr + HexStringValue
  336.  
  337.         If i <> 5 Then MACAddr = MACAddr + "-"
  338.  
  339.     Next i
  340.  
  341.     vbGetMacAddress = MACAddr
  342.  
  343. End Function
  344. Sub DisplayError(ResultCode As Long)
  345.  
  346.     Dim DispMsg As String
  347.  
  348.     Select Case ResultCode
  349.  
  350.         Case NRC_GOODRET    ' Good return (also returned when
  351.             Exit Sub        ' ASYNCH request accepted)
  352.         Case NRC_BUFLEN     ' Illegal Buffer Length
  353.             DispMsg = "Illegal Buffer Length"
  354.         Case NRC_ILLCMD         ' Illegal Command
  355.             DispMsg = "Illegal Command"
  356.         Case NRC_CMDTMO         ' Command Timed Out
  357.             DispMsg = "Command Timed Out"
  358.         Case NRC_INCOMP         ' Message Incomplete, Issue Another
  359.                                 ' Command
  360.             DispMsg = "Message Incomplete.  Issue Another Command"
  361.         Case NRC_BADDR          ' Illegal Buffer Address
  362.             DispMsg = "Illegal Buffer Address"
  363.         Case NRC_SNUMOUT        ' Session Number is Out Of Range
  364.             DispMsg = "Session Number is Out of Range"
  365.         Case NRC_NORES          ' No Resource Available
  366.             DispMsg = "No Resources Available"
  367.         Case NRC_SCLOSED        ' Session Closed
  368.             DispMsg = "Session Closed"
  369.         Case NRC_CMDCAN         ' Command Cancelled
  370.             DispMsg = "Command Cancelled"
  371.         Case NRC_DUPNAME        ' Duplicate Name
  372.             DispMsg = "Duplicate Name"
  373.         Case NRC_NAMTFUL        ' Name Table Full
  374.             DispMsg = "Name Table Full"
  375.         Case NRC_ACTSES         ' No Deletions, Name has Active
  376.                                 ' Sessions
  377.             DispMsg = "No Deletions.  Name has Active Sessions"
  378.         Case NRC_LOCTFUL        ' Local Session Table Full
  379.             DispMsg = "Local Session Table Full"
  380.         Case NRC_REMTFUL        ' Remote Session Table Full
  381.             DispMsg = "Remote Session Table Full"
  382.         Case NRC_ILLNN          ' Illegal Name Number
  383.             DispMsg = "Illegal Name Number"
  384.         Case NRC_NOCALL         ' No Call Name
  385.             DispMsg = "No Call Name"
  386.         Case NRC_NOWILD         ' Cannot Put in NCB_NAME
  387.             DispMsg = "Cannot Put in NCB_NAME"
  388.         Case NRC_INUSE          ' Name in Use on Remote Adapter
  389.             DispMsg = "Name in Use on Remote Computer"
  390.         Case NRC_NAMERR         ' Name Deleted
  391.             DispMsg = "Name Deleted"
  392.         Case NRC_SABORT         ' Session Ended Abnormally
  393.             DispMsg = "Session Ended Abnormally"
  394.         Case NRC_NAMCONF        ' Name Conflict Detected
  395.             DispMsg = "Name Conflict Detected"
  396.         Case NRC_IFBUSY         ' Interface Busy, IRET before
  397.                                 ' Retrying
  398.             DispMsg = "Interface Busy.  IRET Before Retrying"
  399.         Case NRC_TOOMANY        ' Too Many Commands outstanding,
  400.                                 ' Retry later
  401.             DispMsg = "Too Many Commands Outstanding.  Retry Later"
  402.         Case NRC_BRIDGE         ' ncb_lana_num field invalid
  403.             DispMsg = "ncb_lana_num Field Invalid"
  404.         Case NRC_CANOCCR        ' Command Completed While Cancel
  405.                                 ' Occurring
  406.             DispMsg = "Command Completed While Cancel Occurring"
  407.         Case NRC_CANCEL         ' Command Not Valid to Cancel
  408.             DispMsg = "Command Not Valid to Cancel"
  409.         Case NRC_DUPENV         ' Name Defined By Another Local
  410.                                 ' Process
  411.             DispMsg = "Name Defined By Another Process"
  412.         Case NRC_ENVNOTDEF      ' Environment Undefined. RESET
  413.                                 ' Required
  414.             DispMsg = "Environment Undefined.  RESET Required"
  415.         Case NRC_OSRESNOTAV     ' Required OS Resources Exhausted
  416.             DispMsg = "Required OS Resources Exhausted"
  417.         Case NRC_MAXAPPS        ' Max Number of Applications
  418.                                 ' Exceeded
  419.             DispMsg = "Max Number Of Applications Exceeded"
  420.         Case NRC_NOSAPS         ' No Saps Available for NetBIOS
  421.             DispMsg = "No saps Available for NetBIOS"
  422.         Case NRC_NORESOURCES    ' Requested Resources are Not
  423.                                 ' Available
  424.             DispMsg = "Requested Resources are Not Available"
  425.         Case NRC_INVADDRESS     ' Invalid NCB Address, or
  426.                                 ' Length > Segment
  427.             DispMsg = "Invalid NCB Address, or Length > Segment"
  428.         Case NRC_INVDDID        ' Invalid NCB DDID
  429.             DispMsg = "Invalid NCB DDID"
  430.         Case NRC_LOCKFAIL       ' Lock of User Area Failed
  431.             DispMsg = "Lock of User Area Failed"
  432.         Case NRC_OPENERR        ' NetBIOS Not Loaded
  433.             DispMsg = "NetBIOS Not Loaded"
  434.         Case NRC_SYSTEM         ' System Error
  435.             DispMsg = "System Error"
  436.         Case NRC_PENDING        ' Asynchronous Command is Not Yet
  437.                                 ' Completed
  438.             DispMsg = "Asynchronous Command Is Not Yet Completed"
  439.  
  440.     End Select
  441.  
  442.     MsgBox "ERROR: " + CStr(ResultCode) + Chr$(13) + Chr$(10) + DispMsg, vbOKOnly, "vbNetBIOS.BAS Demo"
  443.  
  444. End Sub
  445.