Autor Beitrag
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Do 09.09.04 20:54 
bin zufällig auf assabads tut gestoßen was er in
www.delphi-forum.de/viewtopic.php?t=101
gepostet hat
in dem tut (als PDF) hatte er geschreiben das er nen prob hatte aus ner EXE gleichzeitgi eine dll zu machen und die dann selber zu laden

hab hier mal das prob gelöst

ausblenden volle Höhe Delphi-Quelltext
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:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
program test;

uses windows, sysutils;

procedure haha;    // normaler export
begin
  messagebox(0,'normaler Export aufgerufen',nil,0);
end;

var dllhandle: integer; // dll handle, wenn die exe als dll geladen wurde
procedure dllloadproc; stdcall// was soll passieren wenn dll geladen wird?
begin
  messagebox(0,pchar('Dll geladen bei: '+inttohex(dllhandle,8)),nil,0);
end;

procedure dllunloadproc; stdcall// was soll passieren wenn dll entladen wird
begin
  messagebox(0,pchar('Dll entladen'),nil,0);
end;

procedure DllLoad; stdcall// weiß net die instrucion die alle register aufn stack legt :/
asm
  pusha
  call dllLoadproc
  popa
end;

procedure DllUnload; stdcall;
asm
  pusha
  call dllUnLoadproc
  popa
end;

procedure init;        // diese prozedur wird aufgerufen wenn die dll geladen / entladen wird
asm
  push eax
  mov eax, ebx
  shr eax, 16
  shl eax, 16
  mov [dllhandle], eax
  mov eax, esp
  shl eax, 24
  cmp eax, $a8000000
  pop eax
  jne @_unload
  call dllload
  jmp @_exit
  @_unload:
  call dllunload
  @_exit:
end;

exports
  haha;

procedure SaveMeAsDllAndLoadMeAndExecuteExport; // exe als dll speichern
var IDH: PImageDosHeader;
    INH: PImageNtHeaders;
    h: integer;
    filesize, read, dllh: cardinal;
    filemem: pointer;
    hahaproc: procedure;
begin
  copyfile(pchar(paramstr(0)),'measdll.dll',false);
  h := CreateFileA('measdll.dll',GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
  if h <> -1 then
  begin
    filesize := getfilesize(h,nil);
    getmem(filemem,filesize);
    readfile(h,filemem^,filesize,read,nil);

    IDH := filemem;
    if IDH^.e_magic = IMAGE_DOS_SIGNATURE then
    begin
      INH := pointer(cardinal(filemem)+cardinal(IDH^._lfanew));
      if INH^.Signature = IMAGE_NT_SIGNATURE then
      begin
        INH^.FileHeader.Characteristics :=
           INH^.FileHeader.Characteristics + IMAGE_FILE_DLL;
        INH^.OptionalHeader.AddressOfEntryPoint := cardinal(@init)-getmodulehandle(nil);
        setfilepointer(h,0,nil,FILE_BEGIN);
        writefile(h,filemem^,filesize,read,nil);
      end;
    end;
    closehandle(h);
  end;
  dllh := loadlibrary('measdll.dll');
  @hahaproc := getprocaddress(dllh,'haha');
  hahaproc;
  freelibrary(dllh);
end;

begin
  messagebox(0,'bin eine exe',nil,0);
  SaveMeAsDllAndLoadMeAndExecuteExport;
end.



Moderiert von user profile icontommie-lie: Topic aus Windows API verschoben am Do 09.09.2004 um 21:23
Moderiert von user profile iconTino: Topic aus Neue Einträge / Hinweise / etc. verschoben am So 14.11.2004 um 18:10


Zuletzt bearbeitet von uall@ogc am Do 09.09.04 21:57, insgesamt 1-mal bearbeitet
raziel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2453

Arch Linux
JS (WebStorm), C#, C++/CLI, C++ (VS2013)
BeitragVerfasst: Do 09.09.04 21:12 
ausblenden Delphi-Quelltext
1:
2:
pusha  // alles sichern
popa   // alles wiederherstellen (sind die flags mit dabei, aber die 32 Bit sin da a scho wurscht

Und sowas gehört imho immer noch in die OpenSource-Units oder zumindest in die FAQ's Sparte

raziel

_________________
JSXGraph
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Do 09.09.04 21:26 
raziel hat folgendes geschrieben:
in die FAQ's Sparte
Stimmt.

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
uall@ogc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Do 09.09.04 22:01 
O_o werde mich demnächst dran halten
Gast
Gast
Erhaltene Danke: 1



BeitragVerfasst: So 26.09.04 13:28 
Aber um das Kopieren der EXE kommst du nicht herum, oder?
uall@ogc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: So 26.09.04 13:39 
leider nicht, aus folgendem grund:

ist bei charakteristik der flag gesetzt, dass die exe eine dll ist, kann man die exe nicht mehr ausführen, "blabla.exe ist keine gültige Win32 Anwendung", setzt man den flag nicht kann die exe nicht als dll geladen werden...
Gast
Gast
Erhaltene Danke: 1



BeitragVerfasst: So 26.09.04 14:02 
Hmm ... man müßte quasi nen DllMain()-Hook haben, wo man vor Ausführung des Images (also nach dem Laden) die Flags anpassen könnte ... das wäre cool. Dann könnte man eine EXE für Hookprogramm und Hook-DLL haben.
uall@ogc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: So 26.09.04 14:24 
das geht nicht, wegen windows...

LoadLibrary('meineexe.exe') funktioniert schon nciht, es wird nie versucht überhaupt dllmain aufzurufen

wenn man bei ner exe dll flag setzt, dann die exe startet, kommt die fehlermeldung das es keine gülige win32 anwendung ist, das passiert alels im ring 0
d.h. windoof versucht erst gar nicht dllmain bzw, den entrypoint der exe aufzurufen da windows erst den NT header "scannt" und sieht das der dllflag gesetzt ist und somit agr nicht versucht die exe auszuführen

genauso bei laodlibrary, ist dort halt dllflag nicht gesetzt, denkt windows es ist keine dll und lädt sie einfach nicht in den speicher... bis zum aufruf von dllmain kommt es erst gar nicht

deshalb muss man halt 2 dateien haben, einmal die exe mit dem dllflag nicht gesetzt und einmal die dll mit dem dllflag gesetzt