2010. május 30., vasárnap

TScreen, TApplication used in a DLL


Problem/Question/Abstract:

TScreen, TApplication used in a DLL

Answer:

Each DLL in Delphi maintains its own instance of Application & Screen, your DLL-calling application should send the its own Application and Screen values to the DLL. The DLL should save and restore its original values.

You should put this code somewhere in your DLL and call the Init() function from your application:


const
  SavedApplication: TApplication = nil;
  SavedScreen: TScreen = nil;

  // export this procedure and call it after loading the DLL

procedure Init(anApplicationHandle, aScreenHandle: LongWord);
begin
  if not Assigned(SavedApplication) then
  begin
    SavedApplication := Application;
    Application := TApplication(anApplicationHandle);
  end;

  if not Assigned(SavedScreen) then
  begin
    // ....same...
  end;
end;

initialization

finalization
  if Assigned(SavedApplication) then
  begin
    Application := SavedApplication;
  end;

  if Assigned(SavedScreen) then
  begin
    // ....same.....
  end;
end.

Nincsenek megjegyzések:

Megjegyzés küldése