Delphi 封装动态库调用

unit mp4Impl;

interface

uses
  Windows;

  const
    TITLEMAXLEN=1024;
    DLLNAME = 'xxx.dll';
//函数定义
  TMP4Title_Init=function(filename:pchar):boolean; stdcall;
  TMP4Title_Uninit=procedure(); stdcall;
  TMP4Title_GetChapterCount=function(mtype:TChapterType=ChapterTypeAny):integer; stdcall;
  TMP4Title_GetChapter=function(pArr:TChaptArr; count:integer;
                mtype:TChapterType=ChapterTypeAny):integer; stdcall;
  TMP4Title_AddChapter=function(pArr: TChaptArr; count:integer;
                mtype:TChapterType=ChapterTypeAny):boolean; stdcall;
  TMP4Title_DelChapter=function(timepos:integer; mtype:TChapterType=ChapterTypeAny):boolean;stdcall;
  TMP4Title_DelAllChapter=procedure(mtype:TChapterType=ChapterTypeAny);stdcall;
//接口封装
  TMP4InfoAPI=record
    private
      FHandle: THandle;
      FMP4TitleInit: TMP4Title_Init;
      FMP4TitleUninit: TMP4Title_Uninit;
      FMP4TitleGetChapterCount: TMP4Title_GetChapterCount;
      FMP4TitleGetChapter: TMP4Title_GetChapter;
      FMP4TitleAddChapter: TMP4Title_AddChapter;
      FMP4TitleDelChapter: TMP4Title_DelChapter;
      FMP4TitleDelAllChapter: TMP4Title_DelAllChapter;
    public
      function init(): Boolean;
      procedure Free;
      function MP4TitleInit(filename:pchar):boolean;
      procedure MP4TitleUninit();
      function MP4TitleGetChapterCount(mtype:TChapterType=ChapterTypeAny):integer;
      function MP4TitleGetChapter(pArr:TChaptArr; count:integer;
                mtype:TChapterType=ChapterTypeAny):integer;
      function MP4TitleAddChapter(pArr: TChaptArr; count:integer;
                mtype:TChapterType=ChapterTypeAny):boolean;
      function MP4TitleDelChapter(timepos:integer; mtype:TChapterType=ChapterTypeAny):boolean;
      procedure MP4TitleDelAllChapter(mtype:TChapterType=ChapterTypeAny);
  end;

implementation
//函数入口名
const
  SMP4TitleInit = 'mp4info_init';
  SMP4TitleUninit = 'mp4info_uninit';
  SMP4TitleGetChapterCount = 'mp4info_getchaptercount';
  SMP4TitleGetChapter = 'mp4info_getchapter';
  SMP4TitleAddChapter = 'mp4info_addchapter';
  SMP4TitleDelChapter = 'mp4info_addchapter';
  SMP4TitleDelAllChapter = 'mp4info_delallchapter';
{ TMP4InfoAPI }

procedure TMP4InfoAPI.Free;
begin
  if FHandle <> 0 then
  begin
    FreeLibrary(FHandle);
    FHandle := 0;
    FillChar(Self, SizeOf(Self), 0);
  end;
end;

function TMP4InfoAPI.init: Boolean;  //动态加载动态库,初始化函数入口
begin
  FHandle := 0;
  Windows.InterlockedExchange(Integer(FHandle), LoadLibrary(MP4INFODLLNAME));
  if FHandle <> 0 then
  begin
    FMP4TitleInit := GetProcAddress(FHandle, SMP4TitleInit);
    FMP4TitleUninit := GetProcAddress(FHandle, SMP4TitleUninit);
    FMP4TitleGetChapterCount := GetProcAddress(FHandle, SMP4TitleGetChapterCount);
    FMP4TitleGetChapter := GetProcAddress(FHandle, SMP4TitleGetChapter);
    FMP4TitleAddChapter := GetProcAddress(FHandle, SMP4TitleAddChapter);
    FMP4TitleDelChapter := GetProcAddress(FHandle, SMP4TitleDelChapter);
    FMP4TitleDelAllChapter := GetProcAddress(FHandle, SMP4TitleDelAllChapter);
  end;
  Result:=FHandle<>0;
end;

function TMP4InfoAPI.MP4TitleAddChapter(pArr: TChaptArr; count: integer;
  mtype: TChapterType): boolean;
begin
  result := False;
  if Assigned(FMP4TitleAddChapter) then
    result := FMP4TitleAddChapter(pArr, count, mtype);
end;

procedure TMP4InfoAPI.MP4TitleDelAllChapter(mtype: TChapterType);
begin
  MP4TitleDelAllChapter(mtype);
end;

function TMP4InfoAPI.MP4TitleDelChapter(timepos: integer;
  mtype: TChapterType): boolean;
begin
  result := False;
  if Assigned(FMP4TitleDelChapter) then
    result := FMP4TitleDelChapter(timepos, mtype);
end;

function TMP4InfoAPI.MP4TitleGetChapter(pArr: TChaptArr; count: integer;
  mtype: TChapterType): integer;
begin
  result := 0;
  if Assigned(FMP4TitleGetChapter) then
    result := FMP4TitleGetChapter(pArr, count, mtype);
end;

function TMP4InfoAPI.MP4TitleGetChapterCount(mtype: TChapterType): integer;
begin
  result := 0;
  if Assigned(FMP4TitleGetChapterCount) then
    result := FMP4TitleGetChapterCount(mtype);
end;

function TMP4InfoAPI.MP4TitleInit(filename: pchar): boolean;
begin
  result := FMP4TitleInit(filename);
end;

procedure TMP4InfoAPI.MP4TitleUninit;
begin
  FMP4TitleUninit;
end;

end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值