Python calls DLL to implement some ADB Functions

Source: Internet
Author: User

I recently learned a little Python, and there was just a pre-research job on the mobile synchronization tool to complete.

To realize the communication between PCs and mobile phones, first find their communication protocol. Fortunately, Android has a complete protocol: ADB.

The ADB code is open-source and supports Windows platforms. Ready-made DLL can be called: AdbWinApi. dll and AdbWinUsbApi. dll.

Well, you can do it with VC, but I want to try it with Python, so I started the hard-pressed process of data query + experiment.

 


The experiment process is not much said. Because the above two DLL files are implemented in C and the header files provided are also in C language, the following python Test Program (Python2.7) is available ):


[Python]
Import ctypes
 
# Custom GUID structure. If you are interested, you can use the uuid module.
Class GUID (ctypes. Structure ):
_ Fields _ = [("Data1", ctypes. c_ulong ),
("Data2", ctypes. c_ushort ),
("Data3", ctypes. c_ushort ),
("Data4", ctypes. c_ubyte * 8)]
 
# Define a struct to facilitate the use of the DLL Interface
Class AdbInterfaceInfo (ctypes. Structure ):
_ Fields _ = [("class_id", GUID ),
("Flags", ctypes. c_ulong ),
("Device_name", ctypes. c_wchar * 800)]
 
Def strGUID (GUID ):
String =''
String = string + '% x' % buff. class_id.Data1 +'-% x' % buff. class_id.Data2 + '-% x' % buff. class_id.Data3
String = string + '-% x' % buff. class_id.Data4 [0]
String = string + '% x' % buff. class_id.Data4 [1]
String = string + '% x' % buff. class_id.Data4 [2]
String = string + '% x' % buff. class_id.Data4 [3]
String = string + '% x' % buff. class_id.Data4 [4]
String = string + '% x' % buff. class_id.Data4 [5]
String = string + '% x' % buff. class_id.Data4 [6]
String = string + '% x' % buff. class_id.Data4 [7]
Return string
 
Dll = ctypes. cdll. LoadLibrary ('adbwinapi. dll ')
Usb_class_id = GUID (0xF72FE0D4, 0 xCBCB, 0x407d, (0x88, 0x14, 0x9e, 0xd6, 0x73, 0xd0, 0xdd, 0x6b ))
Enum_handle = dll. AdbEnumInterfaces (usb_class_id, ctypes. c_bool ('true '))
 
While (1 ):
Buff = AdbInterfaceInfo ()
Size = ctypes. c_ulong (ctypes. sizeof (buff ))
Status = dll. AdbNextInterface (enum_handle, ctypes. byref (buff), ctypes. byref (size ))
 
If status = 1:
# Print "GUID =" + strGUID (buff. class_id)
# Print "status =" + str (status)
# Print "Name =" + str (buff. device_name)
HAdbApi = dll. AdbCreateInterfaceByName (buff. device_name );
If hAdbApi = 0:
Print 'adbcreateinterfacebyname Fail'
Else:
Serial = ''x 128
Pserial = ctypes. c_char_p ()
Pserial. value = serial
Serial_len = ctypes. c_ulong (len (serial ))
Ret = dll. AdbGetSerialNumber (hAdbApi, pserial, ctypes. byref (serial_len), ctypes. c_bool ('false '));
If ret = 1:
Print 'device Name: '+' % s' % serial
Else:
Print 'get Device Name Fail'
Else:
Print 'finished'
Break

Import ctypes

# Custom GUID structure. If you are interested, you can use the uuid module.
Class GUID (ctypes. Structure ):
_ Fields _ = [("Data1", ctypes. c_ulong ),
("Data2", ctypes. c_ushort ),
("Data3", ctypes. c_ushort ),
("Data4", ctypes. c_ubyte * 8)]

# Define a struct to facilitate the use of the DLL Interface
Class AdbInterfaceInfo (ctypes. Structure ):
_ Fields _ = [("class_id", GUID ),
("Flags", ctypes. c_ulong ),
("Device_name", ctypes. c_wchar * 800)]

Def strGUID (GUID ):
String =''
String = string + '% x' % buff. class_id.Data1 +'-% x' % buff. class_id.Data2 + '-% x' % buff. class_id.Data3
String = string + '-% x' % buff. class_id.Data4 [0]
String = string + '% x' % buff. class_id.Data4 [1]
String = string + '% x' % buff. class_id.Data4 [2]
String = string + '% x' % buff. class_id.Data4 [3]
String = string + '% x' % buff. class_id.Data4 [4]
String = string + '% x' % buff. class_id.Data4 [5]
String = string + '% x' % buff. class_id.Data4 [6]
String = string + '% x' % buff. class_id.Data4 [7]
Return string

Dll = ctypes. cdll. LoadLibrary ('adbwinapi. dll ')
Usb_class_id = GUID (0xF72FE0D4, 0 xCBCB, 0x407d, (0x88, 0x14, 0x9e, 0xd6, 0x73, 0xd0, 0xdd, 0x6b ))
Enum_handle = dll. AdbEnumInterfaces (usb_class_id, ctypes. c_bool ('true '))

While (1 ):
Buff = AdbInterfaceInfo ()
Size = ctypes. c_ulong (ctypes. sizeof (buff ))
Status = dll. AdbNextInterface (enum_handle, ctypes. byref (buff), ctypes. byref (size ))

If status = 1:
# Print "GUID =" + strGUID (buff. class_id)
# Print "status =" + str (status)
# Print "Name =" + str (buff. device_name)
HAdbApi = dll. AdbCreateInterfaceByName (buff. device_name );
If hAdbApi = 0:
Print 'adbcreateinterfacebyname Fail'
Else:
Serial = ''x 128
Pserial = ctypes. c_char_p ()
Pserial. value = serial
Serial_len = ctypes. c_ulong (len (serial ))
Ret = dll. AdbGetSerialNumber (hAdbApi, pserial, ctypes. byref (serial_len), ctypes. c_bool ('false '));
If ret = 1:
Print 'device Name: '+' % s' % serial
Else:
Print 'get Device Name Fail'
Else:
Print 'finished'
Break


The above simple Python code can be found through the AdbWinApi. dll and AdbWinUsbApi. dll DLL to find the Android device that is being connected to your PC.

Only three DLL interfaces are called, but the purpose has been achieved. The following conclusions can be drawn:

It is feasible to use Python to call the DLL method to implement the ADB tool. Of course, there is no less trouble.

 


At the end of this article, it is quite troublesome for Python to call the DLL written in C, especially for parameter passing, especially for pointer processing. In this regard, the ctypes module has to be used...

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.