Kiều Mạnh
I don't program, I beat code into submission!!!
- Tham gia
- 9/6/12
- Bài viết
- 5,538
- Được thích
- 4,129
- Giới tính
- Nam
Thử với hàm này xem:Máy Em cũng vậy chạy thành công Run As ....
Em muốn hỏi có cách nào ta viết code kiểm tra được nếu là "Wi-Fi" thì Set nwName="Wi-Fi" khác thì là Set nwName="Local Area Connection"
Không Anh .............ý Em muốn hỏi viết thành hàm Bao quát nhất xài cho 2 trường hợp đó
Cảm ơn Anh
Function GetWirlessName()
Dim strKeyPath
Dim strComputer
Dim objReg
Dim arrSubKeys
Dim SubKey
Dim strValueName
Dim dwValue
Dim strValue
Const HKLM = &H80000002
strKeyPath = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
strComputer = "."
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objReg.Enumkey HKLM, strKeyPath, arrSubKeys
For Each SubKey In arrSubKeys
strValueName = "MediaSubType"
objReg.GetDWORDValue HKLM, strKeyPath & "\" & SubKey & "\" & "Connection", strValueName, dwValue
If dwValue = 2 Then
strValueName = "Name"
objReg.GetStringValue HKLM, strKeyPath & "\" & SubKey & "\" & "Connection", strValueName, strValue
Exit For
End If
Next
GetWirlessName = strValue
End Function
Máy Em nó báo EmptyThử với hàm này xem:
Mọi thông số được lấy từ Registry.Mã:Function GetWirlessName() Dim strKeyPath Dim strComputer Dim objReg Dim arrSubKeys Dim SubKey Dim strValueName Dim dwValue Dim strValue Const HKLM = &H80000002 strKeyPath = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}" strComputer = "." Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") objReg.Enumkey HKLM, strKeyPath, arrSubKeys For Each SubKey In arrSubKeys strValueName = "MediaSubType" objReg.GetDWORDValue HKLM, strKeyPath & "\" & SubKey & "\" & "Connection", strValueName, dwValue If dwValue = 2 Then strValueName = "Name" objReg.GetStringValue HKLM, strKeyPath & "\" & SubKey & "\" & "Connection", strValueName, strValue Exit For End If Next GetWirlessName = strValue End Function
Tôi không có điều kiện test trên nhiều máy (kết nối không dây và có dây) nên không chắc lắm
Trong cái đống đó chắc bạn cần lệnh này: netsh int show interfaceChạy lỗi .......... Link sau có các Lệnh liên quan hay Tuy nhiên coi khúc biết khúc ko
https://ss64.com/nt/netsh.html
Vậy thì bạn kiểm tra lại đường dẫn trong registry trên máy bạn, chỗ nào có "Local Area Connection" thì sửa lại cho phù hợpMáy Em nó báo Empty
Chỗ màu vàng theo link anh gửi (https://ss64.com/nt/netsh.html) có đó:Nó thiếu theo Hình màu vàng
netsh interface ipv4 add dns "Wi-fi" 8.8.8.8 'Connection Name = "Wi-Fi"
netsh interface ipv4 add dns "Wi-fi" 8.8.4.4 index=2
Sub Test()
Const NW_CONNECTION = &H31&
Dim objShell As Object, item, objConn As Object
Set objShell = CreateObject("Shell.Application")
Set objConn = objShell.Namespace(NW_CONNECTION)
For Each item In objConn.Items
MsgBox item.Name
Next
End Sub
Em Ctrl + F Local Area Connection thì nó cho ra như hìnhVậy thì bạn kiểm tra lại đường dẫn trong registry trên máy bạn, chỗ nào có "Local Area Connection" thì sửa lại cho phù hợp
máy Em Ok ... máy Desktop xài cáp Quang ... code chạy rất nhanhMạnh thử test code này xem thế nào
Không chắc nên phải test trên nhiều máy, nhiều kiểu kết nối để rút ra kết luậnMã:Sub Test() Const NW_CONNECTION = &H31& Dim objShell As Object, item, objConn As Object Set objShell = CreateObject("Shell.Application") Set objConn = objShell.Namespace(NW_CONNECTION) For Each item In objConn.Items MsgBox item.Name Next End Sub
Private Sub Test()
Dim oWMI, Instances, Instance
Set oWMI = GetObject("WINMGMTS:\\.\ROOT\cimv2")
Set Instances = oWMI.InstancesOf("Win32_NetworkAdapter")
For Each Instance In Instances
If Instance.NetconnectionID <> "null" Then
MsgBox (Instance.NetconnectionID & "---" _
& Instance.AdapterType & "-----" & Instance.NetConnectionStatus)
End If
Next
End Sub
Mạnh có Tìm hiểu nhiều về IP thấy trên GPE có code sau chạy tốt .......... Nhưng Mình thấy cách Viết hơi dài rồi Mình có thử nghiên Cứu Winsock Mình viết thành code sau rất Gọn vậy Nhờ Bạn test dùm trên Windows_x64 xem 2 code sau nó như thế nào vớiAnh @kieumanh thử dùng PowerShell xem sao...
Private Sub GetIPAddress()
Dim Item
Dim rIP As String
On Error Resume Next
With GetObject("winmgmts:\\.\root\cimv2")
For Each Item In .ExecQuery("Select * from Win32_NetworkAdapterConfiguration", , 48)
Range("A65536").End(xlUp).Offset(1) = Item.IPAddress(0)
'MsgBox (Item.IPAddress(0))
Next
End With
End Sub
Public Function GetIPAddress()
GetIPAddress = CreateObject("MSWinsock.Winsock").LocalIp
'MsgBox GetIPAddress
Range("A65536").End(xlUp).Offset(1) = GetIPAddress
End Function
Thử tìm trong /Windows nhưng không thấy file MSWINSCK.OCXvậy là x64 .......... Winsock nó tịt rồi![]()
sao hay vậy ...Mạnh xem tối qua rồi ...........Lỗi ...........ko biết tại sao đang để đó chưa coi lạiThử tìm trong /Windows nhưng không thấy file MSWINSCK.OCX
Anh Mạnh thử cái này xem.
http://www.keysight.com/main/editor...0001131:epsg:sud&nid=-11143.0.00&lc=eng&cc=VN