How to get class object of a function in hooked function? - c++

I recently started to work on application interception using madhookc hooking library.I am hooking the methods called in the application to do manipulation of text. At one point i got stuck and need your help.
I have a paragraph of text in which in which some text is Bold and some is regular style. Now i want to differentiate the regular and bold text and do different operations on both. I know that QFont::setFamily(QString) function is called for each line of text that is going to be displayed.So i hooked setFamily(). Now i am able to get font family of the text ,but all text of the paragraph (i.e. regular and bold) has same font family. Now i want to check whether this text is bold or not, so for that i need a object of QFont class so that i can call it's bold(); which returns true or false. Ultimately i want this pointer of the setFamily() function.(Same as like we get target in pointcut of AspectJ interception.)
void WINAPI newsetFamily( QString & family );
void (WINAPI *UnhooksetFamily)( QString & family );
void WINAPI newsetFamily ( QString & family )
{
QFont *font=this_pointer;
if(font->bold())
{
//do this
}
else
{
//do this
}
}
Please help me . Thanx in advance. . .

Finally i got my way !!!
After some research , i come to know that that current class object of a function is nothing but this pointer of the function, and this pointer can be exist only if the function belongs to some class.
I also came to know that QT class functions follows various calling conventions.The class i am referring follows the __fastcall for its functions. __fastcall calling convention requires first 2 hidden parameters to be passed to function if you want to hook it successfully. So i passed void * (void pointers) as first 2 parameters of the function and it worked.
Then i came to know that first void * pointer is nothing but this pointer of the function. That's how i got my target.
solved code:
void (__fastcall *UnhooksetFamily)( QFont *This,void *noUse,QString & family );
void __fastcall newsetFamily ( QFont *This,void *noUse,QString & family )
{
QFont *font=This;
if(font->bold())
{
//do this
}
else
{
//do this
}
}

Related

calling MFC edit control GetWindowText member can't compile

My code:
//the auto generated stuff (by right-click on editbox add variable to control option)
CEdit edit_name;
void CSendMessageWithActualDataDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_NAME, edit_name);
}
Looks OK so far, right?
void dofoo()
{
//IDE shows error squiggle at dot "name.GetWi" with no pop-up
//details given except the object's blurb text
CString foostring= edit_name.GetWindowTextW();
//error C2661: 'CWnd::GetWindowTextW': no overloaded function takes 0 arguments
// or instead with no parentheses added:
// no IDE error indication
CString foostring= edit_name.GetWindowTextW;
//error C3867: 'CWnd::GetWindowTextW': non-standard syntax; use '&' to create a pointer to member
}
According to:
Read text from edit control in MFC and VS2010
You can then retrieve the text in the edit box like this:
CEdit m_EditCtrl;
// ....
CString filePath1 = m_EditCtrl.GetWindowText()
.
.
.
// Yes, now it works...
I swear, this is like pulling teeth.
Apparently, this is all about convenience. Since you already know how to call CWnd::GetWindowText, but find it too cumbersome to use, just wrap everything up in a free function:
CString GetWindowText(CWnd const& wnd) {
CString s;
wnd.GetWindowText(s);
return s;
}
You can call that, passing in any object that (publicly) derives from CWnd (such as the CEdit edit_name) and get a CString object you can use any which way. (C++17 introduced guaranteed copy elision, meaning that s will never need to get copied.)
If you need to pass it into other Windows API functions that expect an LPCTSTR, CString implements operator PCXSTR that implicitly converts things as needed, e.g.
AfxMessageBox(GetWindowText(edit_name));
When you add a EDIT control to a dialog, and you go to map it to a variable, you have two choices:
Control
Value
If you select Value, then you can map it to a CString:
This creates the DDX_Text entry that was referred to in the comments:
void CTestDialogDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_strText);
}
Or, you can use GetDlgItemText if you want to just get the value directly from the control, and avoid creating a variable. This shows both:
void CTestDialogDlg::OnBnClickedButton1()
{
// This way you get the current text value from the edit control, without even creating a CEdit control member variable
CString strText;
GetDlgItemText(IDC_EDIT1, strText);
AfxMessageBox(strText);
// This way we use our mapped CString to the EDIT control
UpdateData(TRUE);
AfxMessageBox(m_strText);
}
If you do decide to map to a CEdit (a Control instead of a Value) then as you found out:
// Get the value from the control
m_editText.GetWindowText(strText);
AfxMessageBox(strText);
I assume your linked article is incorrect as GetWindowText does not return a CString.
By the way, with Visual Assist (a third-party extension) it shows the options correctly for Intellisense:
By the way, you may find this article useful (Dialog box controls and variable types). If you look at the table and find EDIT:
The above is with the previous rows on the table stripped out. As you can see, you can map the EDIT to more types of variables than just a CString.
this suffices:
class GetWindowText
{
CString s;
public:
GetWindowText(CWnd &w){w.GetWindowText(s);}
~GetWindowText() {}
CString operator()() {return s;}
};
just call like this:
MessageBoxW(GetWindowText(edit_name)());

Using callbacks in C++

I'm working on a project in C++, but at some point in the application it fails and generates a core dump. The application uses a couple of classes, which for the purposes here I'm concentrating on one of the classes, which I'm calling A, and is instantiated as object a. This has a large number of member functions, of which at the moment only a few are being used, but one of these generates a log to produce diagnostics to be used for debugging. I want to use this to find out why the application is failing.
The project is to put together code that invokes the various member functions, and although I have access to the source code and some limited documentation, none of the code can be changed, with all changes being in the code that makes use of the classes and invokes the member functions. The member function in question is:
void enable_log (log_callback callback, void * user_data = nullptr)
where the 1st argument callback contains the message and 2nd argument is optional. For now it can be set to nullptr, so would be invoked as:
a.enable_log(callback, nullptr);
From this documentation it's not at all clear what exactly callback is. However, in looking at the source code this is:
using log_callback = void (*)(const std::string& message, void* user_data);
in a header file, where log_callback is an alias for const std::string& if I understand this correctly.
I already have dummy classes on a platform using Visual Studio 2019 with some test member functions to simulate invoking the member functions on a remote Linux server, but I'm unable to find a way of making use of the member function above. I added the test member function to the dummy class as follows:
void enable_log(const std::string& callback, void* user_data = nullptr) {
callback = "ABCD";
}
which is supposed to generate a test string which is returned, such that in the real application this string will have diagnostic information that will be written to a file. However, the "=" is an error.
The idea is that in the main function an empty string will be declared, then enable_log() should populate this string, which can be printed out.
I've spent some time looking at various resources, including Stackoverflow, but I cannot find a way of returning a string with the information that can be printed out. I need a simple way to simulate this, and as I said above, I must not change the source code of the real member function, so the simulated member function has to produce a string in the same way. How is this done? Some advice would be appreciated.
Callback, in simple words, is some function that will be called later at some point. Example:
void callback_fn(int a);
using callback_t = (void)(*)(int a);
void some_func(callback_t);
You can use some_func() like so:
some_func(callback_fn);
Full example here: https://godbolt.org/z/ET3GhfYrv
For your usecase the parameters of the callback are slightly different. Here's how to read the syntax:
using log_callback = // this just creates an alias for whatever is on the right handside
void // the return type of the "callable" should be void
(*) // this tells us that it is a function pointer
(const std::string& message, void* user_data) // These are the arguments the callable takes. It is a "std::string" and a "void *"
To use this, just create a free function with the same signature:
void callable(const std::string &msg, void *userData = nullptr)
{
// msg is the data sent by the function. use it in whatever way
// you want.
std::cout << msg << '\n';
}
// Pass it to the enable_log
enable_log(callable);

What does it mean to "Implement an empty logic" for a function in ue4?

I'm following a tutorial for setting up a character interaction, and part of it says to make a header file with the following code:
public:
/*This property will be used in order to bind our subtitles
Binding will make sure to notify the UI if the content of the following
variable change.*/
UPROPERTY(BlueprintReadOnly)
FString SubtitleToDisplay;
/*Updates the displayed subtitles based on the given array*/
UFUNCTION(BlueprintCallable, Category = DialogSystem)
void UpdateSubtitles(TArray<FSubtitle> Subtitles);
/*This array will populate our buttons from within the show function*/
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
TArray<FString> Questions;
/*Adds the widget to our viewport and populates the buttons with the given questions*/
UFUNCTION(BlueprintImplementableEvent, BlueprintCallable, Category = DialogSystem)
void Show();
Then, it tells me to "Implement an empty logic for the UpdateSubtitles function for now." I have no idea what this means, and considering that UpdateSubtitles was the one thing to give me an error when I compiled this code, it's probably something important. Does anyone know what this terminology refers to?
It means to just leave the contents of the function blank or return an empty result such as:
FString AMyCharacter::GetNickname()
{
return "";
}
in the case where the return type isn't void.
I figured it out thanks to your comment! However it was a little different than what you described, it was actually:
void UDialogUI::UpdateSubtitles(TArray<FSubtitle> Subtitles)
{}
And only this line; adding a definition for Show() as suggested actually threw an error.

Dll Plug-in basic questions

For the last couple of days i've been learning C++ to make a dll plug-in for a program.
My objective is to get data(the Flight Plan's to be more precise) from the program and on a first phase save them to a text file(second phase will be connect them with python but for now it's just that).
So in my header file i imported a file with many classes and many functions(which is given by the plugin development guide). The class i'm interested in is the class CAircraftFlightPlan and it has some functions inside like this:
bool IsReceived(void) const;
//-----------------------------------------------------------------
// Return :
// true - if any kind of FL is received from the servers
// false - else
//-----------------------------------------------------------------
const char * GetOrigin ( void ) const ;
//-----------------------------------------------------------------
// Return :
// The origin airport.
//-----------------------------------------------------------------
int GetFinalAltitude ( void ) const ;
//-----------------------------------------------------------------
// Return :
// The final requested altitude.
//-----------------------------------------------------------------
I have many doubts about this, hope you can help:
1-what does it mean having "nameoffuction"(void) const? It receives nothing?how do i call these functions then
2-i do not understand this function "const char * GetOrigin ( void ) const ;",what does it want, a const or a char?
3-The comments below the functions tell that they return this or that. But how do they return that if the function is empty, it's just "int GetFinalAltitude(void) const"...
4-In the source file, i try to call one of the functions to write it to a txt file,how can i do this:
int airplane;
ofstream textfile;
textfile.open("FP.txt");
textfile<<int GetTrueAirspeed("MAH545"); //i know there's an error here, how do i solve it?
textfile.close();
I'm very sorry for asking these (noob i suppose) questions but they are way to specific to search for an answer online(i tried already)
Thank you for the help
Yes, it takes no arguments so you call it like so: nameoffunction()
This also takes no arguments so it is called as GetOrigin(). It returns a pointer to a const char.
As above, the functions take no arguments but do return values.
Delete the int in front of the function call. This should get rid of at least one error.
textfile<< GetTrueAirspeed("MAH545");

c++ - Creating custom events at runtime

I'm creating a 2D RPG game engine in C++ with Allegro. I've reached the point in which i need to implement a scripting system. So, my poblem is this one:
I have a struct called Event. Inside this struct there is a function pointer, which points to the function that i want to execute when the event is fired. So, here's an example:
struct Event {
//...
void (*func)(Player*, void*);
//...
}
Now, to create an event i have this function:
Event* Events::register_event_source(int x, int y, std::string name, Player* player, void (*func)(Player*, void*));
So, to use it i just need to create a function with this signature:
void test_event(Player* p, void* data)
{
//Do something cool here
}
and then register an event source, giving the address to that function:
//...
Player* player = new Player(0, 0);
//...
Event* evt = Events::register_event_source(10, 10, "test event", player, &test_event);
//Eventually set some data for the event
evt->set_data(new std::string("Just some test data"));
In this way, when the player goes over the assigned spot (in this case x = 10, y = 10) the event will fire, executing any code in the test_event function.
Now, my question is: is it possible to do, or at least to get close to, this process at runtime?? ...i would need to create the function (in this case "test_event") at runtime, but i did some research, and i think what i understood is that it is not really possible to create functions at runtime.
So, which approach should i go for?? ...I know it is an abstract question...but i really don't know how to approach this problem.
Thanks in advice for any help! and sorry for my bad explaining abilities...English is not my language!
If I understand correctly what you are trying to express, you are writing a scripting engine that interprets some logics built at run-time into a string, and this should determine what to do on Player and data. If so, I can imagine you should have a function like
void InterpretScriptCode(Player* p, void* data, string const& code)
or something equivalent that interprets and execute the logics described in code on p and data.
Then, you can use std::bind and std::function to encapsulate a call to your scripting engine:
// Header <functional> needs to be included, and a proper "using namespace"
// directive must be present for bringing placeholders _1 and _2 into scope
std::function<void(Player*, void*)> fxn = std::bind(
&InterpretScriptCode,
_1,
_2,
"int x = 0; ... blah blah" // this should be your run-time generated script
);
And pass fxn in input to your register_event_source() function.
Btw, you might be interested in using Boost.Signals/Boost.Signals2 for realizing event registration/handling.
If you are not using C++11, you can use boost::bind and boost::function instead of std::bind and std::function.