|
楼主 |
发表于 2023-3-7 13:36
|
显示全部楼层
ChatGPT给的代码,无法运行:
- Option Explicit
- '要使用VBA获取ncpa.cpl窗口中的全部列表信息,需要使用Windows API函数和VBA代码来模拟用户操作。
- '以下是一个示例VBA代码,它可以打开ncpa.cpl窗口,并获取其中所列出的网络适配器列表。请注意,该代码可能需要进行一些修改才能适应您的系统设置和网络配置。
- Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
- Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWndParent As Long, ByVal hWndChildAfter As Long, ByVal lpClassName As String, ByVal lpWindowName As String) As Long
- Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
- Private Const WM_COMMAND As Long = &H111
- Private Const BN_CLICKED As Long = 0&
- Sub GetNetworkAdapters()
- Dim hWndNCPA As Long
- Dim hWndLstAdapters As Long
- Dim i As Integer
- Dim sText As String
-
- ' Find the main window of ncpa.cpl
- hWndNCPA = FindWindow("Shell_TrayWnd", vbNullString)
- hWndNCPA = FindWindowEx(hWndNCPA, 0&, "TrayNotifyWnd", vbNullString)
- hWndNCPA = FindWindowEx(hWndNCPA, 0&, "SysPager", vbNullString)
- hWndNCPA = FindWindowEx(hWndNCPA, 0&, "SysTabControl32", vbNullString)
- hWndNCPA = FindWindowEx(hWndNCPA, 0&, "SysListView32", "FolderView")
-
- ' Find the list of network adapters
- hWndLstAdapters = FindWindowEx(hWndNCPA, 0&, "SysListView32", vbNullString)
-
- ' Click on the first adapter to select it
- SendMessage hWndLstAdapters, WM_COMMAND, BN_CLICKED, 0&
-
- ' Loop through all adapters and get their names
- sText = ""
- i = 0
- Do While True
- sText = ""
- i = i + 1
- hWndLstAdapters = FindWindowEx(hWndNCPA, hWndLstAdapters, "SysListView32", vbNullString)
- If hWndLstAdapters = 0 Then Exit Do
- SendMessage hWndLstAdapters, WM_COMMAND, BN_CLICKED, i - 1
- GetTextFromControl hWndLstAdapters, sText
- Debug.Print sText
- Loop
-
- End Sub
- Private Sub GetTextFromControl(ByVal hWnd As Long, ByRef sText As String)
- Dim buf As String
- Dim ln As Long
-
- ln = SendMessage(hWnd, WM_GETTEXTLENGTH, 0&, ByVal 0&)
- buf = String(ln + 1, Chr$(0))
- SendMessage hWnd, WM_GETTEXT, ln + 1, ByVal buf
- sText = Left(buf, ln)
-
- End Sub
- '这段代码首先使用FindWindow函数来查找ncpa.cpl窗口的主窗口句柄,然后使用FindWindowEx函数来查找该窗口中的网络适配器列表。接下来,代码使用SendMessage函数模拟点击列表中的每个适配器,以获取列表中所有适配器的名称。此时,代码调用GetTextFromControl子程序来获取列表中选定适配器的名称。
- '请注意,这只是一个示例代码,只能模拟用户在ncpa.cpl窗口中进行的手动操作。实际上,如果您要获取网络适配器的详细信息,您可能需要使用其他Windows API函数和WMI类来获取更多信息。
复制代码 |
|