Golang get exe, DLL, APK version number

Source: Internet
Author: User
Tags unpack

Tag:golang    get EXE version     get DLL version     get APK version    

 golang  Implement read exe dll apk  version number package mainimport  (     " Flag "     fmt"      "Log"      "OS"       "Path/filepath") import  (     "Github.com/lunny/axmlparser") var  (     file fileinfo) const  (    MZ        =  "MZ"     PE       =  "PE"      RSRC     =  ". RSRC"     TYPET     = 16    PEOFFSET = 64    MACHINE   = 332    DEFAULT  =  ' C:\Windows\System32\cmd.exe ') type  fileinfo struct {    filepath string    version   string    debug    bool}func  (F *fileinfo)  checkerror (err error)  {     if err != nil {        log. Fatalln (Err)     }}//  get Exe dll version func  (f *fileinfo)  getexeversion ( )   (Err error)  {    file, err := os. Open (F.filepath)     f.checkerror (ERR)     //  first read 64 byte     buffer := make ([]byte, 64)     _, err =  file. Read (buffer)     f.checkerror (ERR)     defer file. Close ()     str := string (buffer[0])  + string (buffer[1])      if str != mz {        log. Fatalln ("Read EXE error, unable to find &NBSP;MZ.",  f.filepath)     }    peoffset := f.unpack ([]byte{buffer[60], buffer[61], buffer[62], &NBSP;BUFFER[63]})     if peOffset < PEOFFSET {         log. Fatalln ("peoffset  read error.",  f.filepath)     }    //  Reads from the beginning of the file to  peoffset, and reads the  24 byte    _, err = file for the second time. Seek (Int64 (Peoffset),  0)     buffer = make ([]byte, 24)      _, err = file. Read (buffer)     f.checkerror (ERR)     str = string (buffer[0])  + string (buffer[1])     if str != PE {         log. Fatalln ("Read EXE error, unable to find  pe.",  f.filepath)     }    machine  : = f.unpack ([]byte{buffer[4], bUFFER[5]})     if machine != MACHINE {         log. Fatalln ("machine  read error.",  f.filepath)     }    nosections  := f.unpack ([]byte{buffer[6], buffer[7]})     optHdrSize :=  F.unpack ([]byte{buffer[20], buffer[21]})     //  read shift from current position to  opthdrsize, third read  40 byte    file. Seek (Int64 (opthdrsize),  1)     resfound := false    for  i := 0; i < int (nosections); i++ {         buffer = make ([]byte, 40)         file . Read (buffer)         str = string (buffer[0])  +  String (buffer[1])  + string (buffer[2])  + string (buffER[3])  + string (buffer[4])         if str ==  Rsrc {            resfound = true             break         }    }    if !resFound {         log. Fatalln ("Read EXE error, unable to find &NBSP;.RSRC.",  f.filepath)     }    infovirt  := f.unpack ([]byte{buffer[12], buffer[13], buffer[14], buffer[15]})      infosize := f.unpack ([]byte{buffer[16], buffer[17], buffer[18], buffer[19 ]})     infooff := f.unpack ([]byte{buffer[20], buffer[21], buffer[22], &NBSP;BUFFER[23]})     //  read from the beginning of the file to  infooff, fourth read  infosize  Byte    file. Seek (Int64 (Infooff),  0)     buffer = make ([]byte, infosize)      _, err = file. Read (buffer)     f.checkerror (ERR)     nameentries := f.unpack ([]byte{buffer[12], buffer[13]})     identries := f.unpack ([]byte{buffer[14] ,  buffer[15]})     var infofound bool    var suboff , i int64    for i = 0; i <  (nameEntries +  identries);  i++ {        typet := f.unpack ([ ]BYTE{BUFFER[I*8+16],&NBSP;BUFFER[I*8+17],&NBSP;BUFFER[I*8+18],&NBSP;BUFFER[I*8+19]})          if typeT == TYPET {             infofound =  True            suboff = int64 (F.unpack ([]byte{buffer[i*8+20], buffer[i*8+21], buffer[i*8+22], buffer[i*8+23]}))             break         }    }    if !infoFound {         log. Fatalln ("Read EXE error, unable to find  typet == 16.",  f.filepath)     }     suboff = suboff & 0x7fffffff    infooff = f.unpack ([]byte{buffer[suboff+20], buffer[suboff+21], buffer[suboff+22], buffer[suboff+23]})  // offset of first fileinfo    infooff = infooff &  0x7fffffff    infooff = f.unpack ([]byte{buffer[infooff+20], buffer[infooff+ 21],&nBSP;BUFFER[INFOOFF+22],&NBSP;BUFFER[INFOOFF+23]})  //offset to data     Dataoff := f.unpack ([]byte{buffer[infooff], buffer[infooff+1], buffer[infooff+2],  BUFFER[INFOOFF+3]})     dataOff = dataOff - infoVirt     version1 := f.unpack ([]byte{buffer[dataoff+48], buffer[dataoff+48+1]})      version2 := f.unpack ([]byte{buffer[dataoff+48+2], buffer[dataoff+48+3]})      version3 := f.unpack ([]byte{buffer[dataoff+48+4], buffer[dataoff+48+5]})      version4 := f.unpack ([]byte{buffer[dataoff+48+6], buffer[dataoff+48+7]})     version := fmt. Sprintf ("%d.%d.%d.%d",  version2, version1, version4, version3)     f. version = version    return nil}func  (F *fileInfo) &NBSp;unpack (b []byte)   (Num int64)  {    for i := 0; i  < len (b);  i++ {        num = 256*num  + int64 ((B[len (b) -1-i] & 0xff))     }     return}//  Get  apk  version func  (f *fileinfo)  getapkversion ()   (Err error)  {    listener := new (Axmlparser.appnamelistener)     _,  err = axmlparser.parseapk (F.filepath, listener)     f.checkerror (ERR)     f.version = listener. Versionname    return nil}func init ()  {    flag. Stringvar (&file. filepath,  "path", default,  "get exe or dll or apk version  Information. ")     flag. Boolvar (&file. Debug,  "D", false,  "If true print exe or dll file name.")} Func main ()  {    flag. Parse ()     suffix := filepath. Ext (file. FilePath)     switch suffix {    case  ". exe",  ". dll ":         file. Getexeversion ()     case  ". apk":         file. Getapkversion ()     default:        log. Fatalln ("Can only get exe, DLL, APK version number, please re-enter the program path."),  file. FilePath)     }    switch {    case file. Debug:        fmt. Printf ("%s  version number is: ",  file. FilePath)     case file. Filepath == default:        flag. PrintdefaulTS ()         fmt. Printf ("%s  version number is: ",  file. FilePath)     }    fmt. Printf ("%s",  file. Version)}


This article is from the "Codyguo" blog, make sure to keep this source http://codyguo.blog.51cto.com/10440635/1700347

Golang get exe, DLL, APK version number

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.