ADB shell screencap 명령으로 디바이스 내부에 별도이미지파일 저장없이 바로 HBITMAP 이미지로 가져오는 방법 (32비트 오토핫키용)


테스트 에뮬레이터 - [MOMO]앱플레이어




;--------------------------------------------------------------------

#SingleInstance Force

#include Gdip.ahk  ;https://autohotkey.com/board/topic/29449-gdi-standard-library-145-by-tic/


;MCode Func

AdjustScreencapData := MCode("2,x86:VVdWU4tsJBSLRCQYjXQF/zn1d1ONfv6NTQGJ6usRZpCJyIPCAYhZ/4PBATnydyI51w+2GnbqgPsNdeWAegENdd8PtloCgPsKdBa7DQAAAOvPKehbXl9dw5CNtCYAAAAAiciDwgPrvjHA6+iQkJCQkA==")

ScreencapToDIB := MCode("2,x86:VVdWU4PsDItEJCiLdCQkhcB+fY0EtQAAAACLVCQsxwQkAAAAAMdEJAQAAAAA99iJRCQIi0QkKIPoAQ+vxo08goX2fjuLRCQgi0wkBDHbjSyIi0SdDInCweoQD7bKicIlAP8AAMHiEIHiAAD/AAnKCdCJBJ+DwwE53nXWAXQkBIMEJAEDfCQIiwQkOUQkKHWwg8QMW15fXcOQkJCQkJCQkA==")



^\::ExitApp


F1::

  RegRead, MomoDir, HKCU, Software\NOXGAMES\MOMO, InstallDir

  adb_path = %MomoDir%adb.exe

  Runwait, "%adb_path%" devices


  sCmd = "%adb_path%" -s "127.0.0.1:5555" shell screencap  ;127.0.0.1:5555 = MoMo 기본 앱플레이어

  if hBitmap := ADBScreenCapStdOutToHBitmap( sCmd )

  {

    SaveHBITMAPToFile(hBitmap, "_testImage.bmp")

    DllCall("DeleteObject", Ptr,hBitmap)

  }

  MsgBox, % hBitmap? "screen cap 이미지 추출성공":"실패"

return


;--------------------------------------------------------------------

;https://autohotkey.com/board/topic/96903-simplified-versions-of-seans-stdouttovar/

ADBScreenCapStdOutToHBitmap( sCmd ) {

  global AdjustScreencapData, ScreencapToDIB


  DllCall( "CreatePipe", UIntP,hPipeRead, UIntP,hPipeWrite, UInt,0, UInt,0 )

  DllCall( "SetHandleInformation", UInt,hPipeWrite, UInt,1, UInt,1 )


  VarSetCapacity( STARTUPINFO, 68,  0 )      ; STARTUPINFO          ;  http://goo.gl/fZf24

  NumPut( 68,         STARTUPINFO,  0 )      ; cbSize

  NumPut( 0x100,      STARTUPINFO, 44 )      ; dwFlags    =>  STARTF_USESTDHANDLES = 0x100 

  NumPut( hPipeWrite, STARTUPINFO, 60 )      ; hStdOutput

  NumPut( hPipeWrite, STARTUPINFO, 64 )      ; hStdError


  VarSetCapacity( PROCESS_INFORMATION, 16 )  ; PROCESS_INFORMATION  ;  http://goo.gl/b9BaI      


  If ! DllCall( "CreateProcess", UInt,0, UInt,&sCmd, UInt,0, UInt,0 ;  http://goo.gl/USC5a

              , UInt,1, UInt,0x08000000, UInt,0, UInt,0

              , UInt,&STARTUPINFO, UInt,&PROCESS_INFORMATION ) 

    Return "" 

    , DllCall( "CloseHandle", UInt,hPipeWrite ) 

    , DllCall( "CloseHandle", UInt,hPipeRead )

    , DllCall( "SetLastError", Int,-1 )


  hProcess := NumGet( PROCESS_INFORMATION, 0 )                 

  hThread  := NumGet( PROCESS_INFORMATION, 4 )                      


  DllCall( "CloseHandle", UInt,hPipeWrite )


  block := {}, blockIndex := 0, allSize := 0, nPipeAvail := 4096

  loop

  {

    ++blockIndex

    block[blockIndex] := {data:"", size:0, addr:0}

    ObjSetCapacity(block[blockIndex], "data", nPipeAvail)

    block[blockIndex].addr := ObjGetAddress(block[blockIndex], "data")

    

    nSz := 0

    if !DllCall( "ReadFile", UInt,hPipeRead, UInt,block[blockIndex].addr, UInt,nPipeAvail, UIntP,nSz, UInt,0 )

      break

    block[blockIndex].size := nSz, allSize += nSz

  }


  DllCall( "GetExitCodeProcess", UInt,hProcess, UIntP,ExitCode )

  DllCall( "CloseHandle", UInt,hProcess  )

  DllCall( "CloseHandle", UInt,hThread   )

  DllCall( "CloseHandle", UInt,hPipeRead )


  if allSize

  {

    VarSetCapacity( bin, allSize, 0 ), tar := &bin

    for k,v in block

      if v.size

        DllCall("RtlMoveMemory", "UPTR",tar, "UPTR",v.addr, "UInt",v.size), tar += v.size

    allSize := DllCall(AdjustScreencapData, "UPTR",&bin, "UInt",allSize, "cdecl")

    width  := NumGet(&bin, 0, "uint"), height := NumGet(&bin, 4, "uint")

    hBM := CreateDIBSection(width, height,"",32, ppvBits)  

    DllCall(ScreencapToDIB, "UPtr",&bin, "UInt",width, "UInt",height, "UPtr",ppvBits, "cdecl")

    return hBM

  }

  

}

 

;--------------------------------------------------------------------

MCode(mcode) {

static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"

if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))

return

if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))

return

p := DllCall("GlobalAlloc", "uint", 0, "ptr", s, "ptr")

if (c="x64")

DllCall("VirtualProtect", "ptr", p, "ptr", s, "uint", 0x40, "uint*", op)

if (DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", p, "uint*", s, "ptr", 0, "ptr", 0))

return p

DllCall("GlobalFree", "ptr", p)

    return

}


SaveHBITMAPToFile(hBitmap, sFile)

{

  VarSetCapacity(DIBSECTION, A_PtrSize=8? 104:84, 0)

  NumPut(40, DIBSECTION, A_PtrSize=8? 32:24,"UInt") ;dsBmih.biSize

  DllCall("GetObject", "UPTR", hBitmap, "int", A_PtrSize=8? 104:84, "UPTR", &DIBSECTION)

  hFile:= DllCall("CreateFile", "UPTR", &sFile, "Uint", 0x40000000, "Uint", 0, "Uint", 0, "Uint", 2, "Uint", 0, "Uint", 0)

  DllCall("WriteFile", "UPTR", hFile, "int64P", 0x4D42|14+40+(biSizeImage:=NumGet(DIBSECTION, A_PtrSize=8? 52:44, "UInt"))<<16, "Uint", 6, "UintP", 0, "Uint", 0)

  DllCall("WriteFile", "UPTR", hFile, "int64P", 54<<32, "Uint", 8, "UintP", 0, "Uint", 0)

  DllCall("WriteFile", "UPTR", hFile, "UPTR", &DIBSECTION + (A_PtrSize=8? 32:24), "Uint", 40, "UintP", 0, "Uint", 0)

  DllCall("WriteFile", "UPTR", hFile, "Uint", NumGet(DIBSECTION, A_PtrSize=8? 24:20, "UPtr"), "Uint", biSizeImage, "UintP", 0, "Uint", 0)

  DllCall("CloseHandle", "UPTR", hFile)

}







Posted by 와이로