Python 携帯電話開発は DLL を呼び出していくつかの ADB 機能を実装します

高洛峰
リリース: 2016-10-18 10:34:11
オリジナル
2351 人が閲覧しました

最近 Python を少し学習しましたが、たまたま携帯電話の同期ツールに関する事前調査を完了する必要がありました。

PC と携帯電話間の通信を実現するには、まずその通信プロトコルを見つける必要があります。幸いなことに、Android には完全なプロトコルがあります。ADB

のコードはオープンソースであり、Windows プラットフォームをサポートする既製の DLL があります。名前: AdbWinApi.dll、AdbWinUsbApi.dll

VC でもできますが、Python でも試してみたかったので、情報の確認 + 実験という大変なプロセスを開始しました。


上記の 2 つの DLL は C で実装されており、提供されるヘッダー ファイルも C 言語であるため、次の Python テスト プログラム (Python2.7) が用意されています。 :

import ctypes
   
#自定义的GUID结构,有兴趣的可以自己研究用uuid模块 
class GUID(ctypes.Structure):
    _fields_ = [("Data1", ctypes.c_ulong),
                ("Data2", ctypes.c_ushort),
                ("Data3", ctypes.c_ushort),
                ("Data4", ctypes.c_ubyte*8)]
   
#自己定义的一个结构体,便于使用DLL接口 
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, 0xCBCB, 0x407d, (0x88, 0x14, 0x9e, 0xd6, 0x73, 0xd0, 0xdd, 0x6b))
enum_handle = dll.AdbEnumInterfaces(usb_class_id, ctypes.c_bool('true'), ctypes.c_bool('true'), 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 = ' '*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
#自定义的GUID结构,有兴趣的可以自己研究用uuid模块
class GUID(ctypes.Structure):
    _fields_ = [("Data1", ctypes.c_ulong),
                ("Data2", ctypes.c_ushort),
                ("Data3", ctypes.c_ushort),
                ("Data4", ctypes.c_ubyte*8)]
#自己定义的一个结构体,便于使用DLL接口
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, 0xCBCB, 0x407d, (0x88, 0x14, 0x9e, 0xd6, 0x73, 0xd0, 0xdd, 0x6b))
enum_handle = dll.AdbEnumInterfaces(usb_class_id, ctypes.c_bool('true'), ctypes.c_bool('true'), 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 = ' '*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
ログイン後にコピー

上記の単純な Python コードでは、2 つの DLL AdbWinApi.dll と AdbWinUsbApi.dll を使用して、PC に接続されている Android デバイスを見つけることができます。

呼び出された DLL インターフェースは 3 つだけですが、目的は達成されました。次の結論が導き出されます:

ADB ツールを実装するために DLL を呼び出すのに Python を使用することは可能ですが、もちろん、たくさんの DLL インターフェースが呼び出されます。トラブル。


最後に書きましたが、PythonではCで書かれたDLLを呼び出すのはかなり面倒で、特にパラメータの受け渡し、特にポインタ処理にはctypesモジュールが必要です。 。 。


ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!