⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 电子书“Visual Basic 6 网络编程实例教程.rar”
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "获取主机名称"
   ClientHeight    =   1785
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   3360
   Icon            =   "Form1.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   1785
   ScaleWidth      =   3360
   StartUpPosition =   2  '屏幕中心
   Begin VB.TextBox TxtHostName 
      Height          =   375
      Left            =   600
      TabIndex        =   1
      Top             =   480
      Width           =   2295
   End
   Begin VB.CommandButton CmdGetHostName 
      Caption         =   "运行(&R)"
      Default         =   -1  'True
      Height          =   495
      Left            =   600
      TabIndex        =   0
      Top             =   960
      Width           =   1215
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      BackStyle       =   0  'Transparent
      Caption         =   "本机的主机名称"
      Height          =   180
      Left            =   600
      TabIndex        =   2
      Top             =   240
      Width           =   1260
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub CmdGetHostName_Click()
    TxtHostName = GetHostName()
End Sub

Private Function GetHostName() As String
Dim buff()        As Byte
Dim cbRequired    As Long
Dim nStructSize   As Long
Dim Info          As FIXED_INFO
'以空pFixedInfo参数调用api函数
'这样该API函数会返回存放数据所需的缓存大小,
'该值被保存在参数cbRequired中
Call GetNetworkParams(ByVal 0&, cbRequired)
If cbRequired > 0 Then
    '创建所需大小的缓存
    ReDim buff(0 To cbRequired - 1) As Byte
    '再一次调用API函数
    If GetNetworkParams(buff(0), cbRequired) = ERROR_SUCCESS Then
        '拷贝该缓存中的数据为FIXED_INFO类型
        CopyMemory Info, ByVal VarPtr(buff(0)), LenB(Info)
        '返回主机名称
        GetHostName = TrimNull(StrConv(Info.HostName, vbUnicode))
    End If
End If
End Function

Private Function TrimNull(item As String)
    Dim pos As Integer
    '检查字符串中是否有chr$(0)字符(即空字符)
    pos = InStr(item, Chr$(0))
    If pos Then
        TrimNull = Left$(item, pos - 1)
    Else
        TrimNull = item
    End If
End Function

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -