CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2005
    Posts
    7

    GDI Calls (and/or) API Hooks - Help!

    Hi,

    I am new to C++ programming, and have only just found the need to learn deeper due to a program I have come up against. I have been lead to believe that it is possible to "overlay" text within applications using API hooks. The situation I am in, I would like to create two simple counters that will be displayed in the bottom right hand corner of the game screen. This is what the counters would do:

    Counter One:
    Count down from a number set by the user outwith the game to zero upon the keypress of F12. (For example, I would set the number to 500, enter the game, and for every time F12 is pressed, this number will decrease)

    Counter Two:
    Count down from 60:00 (60 minutes) to 00:00 (0 minutes) from the time the user carries out a specific key sequence (e.g. Ctrl+F12).

    As I am very new to C++ programming, please be gentle :P Any help will be greatly appreciated to help me on my way to understand C++ better.

  2. #2
    Join Date
    Dec 2005
    Posts
    7

    Re: GDI Calls (and/or) API Hooks - Help!

    Here is some information that I was told to read to try understand it better, and I believe this is what I am needing to do (although it is text I need to display, not exactly graphics):

    Quote Originally Posted by Fraps 1.9D Documentation


    I’m trying to draw on the screen using GDI calls. I can see my text but it flickers badly. How can I fix this?

    This is a common problem that people encounter. It is quite easy to retrieve a handle to the active window and obtain a device context for the screen. You can then issue standard GDI calls and expect them to be drawn accordingly on the screen.

    Unfortunately the flickering problem can’t be avoided. The issue is caused by the fact that most games are double-buffered and GDI only supports single-buffered devices. Whenever you issue GDI calls they are always affecting the same buffer. If the game is double buffered you will see your text half the time, if it’s triple buffered only a third. As far as I know there is no way to obtain a unique device context for the back buffers.


    How can I draw my own graphics on the screen?

    Unfortunately there is no simple way to draw graphics on screen. This part is therefore fairly technical. If you don’t understand some of the terms I’d recommend doing a search for the keywords on google.

    Fraps works by patching directly into the underlying graphics API. Doing things this way allows Fraps to issue graphics commands as if it were the game itself, avoiding having to do a large amount of setup beforehand. There are two techniques that can be used to hook into the API.

    The first method is referred to as “API hooking”. Basically you want to obtain the address of the graphics function in memory, and patch this such that your code will get called whenever the game tries to use this function.

    The other method is to write a “DLL wrapper”. Here you want to write your own DLL that exports the same functions as the graphics DLL. The idea is to rename the original DLL and put your DLL in its place. Then when your DLL is loaded, you proceed to load the original DLL and pass any function that is called through to it. Creating a DLL wrapper for DirectX is reasonably easy because there are only a few functions exported from the DLL. OpenGL however exports ALL of its API calls, which means that it can be very time consuming to build the stubs for all of the functions. If you want to pursue this method it may be easier to download and modify an existing OpenGL wrapper such as GLtrace.

    When you have successfully hooked into the graphics API it is simply a matter of saving and restoring the state before and after you have issued your graphics commands. To learn how to draw graphics you should consult the DirectX and OpenGL SDK’s.

    Other ideas that you might want to pursue if you want something up and running quickly:

    If it’s only for one game, search through the game code for a function that displays text on screen. Most games have to output text at some stage, so utilising their routine may save you the hassle of implementing your own.

    You could also try forcing the game to run in windowed mode. As an example, you could hook the CreateDevice function in DirectX8 and ensure that the Windowed flag is always set TRUE. With the game running in a window, you can draw in your own application alongside.

    I have heard (but not verified) that it is possible to use DirectX overlays to draw on top of the game screen. This may be an easy option if performance is not an issue. Overlay documentation can be found in the DirectX SDK.

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: GDI Calls (and/or) API Hooks - Help!

    We need more information to help you.

    Have you designed the game yourself or do you have the source of it?
    What library is used by the game? Opengl? GDI? DirectX?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Dec 2005
    Posts
    7

    Re: GDI Calls (and/or) API Hooks - Help!

    No, I did not design the game myself, it is a game I am currently involved with and do not have access to the source.

    I'm believe that the library used is DirectX, but I will need to confirm this when I get to my home system (although I am almost certain it is).

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: GDI Calls (and/or) API Hooks - Help!

    In that case, I can't help you any further, perhaps someone else can?

    However, maybe you can take a look at the FullScreenDialog sample that comes with the DX SDK, at least it did with DX8,(samples\Multimedia\DirectDraw\FullScreenDialog) but I don't know if it would help.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  6. #6
    Join Date
    Dec 2005
    Posts
    7

    Re: GDI Calls (and/or) API Hooks - Help!

    Thanks anyway

    For others that read this thread: An example of the kind of thing I'm looking for can be seen if you run Fraps (a screen-capture and framerate display tool for games). Fraps allows you to choose a corner of the screen to display the framerate, and it overlays this over the game screen. This is the same idea that I require, but with the specification that I described in the first post.

  7. #7
    Join Date
    Dec 2005
    Posts
    7

    Re: GDI Calls (and/or) API Hooks - Help!

    Bump

    Does anyone know what I'm looking for?

  8. #8
    Join Date
    Dec 2005
    Posts
    7

    Re: GDI Calls (and/or) API Hooks - Help!

    Nevermind, got my questions answered on another forum.

  9. #9
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    389

    Re: GDI Calls (and/or) API Hooks - Help!

    Well, post it here!

  10. #10
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: GDI Calls (and/or) API Hooks - Help!

    Yes, you might post the answer here to, because I know that this question comes up from time to time and I'm curious myself.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  11. #11
    Join Date
    Dec 2005
    Posts
    7

    Re: GDI Calls (and/or) API Hooks - Help!

    I'll write up an example in the next couple of days, as well as a link to the original article.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured