同一函数,我用在VB_Shell里面的。
Function GetProcAddressDirectly(ByVal lpImageDosHeader As IMAGE_DOS_HEADER Ptr, FuncName As Asciiz) As Dword
Dim lpImageNtHeaders As Local IMAGE_NT_HEADERS Ptr
Dim lpImageExportDirectory As Local IMAGE_EXPORT_DIRECTORY Ptr
Dim lpNameOrdinals As Local Word Ptr
Dim lpFunctions As Local Dword Ptr
Dim lpName As Local Dword Ptr
Dim lpExpFuncName As Local Asciiz Ptr
Dim i As Local Dword
Dim j As Local Dword
Dim lpFuncName As Asciiz Ptr
If @lpImageDosHeader.e_magic <> %IMAGE_DOS_SIGNATURE Then ' invalid DOS signature
Function = 1
MsgBox "Invalid DOS signature",%MB_ICONWARNING,"Error in GetProcAddressDirectly()"
Exit Function
End If
lpImageNtHeaders = lpImageDosHeader + @lpImageDosHeader.e_lfanew
'================================
If @lpImageNtHeaders.Signature <> %IMAGE_NT_SIGNATURE Then ' Invalid NT signature
Function = 1
MsgBox "Invalid NT signature",%MB_ICONWARNING,"Error in GetProcAddressDirectly()"
Exit Function
End If
'================================
If @lpImageNtHeaders.FileHeader.SizeOfOptionalHeader <> SizeOf(@lpImageNtHeaders.OptionalHeader) Or _
@lpImageNtHeaders.OptionalHeader.Magic <> %IMAGE_NT_OPTIONAL_HDR32_MAGIC Then
Function = 0
MsgBox "SizeOfOptionalHeader or OptionalHeader.Magic",%MB_ICONWARNING,"Error in GetProcAddressDirectly()"
Exit Function
End If
lpImageExportDirectory = @lpImageNtHeaders.OptionalHeader.DataDirectory(%IMAGE_DIRECTORY_ENTRY_EXPORT).VirtualAddress
'================================
If lpImageExportDirectory = 0 Then
Function = 0
MsgBox "lpImageExportDirectory",%MB_ICONWARNING,"Error in GetProcAddressDirectly()"
Exit Function
End If
lpImageExportDirectory = lpImageExportDirectory + lpImageDosHeader
'================================
If HiWrd(lpFuncName) Then ' Name
For i = 0 To @lpImageExportDirectory.NumberOfFunctions - 1
If @lpFunctions[i] Then
For j = 0 To @lpImageExportDirectory.NumberOfNames - 1
If @lpNameOrdinals[j] = i Then
lpExpFuncName = @lpName[j] + lpImageDosHeader
If @lpExpFuncName = FuncName Then Function = @lpFunctions[i] + lpImageDosHeader : Exit Function
End If
Next
End If
Next
Else
For i = 0 To @lpImageExportDirectory.NumberOfFunctions - 1
If lpFuncName = @lpImageExportDirectory.nBase + i Then
If @lpFunctions[i] Then Function = @lpFunctions[i] + lpImageDosHeader
Exit Function
End If
Next
End If
'================================
// Get the IMAGE_SECTION_HEADER that contains the exports. This is
// usually the .edata section, but doesn't have to be.
PIMAGE_SECTION_HEADER header;
header = GetEnclosingSectionHeader( exportsStartRVA, pNTHeader );
if ( !header ) return 0;