[Delphi] Runtime unit wisselen

Pagina: 1
Acties:

  • ArieProductions
  • Registratie: Januari 2002
  • Laatst online: 27-11-2024
Ik vroeg me af of het mogelijk is om runtime van unit te wisselen?

Wanneer je programma in meerdere windows omgevingen draait (W98, NT, W2K, etc.) is het soms handig om bepaalde dingen wel of niet te kunnen/mogen. Het is natuurlijk mogelijk om aan de hand van het gedecteerde besturingssysteem wel of niet een bepaald object aan te maken.

Maar nu leek me handiger om gewoon een hele unit te veranderen, dus een unit voor W98, NT en W2K etc.
Of is dit juist weer niet handiger en kan ik het daarom niet vinden?

"Quidquid latine dictum sit, altum videtur" (Whatever is said in Latin sounds profound)


Verwijderd

het makkelijkste lijkt het mij om een aparte class te maken voor ieder os, met een gezamelijke base-class. dan hoef je alleen maar bij de constructie van het object te kijken welk os het is, en de goeie class te instantieren.

  • whoami
  • Registratie: December 2000
  • Laatst online: 11:59
Hmmm... of het mogelijk is, weet ik nog zo niet.

Wat je wel kunt doen, is een exe maken voor W98, een voor W2K, een voor NT, ... door gebruik te maken van conditional defines.
Als je compileert voor W2K definieer je de conditional W2K bijvoorbeeld en in uw programma ga je aangeven welke delen hij moet compileren als die conditional gedefinieerd is.

https://fgheysels.github.io/


  • ArieProductions
  • Registratie: Januari 2002
  • Laatst online: 27-11-2024
Op woensdag 12 juni 2002 18:04 schreef deur het volgende:
het makkelijkste lijkt het mij om een aparte class te maken voor ieder os, met een gezamelijke base-class. dan hoef je alleen maar bij de constructie van het object te kijken welk os het is, en de goeie class te instantieren.
Lijkt me ook ja,

Maar ik zat te denken of ik niet een keer bijvoorbeeld een hele data unit kon veranderen. Ene vol met BDE objectes en andere gemaakt met ADO.

"Quidquid latine dictum sit, altum videtur" (Whatever is said in Latin sounds profound)


  • Tomatoman
  • Registratie: November 2000
  • Laatst online: 13:08

Tomatoman

Fulltime prutser

Een unit vormt een vast onderdeel van een executable en kun je niet los daarvan zien. Sommige onderdelen van de unit worden gelinkt in het datasegment, andere in het codesegment en weer andere als resource. Het is onmogelijk om een unit runtime te veranderen.

Afgezien daarvan, waarom zou je dat willen? Daarvoor zijn DLL's en runtime packages uitgevonden. Je zou bijvoorbeeld drie versies van je DLL kunnen meeleveren, waarbij de applicatie runtime de juiste linkt.

Een goede grap mag vrienden kosten.


  • ArieProductions
  • Registratie: Januari 2002
  • Laatst online: 27-11-2024
Op woensdag 12 juni 2002 18:12 schreef tomatoman het volgende:

Afgezien daarvan, waarom zou je dat willen? Daarvoor zijn DLL's en runtime packages uitgevonden. Je zou bijvoorbeeld drie versies van je DLL kunnen meeleveren, waarbij de applicatie runtime de juiste linkt.
aha, ik ga eens even uitzoeken hoe je DLL's runtime kunt switchen.

"Quidquid latine dictum sit, altum videtur" (Whatever is said in Latin sounds profound)


  • Tomatoman
  • Registratie: November 2000
  • Laatst online: 13:08

Tomatoman

Fulltime prutser

Is al vaak langsgekomen, maar ik zal je op weg helpen. Het voorbeeld is gebaseerd op 'The Tomes of Delphi 3: Win32 Core API' van Larry Diehl.

Code voor DLL, bestaande uit twee units (Example en DLLAboutForm):
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
library Example;

uses
  SysUtils,
  Classes,
  Windows,
  Dialogs,
  DLLAboutForm in 'DLLAboutForm.pas' {AboutBox};

{the exported functions}
exports
  ShowAboutBox name 'ShowAboutBox';

{the DLL entry point procedure.  this procedure will fire every time a
 process or thread attaches to the DLL.  if a process has attached to a DLL,
 any newly created threads will automatically attach themselves}
procedure DLLMain(AttachFlag: DWORD);

 begin
  {display attachement type}
  case AttachFlag of
    DLL_PROCESS_ATTACH: MessageBox(0, 'Process: Attaching', 'Alert', MB_OK);
    DLL_PROCESS_DETACH: MessageBox(0, 'Process: Detaching', 'Alert', MB_OK);
    DLL_THREAD_ATTACH:  MessageBox(0, 'Thread: Attaching' , 'Alert', MB_OK);
    DLL_THREAD_DETACH:  MessageBox(0, 'Thread: Detaching' , 'Alert', MB_OK);
  end;
end;

begin
  {initialize the DLL entry function}

  DLLProc := @DLLMain;

  {call the entry function upon DLL initialization}
  DLLMain(DLL_PROCESS_ATTACH);
end.

Gedeeltelijke code in DLLAboutForm, waar een standaard form file bij hoort:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
unit DLLAboutForm;

[verwijderd: onder meer de declaratie van TAbout]

{the prototype for the exported function}
  function ShowAboutBox(ExampleName, Comments: ShortString): Boolean; export;

var
  AboutBox: TAboutBox;

implementation

{$R *.DFM}

function ShowAboutBox(ExampleName, Comments: ShortString): Boolean;

begin
  {initialize the function results}
  Result := FALSE;

  {create the about box form}
  AboutBox := TAboutBox.Create(Application);

  {initialize labels from the supplied strings}
  AboutBox.ProductName.Caption := ExampleName;
  AboutBox.Comments.Caption := Comments;

  {show a modal about box}
  AboutBox.ShowModal;

  {release the form}
  AboutBox.Release;

  {indicate that the function completed}

  Result := TRUE;
end;

Code om de dynamic link library te laden:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
procedure TForm1.Button1Click(Sender: TObject);
var
   hMod: THandle;                 // holds the DLL handle
   ModuleFileName: array[0..255] of char;  // holds the DLL name

   {this is the prototype for the function imported from the DLL}
   MyFunction: function(ExampleName, Comments: ShortString): Boolean;

begin
   {explicitly load the DLL}
   hMod := LoadLibrary('EXAMPLE.DLL');
   if (hMod=0) then Exit;

   {retrieve the address of the desired function}
   @MyFunction := GetProcAddress(hMod, 'ShowAboutBox' );

   {if the address was returned...}
   if (@MyFunction<>nil) then
   begin
     {call the function to display an about box}
     MyFunction('LoadLibrary Example','This example demonstrates loading '+

            'a dynamic link library via the LoadLibrary function.');

     {retrieve the module filename}
     GetModuleFileName(GetModuleHandle('EXAMPLE.DLL'), @ModuleFileName[0],
                 SizeOf(ModuleFileName));

     {display the DLLs name}
     ShowMessage('The loaded DLL was: '+ModuleFileName);
   end
   else
     {indicate an error}
     ShowMessage('GetProcAddress Failed');


   {free the DLL}
   FreeLibrary(hMod);
end;

Een goede grap mag vrienden kosten.


  • ArieProductions
  • Registratie: Januari 2002
  • Laatst online: 27-11-2024
Tnx, scheelt weer zoekwerk, was ik nog niet aan toegekomen :P

"Quidquid latine dictum sit, altum videtur" (Whatever is said in Latin sounds profound)

Pagina: 1