Ze względu na dość duży plik końcowy mojego programu postanowiłem część funkcji przenieść do biblioteki DLL i wykorzystywać je jako wtyczki do programu. Jako że mam bardzo małe doświadczenie w tym temacie (nigdy mi to nie było potrzebne) poniżej biblioteka DLL do obsługi jednej z firm kurierskich. Proszę o wypowiedź czy waszym zdaniem nie robię jakiegoś błędu.
Zastanawiałem się także aby rekordy pozamieniać na klasy oraz wszystkie stringi pozamieniać na PWideChar / PChar
library dpd;
(DPD_Type)
Type
TReceiver = Packed Record
Address: ShortString;
City: ShortString;
Company: ShortString;
Name: ShortString;
CountryCode: ShortString;
Phone: ShortString;
Code: ShortString;
End;
(....)
uses
DPD_Info in 'DPD_Info.pas',
DPD_Proc in 'DPD_Proc.pas',
DPD_Type, Windows;
const
Version = '18.11.2015';
var
DS: TDPDWebService;
{$R *.res}
// ----------------------------------------------------------
Function DLLType: ShortString; stdcall;
begin
Result := 'COURIER';
end;
Function DLLInfo: ShortString; stdcall;
begin
Result := 'DPD';
end;
Function DLLVersion: ShortString; stdcall;
begin
Result := Version;
end;
Procedure CloseDll(Reason: Integer);
begin
if Reason = DLL_PROCESS_DETACH then
DS.Free;
end;
// ----------------------------------------------------------
// DPDInfo
Function CodeToDescription(Code: PWideChar): ShortString; stdcall;
var
DI: TDPDInfoWebService;
begin
DI := TDPDInfoWebService.Create;
Try
Result := DI.CodeToDescription(Code);
Finally
DI.Free;
End;
end;
Function GetStatusWaybill(Login, Password, WayBill: PWideChar): ShortString; stdcall;
var
DI: TDPDInfoWebService;
begin
DI := TDPDInfoWebService.Create;
Try
DI.Login := Login;
DI.Password := Password;
Result := DI.GetStatusWaybill(WayBill);
Finally
DI.Free;
End;
end;
Function GetCodeWaybill(Login, Password, WayBill: PWideChar): ShortString; stdcall;
var
DI: TDPDInfoWebService;
begin
DI := TDPDInfoWebService.Create;
Try
DI.Login := Login;
DI.Password := Password;
Result := DI.GetCodeWaybill(WayBill);
Finally
DI.Free;
End;
end;
// DPD Service
Procedure Authorization(Login, Password, FId: PWideChar); stdcall;
Begin
DS.Login := Login;
DS.Password := Password;
DS.FId := FId;
End;
Procedure GenerateProtocol(ArrayPackageId: TArrayPackageID; FileName: PWideChar); stdcall;
Begin
DS.GenerateProtocol(ArrayPackageId, FileName);
End;
procedure GenerateLabel(PackageId: Int64; FileName: String); stdcall;
begin
DS.GenerateLabel(PackageId, FileName);
end;
function GetWayBill(Nr: Integer): ShortString; stdcall;
begin
if Nr > High(DS.WayBill) then
Exit('')
else
Exit(DS.WayBill[Nr])
end;
function CountWayBill: Integer; stdcall;
begin
Result := High(DS.WayBill);
end;
Procedure NewPackage(const Sender: TSender;const Receiver: TReceiver;const PackInfo: TPackageInfo;const PackData: TArrayPackage); stdcall;
begin
DS.NewPackage(Sender, Receiver, PackInfo, PackData);
end;
exports
DLLType,
DLLInfo,
DLLVersion,
CodeToDescription,
GetStatusWaybill,
GetCodeWaybill,
Authorization,
GetWayBill,
CountWayBill,
GenerateProtocol,
GenerateLabel,
NewPackage;
begin
DS := TDPDWebService.Create;
DLLProc := @CloseDll;
end.