python ctypes wlapi没有接口

时间:2014-10-29 18:19:08

标签: python winapi ctypes

我正在使用python中的windows api,现在我尝试使用WlanEnumInterfaces函数列出Wlan接口。 这是我的代码:

import ctypes
from ctypes import wintypes


#Api Enums
(wlan_interface_state_not_ready,wlan_interface_state_connected,wlan_interface_state_ad_hoc_network_formed,wlan_interface_state_disconnecting,wlan_interface_state_disconnected,wlan_interface_state_associating,wlan_interface_state_discovering,wlan_interface_state_authenticating) = (0,1,2,3,4,5,6,7)

#ApiFunctions
WlanOpenHandle = ctypes.windll.wlanapi.WlanOpenHandle
WlanEnumInterfaces = ctypes.windll.wlanapi.WlanEnumInterfaces

#ApiStructs
class GUID(wintypes.Structure):
    _fields_ = [('Data1', wintypes.DWORD),('Data2', wintypes.WORD),('Data3', wintypes.WORD),('Data4', wintypes.BYTE * 8)]

class WLAN_INTERFACE_INFO(wintypes.Structure):
    _fields_ = [("InterfaceGuid",GUID),("strInterfaceDescription",wintypes.WCHAR),("isState",ctypes.c_int)]

class WLAN_INTERFACE_INFO_LIST(wintypes.Structure):
    _fields_ = [("dwNumberOfItems",wintypes.DWORD),("dwIndex",wintypes.DWORD),("InterfaceInfo",ctypes.POINTER(WLAN_INTERFACE_INFO))]


def get_wlan_interfaces(client_handle):
    interfaces = WLAN_INTERFACE_INFO_LIST()
    WlanEnumInterfaces(client_handle,None,ctypes.pointer(interfaces))
    return interfaces

def get_client_handle():
    client_version = ctypes.c_ulong(1)
    wlan_api_version = ctypes.c_ulong()
    client_handle = ctypes.c_ulong()
    WlanOpenHandle(client_version,0,wlan_api_version,client_handle)
    return client_handle

handle = get_client_handle()
interfaces = get_wlan_interfaces(handle)
print interfaces.dwNumberOfItems

因此它应该打印接口的计数,但它只打印" 0"。 至少应该有一个界面。 有人能找到问题吗?

现在我将代码更改为以下代码:

import ctypes
from ctypes import wintypes


#Api Enums
(wlan_interface_state_not_ready,wlan_interface_state_connected,wlan_interface_state_ad_hoc_network_formed,wlan_interface_state_disconnecting,wlan_interface_state_disconnected,wlan_interface_state_associating,wlan_interface_state_discovering,wlan_interface_state_authenticating) = (0,1,2,3,4,5,6,7)

#ApiFunctions
WlanOpenHandle = ctypes.windll.wlanapi.WlanOpenHandle
WlanEnumInterfaces = ctypes.windll.wlanapi.WlanEnumInterfaces

#ApiStructs
class GUID(wintypes.Structure):
    _fields_ = [('Data1', wintypes.DWORD),('Data2', wintypes.WORD),('Data3', wintypes.WORD),('Data4', wintypes.BYTE * 8)]

class WLAN_INTERFACE_INFO(wintypes.Structure):
    _fields_ = [("InterfaceGuid",GUID),("strInterfaceDescription",wintypes.WCHAR),("isState",ctypes.c_int)]

class WLAN_INTERFACE_INFO_LIST(wintypes.Structure):
    _fields_ = [("dwNumberOfItems",wintypes.DWORD),("dwIndex",wintypes.DWORD),("InterfaceInfo",ctypes.POINTER((WLAN_INTERFACE_INFO))]


def get_wlan_interfaces(client_handle):
    interfaces = WLAN_INTERFACE_INFO_LIST()
    WlanEnumInterfaces(client_handle,None,ctypes.pointer(interfaces))
    return interfaces

def get_client_handle():
    client_version = wintypes.DWORD(1)
    wlan_api_version = wintypes.DWORD()
    client_handle = wintypes.HANDLE()
    print WlanOpenHandle(client_version,None,ctypes.pointer(wlan_api_version),ctypes.pointer(client_handle))
    return client_handle

handle = get_client_handle()
interfaces = get_wlan_interfaces(handle)
print interfaces.dwNumberOfItems

现在我没有收到错误代码,但我记得总是有不断变化的接口数。

0 个答案:

没有答案