Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   C# In fremdem OpenGL Grafikkontext zeichnen (https://www.delphipraxis.net/119064-fremdem-opengl-grafikkontext-zeichnen.html)

idontwantaname 20. Aug 2008 13:06


In fremdem OpenGL Grafikkontext zeichnen
 
Hallo!

Auf meinem Computer läuft eine OpenGL Anwendung und rendert ihre Welt. Ich möchte nun auf diesen Grafikkontext zugreifen und selbst etwas "dazurendern" bzw. zweidimensionale Grafiken und Texte anzeigen lassen.
Falls ihr das Programm Fraps kennt, dann wisst ihr, dass es die Framerate direkt im Renderingfenster anzeigen lassen kann. Wenn das möglich ist, dann sollte auch mein Vorhaben realisierbar sein.
Und hier kommt ihr ins Spiel, denn ich habe leider keine Ahnung, wie ich soetwas bewerkstelligen kann ;) Habt ihr nützliche Infos oder Links für mich, um mich mit dem Thema vertraut zu machen?

Lg oli

PS: ich programmiere zwar C#, kann aber auch native Delphiprogramme schreiben ;)

gekkorist 20. Aug 2008 14:01

Re: In fremdem OpenGL Grafikkontext zeichnen
 
auf externe anwendungen schreiben ist sone sachen, das kann man meist nur mit OGL Hooks bewerkstelligen, und das ist alles ne komplizierte Materie.

Kannst es auch per GDI machen, aber das flackern ist ein nateil der sehr nervt.
Durchsuche mal das forum hier, da wurde das schon öfters mal besprochen.

idontwantaname 31. Aug 2008 10:44

Re: In fremdem OpenGL Grafikkontext zeichnen
 
Hi!

Ich habe mir jetzt OpenGL Hooks angeschaut und es funktioniert auch so halbwegs. Also ich kann schon in der fremden Anwendung etwas zeichnen, nur leider ist es nicht immer vollständig da und flackert auch - leider weiß ich nicht wieso.

Hier mein Code (ich verwende die uallCollection für den API Hook):

Hook-Installation und -Entfernung in meiner Anwendung:
Delphi-Quellcode:
procedure TControlForm.ButtonInstallHookClick(Sender: TObject);
var
  PID: Integer;
begin
  if TryStrToInt(EditProcessID.Text, PID) then
  begin
    if InjectLibrary(PID, PChar(DllFileName)) then
      MessageBox(Handle, 'Hook installation was successful!', 'Information', MB_OK or MB_ICONINFORMATION)
    else
      MessageBox(Handle, 'Hook installation was not successful!', 'Error', MB_OK or MB_ICONERROR);
  end
  else
    MessageBox(Handle, 'The entered process id is not a valid number!', 'Error', MB_OK or MB_ICONERROR);
end;

procedure TControlForm.ButtonRemoveHookClick(Sender: TObject);
var
  PID: Integer;
begin
  if TryStrToInt(EditProcessID.Text, PID) then
  begin
    if UnloadLibrary(PID, PChar(DllFileName)) then
      MessageBox(Handle, 'Hook uninstallation was successful!', 'Information', MB_OK or MB_ICONINFORMATION)
    else
      MessageBox(Handle, 'Hook uninstallation was not successful!', 'Error', MB_OK or MB_ICONERROR);
  end
  else
    MessageBox(Handle, 'The entered process id is not a valid number!', 'Error', MB_OK or MB_ICONERROR);
end;
Die einzige Unit der injizierten DLL:
Delphi-Quellcode:
unit Overlay;

interface

uses
  Windows, SysUtils, uallHook, Graphics;

type
  TSwapBuffersFunction = function(DC: HDC) : BOOL; stdcall;

implementation

var
  NewSwapBuffersFunction: TSwapBuffersFunction;

function MySwapBuffersFunction(DC: HDC): BOOL; stdcall;
var
  Canvas: TCanvas;
  Rect: TRect;
begin
  Rect.Left := 0;
  Rect.Right := 100;
  Rect.Top := 0;
  Rect.Bottom := 100;

  Canvas := TCanvas.Create;
  Canvas.Handle := DC;
  Canvas.Brush.Color := clRed;
  Canvas.FillRect(Rect);
  Canvas.Free;

  Result := NewSwapBuffersFunction(DC);
end;

procedure InstallHook;
var
  module: HMODULE;
  SwapBuffersFunction: Pointer;
begin
  module := GetModuleHandle(gdi32);
  if module <> INVALID_HANDLE_VALUE then
  begin
     SwapBuffersFunction := GetProcAddress(module, 'SwapBuffers');
     if SwapBuffersFunction <> nil then
     begin
       if not HookCode(SwapBuffersFunction, @MySwapBuffersFunction, @NewSwapBuffersFunction) then
         MessageBox(0, 'Hooking code has failed.', 'Error', MB_OK or MB_ICONERROR);
     end
     else
       MessageBox(0, 'Invalid function address!', 'Error', MB_OK or MB_ICONERROR);
  end
  else
    MessageBox(0, PChar('Invalid module handle!', 'Error', MB_OK or MB_ICONERROR);
end;

procedure RemoveHook;
begin
  if not UnhookCode(@NewSwapBuffersFunction) then
    MessageBox(0, 'Unhooking code has failed', 'Error', MB_OK or MB_ICONERROR);
end;

initialization

InstallHook;

finalization

RemoveHook;

end.
Habt ihr Ideen, woran es scheitert?

Lg oli

idontwantaname 1. Sep 2008 14:36

Re: In fremdem OpenGL Grafikkontext zeichnen
 
Hi!

Ich erlaube mir, hier einmal zu pushen :duck:
Brauche das ganze leider recht bald :?

Lg oli

littleDave 1. Sep 2008 15:00

Re: In fremdem OpenGL Grafikkontext zeichnen
 
Schau dir mal den Link im ersten Post an, vielleicht hilft dir der ja.


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:01 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz