1. 程式人生 > >delphi獲取dll的函式列表

delphi獲取dll的函式列表

找了幾個,終於找到一個好用的

function GetDLLFileExports(

  szFileName: PChar;
  mStrings: TStrings
): Boolean;
var
  hFile: THANDLE;
  hFileMapping: THANDLE;
  lpFileBase: Pointer;
  pImg_DOS_Header: PImageDosHeader;
  pImg_NT_Header: PImageNtHeaders;
  pImg_Export_Dir: PImageExportDirectory;
  ppdwNames: ^PDWORD;
  szFunc: PChar;
  i: Integer;
begin
  Result := False;
  if not Assigned(mStrings) then Exit;
  hFile := CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, nil,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if(hFile = INVALID_HANDLE_VALUE) then Exit;
  hFileMapping := CreateFileMapping(hFile, nil, PAGE_READONLY, 0, 0, nil);
  if hFileMapping = 0 then
  begin
    CloseHandle(hFile);
    Exit;
  end;

  lpFileBase := MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
  if lpFileBase = nil then
  begin
    CloseHandle(hFileMapping);
    CloseHandle(hFile);
    Exit;
  end;

  pImg_DOS_Header := PImageDosHeader(lpFileBase);
  pImg_NT_Header := PImageNtHeaders(
    Integer(pImg_DOS_Header) + Integer(pImg_DOS_Header._lfanew));

  if IsBadReadPtr(pImg_NT_Header, SizeOf(IMAGE_NT_HEADERS)) or
    (pImg_NT_Header.Signature <> IMAGE_NT_SIGNATURE) then
  begin
    UnmapViewOfFile(lpFileBase);
    CloseHandle(hFileMapping);
    CloseHandle(hFile);
    Exit;
  end;

  pImg_Export_Dir := PImageExportDirectory(
    pImg_NT_Header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].
      VirtualAddress);
  if not Assigned(pImg_Export_Dir) then
  begin
    UnmapViewOfFile(lpFileBase);
    CloseHandle(hFileMapping);
    CloseHandle(hFile);
    Exit;
  end;
  // 63 63 72 75 6E 2E 63 6F 6D
  pImg_Export_Dir := PImageExportDirectory(
    ImageRvaToVa(pImg_NT_Header, pImg_DOS_Header, DWORD(pImg_Export_Dir),
    PImageSectionHeader(Pointer(nil)^)));

  ppdwNames := Pointer(pImg_Export_Dir.AddressOfNames);

  ppdwNames := Pointer(ImageRvaToVa(pImg_NT_Header, pImg_DOS_Header,
    DWORD(ppdwNames), PImageSectionHeader(Pointer(nil)^)));
  if not Assigned(ppdwNames) then
  begin
    UnmapViewOfFile(lpFileBase);
    CloseHandle(hFileMapping);
    CloseHandle(hFile);
    Exit;
  end;

  for i := 0 to pImg_Export_Dir.NumberOfNames - 1 do
  begin
    szFunc := PChar(ImageRvaToVa(pImg_NT_Header, pImg_DOS_Header,
      DWORD(ppdwNames^), PImageSectionHeader(Pointer(nil)^)));
    mStrings.Add(szFunc);
    Inc(ppdwNames);
  end;
  UnmapViewOfFile(lpFileBase);
  CloseHandle(hFileMapping);
  CloseHandle(hFile);
  Result := True;

end;

//呼叫示例

procedure TF_Main.btnTestClick(Sender: TObject);
begin
  GetDLLFileExports('F:\光碟燒錄\Reclib\librtmp.dll', Memo1.Lines);
end;