Delphi modyfikacja głównej formy przez DLL

0

witam,

mam taki pliczek zwykły plik DLL:

library Communication;
uses
SysUtils, Classes, Dialogs,Main,Controls,Forms,StdCtrls,Windows,Graphics;
procedure test; export;
begin
MainFormBMS.TestCapation.visible:=false;
end;
begin
end.

plik pośrednik między dll a programem glównym:

unit Dll_Managment;
interface
procedure test; stdcall external 'Communication.dll' name 'test' ;
implementation
end.

i program glówny:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs,Dll_Managment;

i tu wywołanie funkcji "test"<<

chcę za pomocą tego dll zmienić wartość jakiejś etykiety na formie głównej, ale za każdym razem wywala access volation. Muszę zrobić jakieś szczególne odwołanie do etykiety formy głównej wywoływanej w DLL?

0
  1. Co to jest MainFormBMS? Skąd dllka to ma?

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

MainFormBMS to nazwa mojej formy głównej

do programu głównego załączam moduł(w sekcji uses): Dll_Managment w którym to importuje procedurę z pliku DLL.

0
borewicz_311 napisał(a)

MainFormBMS to nazwa mojej formy głównej

Tego się domyślam, ale nadal nie pokazałeś w jaki sposób dll dostaje dostęp do tej formy.

0

dobre pytanie....jesli w sekcji uses dll wpisze sobie główny unit("Main") to nie wystarcza??

0

Oczywiście, że to nie wystarcza i właściwie nie wiem po co dodawać do uses ten moduł.
Możesz przekazywać formę jako parametr funkcji/procedury
Rzuć okiem na ten naskrobany na szybko przykład:
DLL

library Communication;

{ 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. }

uses
  SysUtils,
  Classes, Forms;

{$R *.res}

function SetVisible(frm: TForm; Value: boolean): Boolean;
begin
  result:= True;
  try
    frm.Visible:= Value;
  except
    result:= False;
  end;
end;

exports
  SetVisible;
  
begin
end.

PROGRAM:

unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btnHide: TButton;
    procedure btnHideClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

  function SetVisible(frm: TForm; Value: boolean): Boolean;
     external 'Communication.dll';

implementation

{$R *.dfm}

procedure TForm1.btnHideClick(Sender: TObject);
begin
  SetVisible(Form1, False);
  Sleep(5000);
  SetVisible(Form1, True);
end;

end.
0
borewicz_311 napisał(a)

dobre pytanie....jesli w sekcji uses dll wpisze sobie główny unit("Main") to nie wystarcza??

Nie. Forma jest zmienną globalną wewnątrz tego unitu. Ponieważ DLL i EXE to osobne „jednostki kompilacji”, w rezultacie forma w DLL jest zupełnie inną zmienną niż forma z EXE. W dodatku ta pierwsza nie jest nigdzie inicjalizowana. Kod kompiluje się, owszem, po czym zupełnie prawidłowo się wywala.

tu napisałem coś więcej, ale wycinam, bo to zły pomysł był

0

Dziękuję-to mi rozjaśniło obraz sprawy, popróbuję od tego najprostszego przykładu, bo niestety muszę zaszyć w DLL fragment kodu który to wykorzystuje zewnętrzne moduły i pliki dll.

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