home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / EliteXp®_P21651610152009.psc / PE.cls < prev    next >
Text File  |  2009-10-15  |  9KB  |  272 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "PE"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Private Type Props
  15.     hMapFile As Long
  16.     hFile As Long
  17.     dwFileSize As Long
  18.     FileName As String
  19.     FileLoaded As Boolean
  20. End Type
  21.  
  22. Private Type Objects
  23.     DosHeader As DosHeader
  24.     NtHeader As NtHeader
  25.     Sections As Sections
  26.     BoundImports As BoundImports
  27. End Type
  28.  
  29. Dim Mem As Props    'Properties For this Class
  30. Dim nFile As Props  'Properties For New File
  31. Dim Obj As Objects
  32.  
  33. Property Get FileName() As String
  34. FileName = Mem.FileName
  35. End Property
  36. Property Get Base() As Long
  37. Base = Mem.hMapFile
  38. End Property
  39.  
  40. Function LoadFile(ByVal FileName As String) As Boolean
  41. On Error Resume Next
  42. Mem.FileLoaded = False
  43. Mem.hFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, ByVal 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  44. If Mem.hFile = -1 Then
  45.     SetError "Error Opening File. Check if the File exists and is not locked."
  46.     Exit Function
  47. End If
  48. Mem.FileName = FileName
  49. Mem.dwFileSize = GetFileSize(Mem.hFile, 0)
  50. If Mem.dwFileSize = 0 Then
  51.     SetError "FileSize Error. Check if the file is locked by other applications"
  52.     Exit Function
  53. End If
  54. Mem.hMapFile = GlobalAlloc(GMEM_FIXED Or GMEM_ZEROINIT, Mem.dwFileSize)
  55. If Mem.hMapFile = 0 Then
  56.     SetError "Error Allocating Memory Space."
  57.     Exit Function
  58. End If
  59. If ReadFile(Mem.hFile, ByVal Mem.hMapFile, Mem.dwFileSize, 0, ByVal 0) = 0 Then
  60.     SetError "Error in Reading File"
  61.     Exit Function
  62. End If
  63. '***********Close the File Handle
  64. DosHeader.Fill Mem.hMapFile
  65. If DosHeader.Magic <> IMAGE_DOS_SIGNATURE Then
  66.     CloseFile
  67.     SetError "Not a Valid PE File.Magic Number not Found."
  68.     Exit Function
  69. End If
  70. NtHeader.Fill Base + DosHeader.lFaNew
  71. If NtHeader.Signature <> IMAGE_NT_SIGNATURE Then
  72.     CloseFile
  73.     SetError "Not a Valid PE File.PE Signature not Found."
  74.     Exit Function
  75. End If
  76. Dim idx As Long, Bytes() As Byte, dwBytes As Long
  77. Dim tmpSection As Section
  78. Dim dwFirstSecOffset As Long
  79. dwFirstSecOffset = DosHeader.lFaNew + NtHeader.Length 'First Section Occurs After NtHeader
  80. For idx = 0 To NtHeader.FileHeader.NumberOfSections - 1
  81.     Set tmpSection = New Section
  82.     tmpSection.Fill Base + dwFirstSecOffset + idx * tmpSection.Length
  83.     tmpSection.FillCode Base + tmpSection.PointerToRawData, tmpSection.SizeOfRawData
  84.     Sections.Add tmpSection
  85. Next
  86. Dim dwBIoffset As Long
  87. dwBIoffset = dwFirstSecOffset + Sections.Count * tmpSection.Length
  88. Dim tmpBi As BoundImport, tmpStr As String
  89. OffSet = 0
  90. Do
  91.     Set tmpBi = New BoundImport
  92.     tmpBi.Fill Base + dwBIoffset + OffSet
  93.     dwBytes = StrLenPtr(Base + tmpBi.ModuleNameOffSet + dwBIoffset)
  94.     If dwBytes <> 0 And tmpBi.ModuleNameOffSet <> 0 Then
  95.         tmpStr = String(dwBytes, 0)
  96.         StrCpyPtrToStr tmpStr, Base + tmpBi.ModuleNameOffSet + dwBIoffset
  97.         tmpBi.ModuleName = tmpStr
  98.         BoundImports.Add tmpBi
  99.     End If
  100.     OffSet = OffSet + tmpBi.Length
  101. Loop While tmpBi.TimeStamp <> 0
  102. Mem.FileLoaded = True
  103. CloseFile
  104. LoadFile = True
  105. End Function
  106. Function SaveFile(ByVal FileName As String) As Boolean
  107. Dim tMem As Props
  108. Dim Bytes() As Byte, idx As Long, OffSet As Long, dwFirstOffSet As Long
  109. If Mem.FileLoaded = False Then
  110.     SetError "Source PE not loaded. Make sure the input was a valid PE File."
  111.     Exit Function
  112. End If
  113. nFile = tMem
  114.  
  115. nFile.dwFileSize = DosHeader.Length + NtHeader.Length + LenBytes(DosStub)
  116. nFile.dwFileSize = nFile.dwFileSize + Sections.Count * Sections(1).Length
  117. Dim fPtr As Long, tmp As Long
  118. For idx = 1 To Sections.Count
  119.     tmp = Sections(idx).PointerToRawData + Sections(idx).SizeOfRawData
  120.     If tmp > fPtr Then fPtr = tmp
  121. Next
  122. nFile.dwFileSize = nFile.dwFileSize + fPtr
  123. nFile.hFile = CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_WRITE Or FILE_SHARE_READ, ByVal 0, CREATE_ALWAYS, 0, 0)
  124. If nFile.hFile = -1 Then
  125.     SetError "Error Creating File on Disk! Make sure the disk is ready."
  126.     Exit Function
  127. End If
  128. nFile.hMapFile = GlobalAlloc(GMEM_FIXED Or GMEM_ZEROINIT, nFile.dwFileSize)
  129. If nFile.hMapFile = 0 Then
  130.     SetError "Error Allocating Memory! Try closing some applications."
  131.     CloseNewFile
  132.     Exit Function
  133. End If
  134. If DosHeader.CopyTo(nFile.hMapFile) = False Then
  135.     SetError "Error Setting Dos Header!"
  136.     CloseNewFile
  137.     Exit Function
  138. End If
  139. '************Copy The Dos Stub**************
  140.  
  141. Bytes = DosStub
  142. idx = UBound(Bytes)
  143. CopyMemory ByVal NewBase + DosHeader.Length, Bytes(0), idx
  144. '***********Modify Number of Sections
  145. Dim fh  As IMAGE_FILE_HEADER
  146. fh = NtHeader.FileHeader.Struct
  147. fh.NumberOfSections = Sections.Count
  148. NtHeader.FileHeader.Struct = fh
  149. '*********************************************************
  150. Dim hdr As IMAGE_OPTIONAL_HEADER
  151. hdr = NtHeader.OptionalHeader.Struct
  152. hdr.DataDirectory(11).VirtualAddress = 0
  153. hdr.DataDirectory(11).Size = 0
  154. NtHeader.OptionalHeader.Struct = hdr
  155.  
  156. If NtHeader.CopyTo(NewBase + DosHeader.lFaNew) = False Then
  157.     SetError "Error Setting NT Header!"
  158.     CloseNewFile
  159.     Exit Function
  160. End If
  161. If Sections.Count < 1 Then
  162.     SetError "PE File must contain at-least 1 section"
  163.     CloseNewFile
  164.     Exit Function
  165. End If
  166.  
  167.  
  168.  
  169. Dim tmpBi As BoundImport
  170. dwFirstOffSet = DosHeader.lFaNew + NtHeader.Length + Sections.Count * Sections(1).Length
  171. OffSet = 0
  172. For Each tmpBi In BoundImports
  173.     tmpBi.CopyTo NewBase + dwFirstOffSet + OffSet
  174.     Bytes = StrConv(tmpBi.ModuleName, vbFromUnicode)
  175.     dwBytes = UBound(Bytes) + 1
  176.     CopyMemory ByVal NewBase + dwFirstOffSet + tmpBi.ModuleNameOffSet, Bytes(0), dwBytes
  177.     OffSet = OffSet + tmpBi.Length
  178. Next
  179. '*************Section***************
  180. Dim tmpSection As Section
  181. dwFirstOffSet = DosHeader.lFaNew + NtHeader.Length
  182.  
  183. For idx = 1 To Sections.Count
  184.     Set tmpSection = Sections(idx)  'Collection is 1 Based
  185.     OffSet = NewBase + dwFirstOffSet + (idx - 1) * tmpSection.Length
  186.     If tmpSection.CopyTo(OffSet) = False Then
  187.         SetError "Error Setting Section: " & tmpSection.Name
  188.         CloseNewFile
  189.         Exit Function
  190.     End If
  191.     OffSet = NewBase + tmpSection.PointerToRawData
  192.     If tmpSection.CopyCodeTo(OffSet) = False Then
  193.         SetError "Error Setting Section Code: " & tmpSection.Name
  194.         CloseNewFile
  195.         Exit Function
  196.     End If
  197.    
  198. Next
  199.  
  200. WriteFile nFile.hFile, ByVal NewBase, nFile.dwFileSize, 0, ByVal 0
  201. GlobalFree NewBase
  202. CloseNewFile
  203. End Function
  204.  
  205.  
  206.  
  207.  
  208. '***************************Private Procedures************
  209. '***********************************************************
  210.  
  211. Private Sub SetError(ByVal Desc As String)
  212. Err.Description = Desc
  213. End Sub
  214. Private Sub CloseFile()
  215. CloseHandle Mem.hFile
  216. End Sub
  217. Private Sub CloseNewFile()
  218.  
  219. CloseHandle nFile.hFile
  220. End Sub
  221. Private Function PEAlign(ByVal dwTarNum As Long, ByVal dwAlignTo As Long) As Long
  222. PEAlign = (((dwTarNum + dwAlignTo - 1) / dwAlignTo) * dwAlignTo)
  223. End Function
  224. Private Sub Class_Terminate()
  225. If Mem.hMapFile <> 0 Then
  226.     If IsBadCodePtr(Mem.hMapFile) = False Then Exit Sub
  227.     GlobalFree Mem.hMapFile
  228. End If
  229. End Sub
  230. Private Property Get NewBase() As Long
  231. NewBase = nFile.hMapFile
  232. End Property
  233.  
  234. '****************************Dependent Properties****************
  235. '****************************************************************
  236. Property Get DosStub() As Byte()
  237. Dim dwSize As Long
  238. dwSize = DosHeader.lFaNew - DosHeader.Length
  239. Dim ret() As Byte
  240. ReDim ret(dwSize - 1)
  241. CopyMemory ret(0), ByVal Base + DosHeader.Length, dwSize
  242. DosStub = ret
  243. End Property
  244. Property Get DosHeader() As DosHeader
  245. If Obj.DosHeader Is Nothing Then
  246.     Set Obj.DosHeader = New DosHeader
  247.     Obj.DosHeader.SetOwner Me
  248. End If
  249. Set DosHeader = Obj.DosHeader
  250. End Property
  251. Property Get NtHeader() As NtHeader
  252. If Obj.NtHeader Is Nothing Then
  253.     Set Obj.NtHeader = New NtHeader
  254.     Obj.NtHeader.SetOwner Me
  255. End If
  256. Set NtHeader = Obj.NtHeader
  257. End Property
  258. Property Get Sections() As Sections
  259. If Obj.Sections Is Nothing Then
  260.     Set Obj.Sections = New Sections
  261.     Obj.Sections.SetOwner Me
  262. End If
  263. Set Sections = Obj.Sections
  264. End Property
  265. Property Get BoundImports() As BoundImports
  266. If Obj.BoundImports Is Nothing Then
  267.     Set Obj.BoundImports = New BoundImports
  268.     Obj.BoundImports.SetOwner Me
  269. End If
  270. Set BoundImports = Obj.BoundImports
  271. End Property
  272.