funkcja w dll

0

mam pytanie jak skorzystać z funkcji z dll?

zrobiłem sobie taki teścik:

...

type
  my = function (s : integer) : string;

...

procedure TForm1.Button1Click(Sender: TObject);
var
  DLL : THandle;
  test : my;
begin
  DLL := LoadLibrary('win.dll');
  try
    @test := GetProcAddress(DLL, 'Podwojenie');
    if @test=nil then raise Exception.Create('Bład - nie mogę znaleźć funkcji w bibliotece!');
    ShowMessage(test(4));
  finally
    FreeLibrary(DLL); 
  end;
end;

end.

i kod biblioteczki

library Project1;

uses Windows,SysUtils;

function Podwojenie(var a : integer) : string; stdcall;
begin
  a := a * 2;
  Result := IntToStr(a);
end;

exports
  Podwojenie name 'Podwojenie';

begin
end.

i wywaliło mi bład "Access violation at address ..."

wie ktoś jak powino być poprawnie?

0

po utworzeniu nowego projektu dla dll, borland pisze

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }
0

Po pierwsze jak tworzysz nową bibliotekę DLL, to na górze masz taki wielki komentarz:

{ .. }

Dopisane: z powyższym się spóźniłem :)

Po drugie zdefiniowany, w type wzorzec funkcji ma przekazywanie zmiennej s przez wartość, a w DLL przekazujesz a przez referencję. Funkcji z DLL nie mógłbyś przekazać stałej 4 jako parametr, a dzięki złemu wzorcowi, właśnie to robisz.

Wzorzec też nie jest wywoływany w stdcall.

0

no dobrze to czy ktoś może mi podać przykład funkcji w dll i jej wywołania?

PS. nie znam za dobrze delphi, i się troszeczkę sugerowałem php

0

DLL:

library win;

uses
  ShareMem,
  Windows,
  SysUtils;

function Podwojenie(a : integer) : string; stdcall;
var i:integer;
begin
  i := a * 2;
  Result := IntToStr(i);
end;

exports
  Podwojenie name 'Podwojenie';

begin
end.

Fragment programu:

uses ShareMem {, ...};

{...}

type
  my = function (s : integer) : string; stdcall;

procedure TForm1.Button1Click(Sender: TObject);
var
  DLL : THandle;
  test : my;
begin
  DLL := LoadLibrary('win.dll');
  try
    @test := GetProcAddress(DLL, 'Podwojenie');
    if (@test=nil) then
      raise Exception.Create('Bład - nie mogę znaleźć funkcji w bibliotece!');
    ShowMessage(test(4));
  finally
    FreeLibrary(DLL);
  end
end;
0

no wszystko jest ok :) wielkie dzięki ale został mi jeszcze jeden problem do rozwiązania. Czemu przy zamknięciu aplikacji wyskakuje komunikat o wyjątku EInvalidPointer? coś nie tak ze zwalnianiem pamięci? skorzystałem z tego

1 użytkowników online, w tym zalogowanych: 0, gości: 1