Einzelnen Beitrag anzeigen

Kati2608

Registriert seit: 30. Okt 2007
3 Beiträge
 
#1

CreateWindow('Static'... aus injizierter DLL

  Alt 30. Okt 2007, 16:06
Hallo Leute,

ich habe da ein Problem bei der mir bisher noch keiner helfen konnte...

Und zwar injiziere ich eine DLL in ein anderes Programm (in diesem Beispiel zu Testzwecken in Notepad). Aus dieser DLL heraus soll dann in Notepad mittels CreateWindow ein Static Control angezeigt werden. Das funktioniert auch super... Das Problem ist nur, dass das Control sofort wieder verschwindet, es wird nur den Bruchteil einer Sekunde angezeigt.

Getestet habe ich das ganze auch mit einem Fenster welches anstatt des Static Controls angezeigt wird, aber auch da verschwindet das Fenster sofort wieder...

Wenn ich das ganze in einem normalen Programm versuche klappt es so wie gewünscht... Das Fenster bzw. Control wird so angezeigt wie es sein soll. Nur eben aus der DLL nicht.

Weiß einer Rat?

Liebe Grüße,
Kati

Code der DLL (Static):
Delphi-Quellcode:
uses
  SysUtils, Classes, Windows, Messages, ShareMem;

{$R *.res}


Procedure Init(); stdcall;
var hMain: HWND;
  WndDC: HDC;
  MyFont: HWND;
begin
  hMain := FindWindow('notepad',nil);
  if (hMain <> 0) then
  begin
    MyFont := CreateFont( -11, 0, 0, 0, 0, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, 'MS Sans Serif');
    WndDC := CreateWindow('Static', 'lbltest', WS_VISIBLE or WS_CHILD, 10, 10, 250, 14, hMain, 0, 0, nil);
    SendMessage(WndDC, WM_SETFONT, Integer(MyFont), Integer(true));
  end;
end;


Procedure EntryPoint(dwReason: Integer); stdcall;
begin
case dwReason of
    DLL_PROCESS_DETACH: {};
    DLL_PROCESS_ATTACH: Init();
    DLL_THREAD_ATTACH: {};
    DLL_THREAD_DETACH: {};
   end;
end;


begin
 DisableThreadLibraryCalls(hInstance);
 DLLProc := @EntryPoint;
 EntryPoint(DLL_PROCESS_ATTACH);
end.
Code der DLL (Fenster):
Delphi-Quellcode:
library InjectDLL1;


uses
  SysUtils, Classes, Windows, Messages, ShareMem;

{$R *.res}


function RegisterClass: Boolean;
var
  WindowClass: TWndClass;
begin
  WindowClass.Style := CS_HREDRAW or CS_VREDRAW;
  WindowClass.lpfnWndProc := @DefWindowProc;
  WindowClass.cbClsExtra := 0;
  WindowClass.cbWndExtra := 0;
  WindowClass.hInstance := hInstance;
  WindowClass.hIcon := 0;
  WindowClass.hCursor := 0;
  WindowClass.hbrBackground := COLOR_WINDOW;
  WindowClass.lpszMenuName := nil;
  WindowClass.lpszClassName := 'MyWindowClass';
  Result := Windows.RegisterClass(WindowClass) <> 0;
end;


Procedure Init(); stdcall;
var
  hMain: HWND;
  hWindow: HWND;
begin
  hMain := FindWindow('notepad',nil);
  if (hMain <> 0) then
  begin
    if not RegisterClass then
      begin
        MessageBox(0, 'RegisterClass failed...', 'Notepad', MB_OK);
        Exit;
      end;
      hWindow := CreateWindowEx(0, 'MyWindowClass', 'Do you see me?', WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInstance, nil);
      if hWindow <> 0 then
      begin
        ShowWindow(hWindow, SW_SHOWNORMAL);
        UpdateWindow(hWindow);
      end else
      begin
        MessageBox(0, 'CreateWindow failed...', 'Notepad', MB_OK);
        Exit;
      end;
  end;
end;


Procedure EntryPoint(dwReason: Integer); stdcall;
begin
case dwReason of
    DLL_PROCESS_DETACH: {};
    DLL_PROCESS_ATTACH: Init();
    DLL_THREAD_ATTACH: {};
    DLL_THREAD_DETACH: {};
   end;
end;


begin
 DisableThreadLibraryCalls(hInstance);
 DLLProc := @EntryPoint;
 EntryPoint(DLL_PROCESS_ATTACH);
end.
  Mit Zitat antworten Zitat