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
/
PeFile.cls
< prev
next >
Wrap
Text File
|
2009-10-14
|
7KB
|
211 lines
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "PEFile"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Private Type Props
hMapFile As Long
hFile As Long
dwFileSize As Long
FileName As String
End Type
Private Type PESection
Section As IMAGE_SECTION_HEADER
Code() As Byte
End Type
Private Type PEStruct
DosHeader As IMAGE_DOS_HEADER
NTHeader As IMAGE_NT_HEADERS
OptHeader As IMAGE_OPTIONAL_HEADER
Sections() As PESection
DosStub() As Byte
End Type
Private Type MemoryPE
DosHeader As IMAGE_DOS_HEADER
DosStub() As Byte
NTHeader As IMAGE_NT_HEADERS
End Type
Dim Mem As Props 'Properties For this Class
Dim PE As PEStruct 'Stores PE Formats
Property Get FileName() As String
FileName = Mem.FileName
End Property
Property Get Base() As Long
Base = Mem.hMapFile
End Property
Function LoadFile(ByVal FileName As String) As Boolean
On Error Resume Next
Mem.hFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, ByVal 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
If Mem.hFile = -1 Then
SetError "Error Opening File. Check if the File exists and is not locked."
Exit Function
End If
Mem.dwFileSize = GetFileSize(Mem.hFile, 0)
If Mem.dwFileSize = 0 Then
SetError "FileSize Error. Check if the file is locked by other applications"
Exit Function
End If
Mem.hMapFile = GlobalAlloc(GMEM_FIXED Or GMEM_ZEROINIT, Mem.dwFileSize)
If Mem.hMapFile = 0 Then
SetError "Error Allocating Memory Space."
Exit Function
End If
If ReadFile(Mem.hFile, ByVal Mem.hMapFile, Mem.dwFileSize, 0, ByVal 0) = 0 Then
SetError "Error in Reading File"
Exit Function
End If
'***********Close the File Handle
'CopyMemory PE.DosHeader, ByVal Base, Len(PE.DosHeader)
If PE.DosHeader.e_magic <> IMAGE_DOS_SIGNATURE Then
SetError "Not a valid PE File. MZ Signature not found"
Exit Function
End If
CopyMemory PE.NTHeader, ByVal Base + PE.DosHeader.e_lfanew, Len(PE.NTHeader)
If PE.NTHeader.Signature <> IMAGE_NT_SIGNATURE Then
SetError "Not a Valid PE File. PE Signature not found"
Exit Function
End If
'Get the Dos Stub
Dim dwStubSize As Long
Dim bytes() As Byte, dwBytes As Long
dwStubSize = PE.DosHeader.e_lfanew - Len(PE.DosHeader)
ReDim bytes(dwStubSize)
CopyMemory bytes(0), ByVal Base + Len(PE.DosHeader), dwStubSize
PE.DosStub = bytes
'Deal with NT Header of PE File
Dim idx As Long
Dim dwFirstSectionOffset As Long
dwFirstSectionOffset = PE.DosHeader.e_lfanew + Len(PE.NTHeader)
With PE.NTHeader
'************Populate Section Headers
Dim SectionHdr As IMAGE_SECTION_HEADER
Dim sctCount As Long
sctCount = PE.NTHeader.FileHeader.NumberOfSections
ReDim PE.Sections(1 To sctCount)
For idx = 1 To sctCount
CopyMemory SectionHdr, ByVal Base + dwFirstSectionOffset + (idx - 1) * Len(SectionHdr), Len(SectionHdr)
PE.Sections(idx).Section = SectionHdr
'dwBytes = PEAlign(SectionHdr.SizeOfRawData, PE.NTHeader.OptionalHeader.SectionAlignment)
dwBytes = SectionHdr.SizeOfRawData
ReDim bytes(dwBytes)
CopyMemory bytes(0), ByVal Base + SectionHdr.PointerToRawData, SectionHdr.SizeOfRawData
PE.Sections(idx).Code = bytes
Next
End With
CloseHandle Mem.hFile
End Function
Function SaveFile(ByVal FileName As String) As Boolean
Mem.hFile = CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_WRITE Or FILE_SHARE_READ, ByVal 0, CREATE_ALWAYS, 0, ByVal 0)
If Mem.hFile = -1 Then
SetError "Error Creating File For Writing. Make sure the disk is ready."
Exit Function
End If
If IsBadCodePtr(Base) = False Then
Mem.hMapFile = 0
Mem.hMapFile = GlobalAlloc(GMEM_FIXED Or GMEM_ZEROINIT, Mem.dwFileSize)
End If
If Mem.hMapFile = 0 Then
SetError "Error in Memory Allocation"
Exit Function
End If
FillMemory ByVal Base, GlobalSize(Base), 0
AlignSections
CopyMemory ByVal Base, PE.DosHeader, Len(PE.DosHeader)
Dim bytes() As Byte
bytes = PE.DosStub
CopyMemory ByVal Base + Len(PE.DosHeader), bytes(0), UBound(PE.DosStub)
CopyMemory ByVal Base + PE.DosHeader.e_lfanew, PE.NTHeader, Len(PE.NTHeader)
Dim dwFirstSectionOffset As Long
dwFirstSectionOffset = PE.DosHeader.e_lfanew + Len(PE.NTHeader)
Dim sctNum As Long, idx As Long
sctNum = UBound(PE.Sections)
For idx = 1 To sctNum
CopyMemory ByVal Base + dwFirstSectionOffset + (idx - 1) * Len(PE.Sections(idx).Section), PE.Sections(idx).Section, Len(PE.Sections(idx).Section)
CopyMemory ByVal Base + PE.Sections(idx).Section.PointerToRawData, PE.Sections(idx).Code(0), PE.Sections(idx).Section.SizeOfRawData
Next
SetFilePointer hFile, 0, 0, FILE_BEGIN
WriteFile Mem.hFile, ByVal Base, GlobalSize(Base), 0, ByVal 0
CloseHandle Mem.hFile
SaveFile = True
End Function
'************************************************************************************
'Private Routines
'************************************************************************************
Private Sub AlignSections()
End Sub
Private Sub SetError(ByVal Desc As String)
Err.Description = Desc
End Sub
Private Function GetString(ByVal hMem As Long) As String
Dim nLen As Long
nLen = StrLenPtr(hMem)
Dim st As String
st = String(nLen, 0)
StrCpyPtrToStr st, hMem
GetString = st
End Function
Private Function GetLong(ByVal hMem As Long) As Long
CopyMemory GetLong, ByVal hMem, 4
End Function
Private Function GetInt(ByVal hMem As Long) As Integer
CopyMemory GetInt, ByVal hMem, 2
End Function
Private Function GetByte(ByVal hMem As Long) As Byte
CopyMemory GetByte, ByVal hMem, 1
End Function
Function PEAlign(ByVal dwTarNum As Long, ByVal dwAlignTo As Long) As Long
PEAlign = (((dwTarNum + dwAlignTo - 1) / dwAlignTo) * dwAlignTo)
End Function
Private Property Get NewBase() As Long
NewBase = Mem.hMapFile
End Property
'**************************************************************************************
'**************************************************************************************
'Class Termiantion
'**************************************************************************************
'**************************************************************************************
Private Sub Class_Terminate()
If Mem.hMapFile <> 0 Then
GlobalFree Mem.hMapFile
End If
End Sub
'**************************************************************************************
'Properties For PE
'**************************************************************************************
Property Get DosStub() As String
DosStub = StrConv(PE.DosStub, vbUnicode)
End Property
Property Get EntryPoint() As Long
EntryPoint = PE.NTHeader.OptionalHeader.AddressOfEntryPoint
End Property