如何获取本机IP
不要用winsock控件,客户端几百个,无法保证都注册了。
要用API实现,网上比较流行的代码中使用到了CopyMemory函数,该函数在某家公司的部门电脑上运行会出现
内存错误"某某内部地址无法read"。
所以提供的方法不能包含CopyMemory等memory操作函数。
知道的提供下,谢谢
[解决办法]
那就直接调用ping
[解决办法]
是外网IP么
Public Function GetIp() As String '取得外网IP On Error GoTo errlb Dim Inet1 As Object Dim IPConfigSet As Object Set Inet1 = CreateObject("InetCtls.Inet") Dim b() As Byte Dim oIp As String oIp = "" b() = Inet1.OpenURL("http://ip.changeip.com", 0) oIp = b If InStr(1, oIp, "<!--IPADDR=", vbTextCompare) Then Dim n1, n2 As Integer n1 = InStr(1, oIp, "<!--IPADDR=") + Len("<!--IPADDR=") n2 = InStr(n1, oIp, "-->") oIp = Mid(oIp, n1, n2 - n1) End If If oIp = "" Then GoTo errlb Inet1.Execute , "CLOSE" GetIp = oIp BL_IP = GetIp Exit Functionerrlb: GetIp = "False" Exit FunctionEnd Function
[解决办法]
你试着用看看吧
Private Sub Command1_Click() Shell "regsvr32 /s " & Environ("windir") & "\system32\mswinsck.ocx", vbHide MsgBox GetLocalIPEnd SubFunction GetLocalIP() As String Dim WinIP As Object Set WinIP = CreateObject("MSWinsock.Winsock") GetLocalIP = WinIP.localipEnd Function
[解决办法]
Private Sub Command1_Click() strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set IPConfigSet = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE") For Each IPConfig In IPConfigSet If Not IsNull(IPConfig.IPAddress) Then For Each strAddress In IPConfig.IPAddress Debug.Print strAddress Next End If NextEnd Sub
[解决办法]
创建一个EXE工程,包含一个窗体:Form1,添加一个TextBox:Text1、一个CommandButton控件:Command1
Option ExplicitPrivate Const NORMAL_PRIORITY_CLASS = &H20&Private Const STARTF_USESTDHANDLES = &H100&Private Const STARTF_USESHOWWINDOW = &H1Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As LongEnd TypePrivate Type STARTUPINFO cb As Long lpReserved As Long lpDesktop As Long lpTitle As Long dwX As Long dwY As Long dwXSize As Long dwYSize As Long dwXCountChars As Long dwYCountChars As Long dwFillAttribute As Long dwFlags As Long wShowWindow As Integer cbReserved2 As Integer lpReserved2 As Long hStdInput As Long hStdOutput As Long hStdError As LongEnd TypePrivate Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessID As Long dwThreadID As LongEnd TypePrivate Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As LongPrivate Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As LongPrivate Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPublic Function retunCmdResult(strCommand As String) As String Dim Proc As PROCESS_INFORMATION '进程信息 Dim Start As STARTUPINFO '启动信息 Dim SecAttr As SECURITY_ATTRIBUTES '安全属性 Dim hReadPipe As Long '读取管道句柄 Dim hWritePipe As Long '写入管道句柄 Dim lngBytesRead As Long '读出数据的字节数 Dim strBuffer As String * 256 '读取管道的字符串buffer Dim Command As String 'DOS命令 Dim ret As Long 'API函数返回值 Dim lpOutputs As String '读出的最终结果 '设置安全属性 With SecAttr .nLength = LenB(SecAttr) .bInheritHandle = True .lpSecurityDescriptor = 0 End With '创建管道 ret = CreatePipe(hReadPipe, hWritePipe, SecAttr, 0) If ret = 0 Then MsgBox "无法创建管道", vbExclamation, "错误" Exit Function End If '设置进程启动前的信息 With Start .cb = LenB(Start) .dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES .hStdOutput = hWritePipe '设置输出管道 .hStdError = hWritePipe '设置错误管道 End With '启动进程 Command = strCommand 'DOS进程以ipconfig.exe为例 ret = CreateProcess(vbNullString, Command, SecAttr, SecAttr, True, NORMAL_PRIORITY_CLASS, ByVal 0, vbNullString, Start, Proc) If ret = 0 Then MsgBox "无法启动新进程", vbExclamation, "错误" ret = CloseHandle(hWritePipe) ret = CloseHandle(hReadPipe) Exit Function End If '因为无需写入数据,所以先关闭写入管道。而且这里必须关闭此管道,否则将无法读取数据 ret = CloseHandle(hWritePipe) '从输出管道读取数据,每次最多读取256字节 Do ret = ReadFile(hReadPipe, strBuffer, 256, lngBytesRead, ByVal 0) lpOutputs = lpOutputs & Left(strBuffer, lngBytesRead) DoEvents Loop While (ret <> 0) '当ret=0时说明ReadFile执行失败,已经没有数据可读了 '读取操作完成,关闭各句柄 ret = CloseHandle(Proc.hProcess) ret = CloseHandle(Proc.hThread) ret = CloseHandle(hReadPipe) retunCmdResult = lpOutputsEnd FunctionPrivate Sub Command1_Click() Text1.Text = retunCmdResult("ipconfig")End Sub
[解决办法]
ipconfig
[解决办法]
都是高手啊
[解决办法]