Shagrouni

فيجوال بيسك و دلفي معا، انشاء مكتبة DLL

خالد الشقروني, 2 نوفمبر 2001

انكليزي

أوراق دلفي
 

 

  هذا مثال عن كيفية صنع ملف DLL ليتم مخاطبته من بيئات برمجية أخرى مثل فيجوال بيسك.

مكتبة DLL في هذا المثال تستخدم TStringList، إحدى المكونات القوية في مكتبة دلفي،  حيث يتم استخدام بعض خصائص و وظائف هذا المكوّن لمناولة وتخزين القوائم النصّية.

أدناه تجد سردا للتوليف المصدري لهذه المكتبة ، والتي بالإمكان استنزال ملفّها رفق النسخة المجمّعة مع أمثلة مكتوبة بفيجوال بيسك و دلفي حول كيفية مناداة وظائف هذه المكتبة.

 

library TextDb;
{Example of a DLL}
{Always UnloadFile to free the memory}
uses
  SysUtils, Classes;

var
  List: TStringList;   {Global variable}

function LoadFile(lpString: PChar; Sorted: Cardinal): integer; stdcall;
begin
  try
    if List <> nil then List.Free; {Destroy any Previous instance}
    List := TstringList.create;    {Creating an instance to List}
    if Sorted > 0 then
    begin
      List.Sorted := true;
      List.Duplicates := dupAccept; {Allow duplicate values}
    end;
    if FileExists(lpString) then
      List.LoadFromFile(lpString);  {Load the file lines into our List}
    result := List.Count;           {Number of lines in the List}
  except                            {If any thing goes wrong..}
    List.Free;                      {Destroy the creature}
    List := nil;
    result := 0;
  end;
end;

function UnloadFile: integer; stdcall;
begin
  try
    if List <> nil then List.Free;
    List := nil;
    result := 1;
  except
    result := 0;
  end;
end;

function GetLine(lpString: PChar; nMaxCount, LineNum: Cardinal): integer; stdcall;
var
  s: string;
begin
  result := -1;
  if List <> nil then
    if Integer(LineNum) < List.Count then
    begin
      s := List[LineNum];
      StrLCopy(lpString, PChar(s), nMaxCount); {Convert Pascal string to PChar}
      result := Length(S);                     {Len(s)}
    end;
end;

function AddLine(lpString: PChar): integer; stdcall;
begin
  result := -1;
  if List <> nil then
  begin
    result := List.Add (lpString);         {return the position of the line}
  end;
end;

function SaveFile(lpString: PChar): integer; stdcall;
begin
  try
    if (List <> nil) and (Trim(lpString) <> '') then
      List.SaveToFile(lpString);           {Save lines into file}
    result := List.Count;                  {Number of lines in the List}
  except
    result := 0;
  end;
end;

function DeleteLine(LineNum: Cardinal): integer; stdcall;
begin
  result := -1;
  if (List <> nil) and (Integer(LineNum) < (List.Count)) then
  begin
    List.Delete(LineNum);
    result := List.Count;
  end;
end;

function UpdateLine(lpString: PChar; LineNum: Cardinal): integer; stdcall;
begin
  result := -1;
  if (List <> nil) and (Integer(LineNum) < (List.Count)) then
  begin
    List[LineNum] := lpString;
    result := LineNum;
  end;

end;

function LineCount: integer; stdcall;
begin
  result := -1;
  if List <> nil then
    result := List.Count;
end;

function GetText(lpString: PChar; nMaxCount: Cardinal): integer; stdcall;
var
  s: string;
begin
  result := -1;
  if List <> nil then
  begin
    s := List.Text;
    StrLCopy(lpString, PChar(s), nMaxCount); {Convert Pascal string to PChar}
    result := Length(S);                     {Len(s)}
  end;
end;

exports
  LoadFile, UnloadFile, GetLine, AddLine, DeleteLine, UpdateLine, SaveFile,
  LineCount, GetText;

begin
end.




Generated by PasToWeb, a tool by Marco Cantù.

Copyright 1997 ...

Shagrouni 2001 Khaled Shagrouni khaled@shagrouni.com