码迷,mamicode.com
首页 > Windows程序 > 详细

delphi获取dll的函数列表

时间:2017-07-24 20:24:57      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:dll   ring   obj   add   .net   函数   image   tail   pfile   

找了几个,终于找到一个好用的

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;

 

http://blog.csdn.net/youthon/article/details/7666727

delphi获取dll的函数列表

标签:dll   ring   obj   add   .net   函数   image   tail   pfile   

原文地址:http://www.cnblogs.com/findumars/p/7230718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!