how to use gdb to debug a GUI program - c++

I know how to debug a console program, but what if the program has a GUI?
For example, there is a Calc icon, when I push it down, it will call some methods.
I want to know what methods it will call. I have the source codes but have no idea about which file corresponds to what, and want to back-track what codes it will implement if I click on the icon.

There conceptually isn't any difference between debugging a GUI application and a console application - they both come down procedural programming - some action calls a particular function. There's no difference whether the trigger of the action is you pressing a button on the GUI, or typing in a command in the console. You can use a debugger for both of them (visual or command line).
If you have the source, and know what the triggering mechanism is, it should be easy enough to follow along in the source code to see which functions are called by the trigger (eg. not at runtime). You have the question tagged as Qt, which uses the connect function to connect 'signals' (events) to either other signals or 'slots'. This is essentially associates a callback function, with a particular event, so searching your source for that would be a good starting place.

Related

c++ event loop in sub application in a dll

Good day,
I have the following issue:
I have one exe application that writes text files to the disk, and that exe source is unavailable.
Customer has asked that, when users press numpad 5, a new window pops up, and does some operations with some files.
Problem is, numpad 5 + new application popup MUST only work when the application is running and has focus (they use numpad 5 for other operations).
I thought about this
create a dll with a form and buttons that do the required actions
inject the dll in the process
But I'm struggling to understand if there is a way to create a "keypress loop" in the dll.
Please note that I'm a beginner in c++ and forms, but I just need a feasibility check and a direction.
Is it possible to create a window application that waits for a keypress in an injected dll?
Is it there any simple example of this? Using google like a madman I was unable to find references to this so I think I have a problem with the proper terms.
My main issue is that the dllmain is obviously a one shot routine, and I don't understand how to create an "event loop".
Thanks for any information provided.
If you have successfully managed to inject your DLL in the traget process, use the _beginthread API in DllMain to start a new thread, and in that thread you can create a Dialog Box and have a message loop in the usal way.

Add console to existing MFC application

I'm working with 2 friends in a class project to make a D&D game. so far for the assignments I've been doing character creation stuff and strutting on the command line.
Now we're bringing or part together and I need to output ny dice rolls on a console and a few things on another one that will have to become the main view or tab or whatever it's called when it requires input/attention.
Problem is I never learned MFC yet because I didn't need it. How hard would it be to make a sample MFC console all that I can give to the teammate in charge of the GUI?
Could anyone point me to some instruction on making a console for an MFC app and how to give it output and receive output?
First, you can't. for both Unix/Linux and Windows, there is a one console/process limit. If you want another console, you have to create another process, that writes and reads the other console, while you send and receive the data.
You can use a NamedPipe http://msdn.microsoft.com/en-us/library/windows/desktop/aa365590%28v=vs.85%29.aspx to send data between processes, and the CreateProcess() function lets you create a process with a separate Console window.
Alternatively you can just write a Console-Look-a-Like window in some GUI.

Is it possible to embed a command prompt in a win32 app?

In linux and when installing packages etc. There are some installers that have a progress bar and a dos window which shows the files being extracted etc. How can i add this window to my C++ Win32 programs so that i can have it showing the tasks im doing? I cannot find any documentation on MSDN.
Question: How can i add a console window (if that's what its called, sure looks like one) in my program to show the details of the task at hand being done?
Here is a window with what i am asking.. (personal info so I erased the details. :]
You cannot embed a real console window inside another window (although a windowed process can have a separate console window). While it looks like a console window / command prompt, it is just a matter of appearances. What you want to do is create a sub-window/control with similar characteristics as a console window and then redirect the console output from the application(s) being run to append to that sub-window. For more information on how to do redirect the console output in Windows, see http://support.microsoft.com/kb/190351.
That "dos window" is a regular edit control: CreateWindow(ES_MULTILINE, EDIT, ...
However, it has the font set to a fixed-width one (Looks like courier). This is done by sending WM_SETFONT to the edit control.
#user995048 says "You cannot embed a real console window inside another window". But "cannot" is a strong word! I can run an entire virtualized computer in a window if I wish. :) So one can quite reasonably intuit that there are ways of doing what you say.
Sure, it is true that what you've seen are almost certainly cases of output redirection into a custom widget, designed to mimic the simple appearance of a terminal. However...if you want to embed one application's window inside another, there are things you can look into which might fit. Cooperative methods exist like GtkPlug, for instance:
http://developer.gnome.org/gtk/2.24/GtkPlug.html
To actually capture a not-designed-to-cooperate app's window and throw it in your app would be trickier. But possible, just as screen captures and virtual machines are possible. Probably best to avoid that sort of thing unless there's really a cause for it, though...
Try this
http://www.codeguru.com/cpp/misc/misc/article.php/c277/
link. I think the solution provided is what you need.
I tried it many years ago and it worked. I have not tried it in newer versions of windows though.

Closing a MessageBox automatically

I have a third party encryption library, which may create a MessageBox if key creation fails. The failure can be caused by bad random number generation or other rarities, and in most cases, trying again will result in success. My code will attempt key creation up to three times before deciding it failed.
Now, the issue is that the program may be used with automation. If a MessageBox is created during automation, it will block the process forever, because there's nobody to click the 'OK' button.
Does anyone know of a way to catch when this message box is created and automatically close it?
Anything is fair game, as long as it's not something that will make security suites angry. This means no hooking or code tunneling.
In summary, I need to catch when a MessageBox is created and close it. The MessageBox's creation is outside of my control. Modifying the code at runtime is not acceptable.
Also, I've noticed there are some other similar questions, but they don't have the same requirements.
EDIT: Additional note, I can find the message box via searching through all windows until I find one with a matching title and then send it a WM_CLOSE message, but I don't think this is a great solution. I also have no guarantee that the message box has been/will be displayed, or how long after my call it will be displayed. It could display instantly, it could display 1200 ms later, or it could not display at all.
Just before you begin the encryption process, install a WH_CBT hook, and in its callback watch for an nCode of HCBT_CREATEWND. If you get a matching class name ('#32770 (Dialog)' ?) and a matching title either return a nonzero value from the callback, or if that doesn't work post a WM_CLOSE (or a BM_CLICK to a relevant button if selecting an option is necessary). Uninstall the hook after the process for not messing with every possible dialog your application pops up.
That sounds like bad design on the part of that library. Generally any sort of utility library (like encryption) has no business invoking any kind of GUI (unless you explicitly ask it to).
Is there possibly some configuration or setting in this library that could disable its use of message boxes?
If not, I'd suggest that you might want to investigate using a different library. After all, if the designers of this library have already made this kind of poor design decision once, then there may be other unfortunate surprises lurking in there.
You can hope that it will be found by GetForegroundWindow, but this may catch other applications. The more brute force way is to iterate over all windows with EnumWindows looking for something that has a caption or text equal to this shown by the library.
I have once "remote controlled" an application by sending mouse click events to some controls. I guess you would have to do this in a separate thread that is watching for Events if a window is opened. Pretty ugly but working...
Create a new thread. If your function fails and a Message Box is opened, obtain a handle to the message box by looping through the windows (GetTopWindow, GetNextWindow) and comparing the window's process id to the one returned from GetCurrentProcessId().
Or, you can avoid all the hard work and just hook the MessageBox API with detours. It's not very hard, and if you don't want to pay for detours, you can do it manually.
Call VirtualProtect and set the memory protection at MessageBox at PAGE_EXECUTE_READWRITE
Create a naked function, and use it as a trampoline.
Create a function identical in parameters to MessageBox (this will be your hook)
Create a jump from MessageBox to your hook function.

Is there a Click Handler for Shell Extension

After going through MSDN Shell Extensions I am not quite sure if I can extend the behaviour of Shell Click or Click Event of explorer. Any suggestion or Code Snipet, article or Walk through?
There is no such possibility. That would make the shell too vulnerable. Imagine all the malware that exploits this functionality.
If you want to capture the click event, there is no easy way. You may SetWindowsHookEx and monitor/capture mouse messages for all windows of CabinetWClass and/or DirectUIHWND class. You may inject your code to explorer.exe's process and intercept messages from there. Here's a snippet of injecting a dll into a process.