How to call a function inside a struct

Hello all ,
i found this code online
But I don't understand it. Please explain this struct inside the class
(I am a beginner)
Artifacts.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Artifacts
{
	

public:
	Artifacts();



	struct sAABuffer_Helper
	{
		DWORD SecondDword;
		BYTE Filler[0x200];
	};

	struct sAABuffer
	{
		DWORD FirstDword;
		BYTE Filler[0x400];	
		sAABuffer_Helper* pBuffer;

		sAABuffer(DWORD First, DWORD Second)
		{
			FirstDword = First;
			pBuffer = new sAABuffer_Helper{};
			memset(Filler, 0, 0x400);
			
			pBuffer->SecondDword = Second;
			memset(pBuffer->Filler, 0, 0x200);
		}

		~sAABuffer()
		{
			delete pBuffer;
		}

	};





public:

	sAABuffer* m_AABuffer = 0x0;



private:

	VOID Init();


	~Artifacts()
	{
		delete m_AABuffer;

	}

};




Artifacts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25



#include "Artifacts.h"


Artifacts::Artifacts()
{
	Init();
}


VOID Artifacts::Init()
{


	LPVOID m_lpAAPacketFirstDword = 0x7df2;
	LPVOID m_lpAAPacketSecondDword = 0x7df3;

	m_AABuffer = new sAABuffer((DWORD)m_lpAAPacketFirstDword, (DWORD)m_lpAAPacketSecondDword);


}





other file

run.cpp

1
2
auto AAPacketBuffer = reinterpret_cast<DWORD>(this->m_Artifacts->m_AABuffer);

I think this file is incomplete

Please explain this code and is there another way to call this function sAABuffer(DWORD First, DWORD Second) .
thanks all ..



Last edited on
First of all, in C++, class and struct are the same, except that the default visibility of members is "private" for classes and "public" for structs. By convention, I would recommend to use struct for POD (plain-old-data), and class for all other types that are not POD.
https://www.educative.io/answers/what-are-plain-old-data-pod-types-in-cpp

In your code, the class Artifacts is declared in Artifacts.h, and its methods as well the the constructor are implemented (defined) in Artifacts.cpp. Maintaining the declaration and the implementation of a class in separate files is a standard approach in C++.

The class Artifacts also declares two "nested" classes (structs), called sAABuffer and sAABuffer_Helper.

______

The constructor of Artifacts, i.e. Artifacts::Artifacts(), simply calls the (private) method Artifacts::Init(). That method creates a new instance of sAABuffer, which will invoke the constructor of sAABuffer, i.e. sAABuffer::sAABuffer(). Also note that, in Artifacts::Init(), the pointer to the newly created sAABuffer instance is stored in the member variable Artifacts::m_AABuffer. Last but not least, the destructor, i.e. Artifacts::~Artifacts() is going to destroy the sAABuffer instance in Artifacts::m_AABuffer.

______

I think this code in invalid/dangerous:
auto AAPacketBuffer = reinterpret_cast<DWORD>(this->m_Artifacts->m_AABuffer);

That is because m_Artifacts->m_AABuffer is a pointer to an sAABuffer instance, i.e. it has type sAABuffer*. What is the purpose of reinterpret-castling this pointer to DWORD? Also, on 64-Bit operating systems, this will truncate the 64-Bit pointer to 32-Bits !!!

If you really need to store a pointer as an integral type, then at least use the DWORD_PTR type! But why is the cast needed at all? 😕
Last edited on
Thanks for the explanation, I understand, but there is one point
this file
run.cpp
1
2
3
4
5


auto AAPacketBuffer = reinterpret_cast<DWORD>(this->m_Artifacts->m_AABuffer);


i add

1
2
3
4
5
#include "Artifacts.h" 
Artifacts m_Artifacts ;

auto AAPacketBuffer = reinterpret_cast<DWORD>(m_Artifacts.m_AABuffer);


What do I add to make this line work?
this->m_Artifacts->m_AABuffer
But why is the cast needed at all?


 
	typedef bool(__thiscall* p_AAFunction)(DWORD __this, DWORD __Two, DWORD __PlayerID, DWORD __MobID, DWORD __PlayerX, DWORD __PlayerY, DWORD __Zero);

i call game function (thiscall function) , first arg DWORD __this .
Maintaining the declaration and the implementation of a class in separate files is a standard approach in C++.


Except for templated classes.

What is the purpose of reinterpret-castling this pointer to DWORD? Also, on 64-Bit operating systems, this will truncate the 64-Bit pointer to 32-Bits !!!


i found this code online


hiding under a rock? Much code 'found' on-line isn't 'good' modern C++ code. This code has all the 'bad smell' of c++98 32-bit code. It certainly won't work reliably with a 64-bit compile. This shows the 'bad practice' that was used many years ago when a pointer was typically 4 bytes and could be assigned-to/obtained -from a 32-bit unsigned variable such as DWORD. That 'trick' can't be done with a 64-bit compile where pointers are 8 bytes - not 4 bytes.

What's it supposed to do? From where did you 'find it' for that search?
Last edited on
Be aware that foo->bar is nothing but a shorthand for (*foo).bar.

In other words, the expression foo->bar is used to access the member "bar" of the object that the pointer foo is pointing to.

If you have a "local" object, rather than a pointer to an object, you can use foo.bar.


What do I add to make this line work?
this->m_Artifacts->m_AABuffer

Please explain, what do you want this line to do ?!?


i call game function (thiscall function) , first arg DWORD __this .
The type DWORD is a 32-Bit unsigned integer (number).

Be aware: You must not use DWORD to pass a pointer, because pointers can be (and usually are) 64-Bit in size nowadays!

If you want to pass a pointer, the parameter shall have the appropriate pointer type, e.g. sAABuffer*.


Casting a pointer to an integral type is not recommended and should be avoided whenever possible 😟

But if you really have to do it, then use the uintptr_t or DWORD_PTR type!

(uintptr_t and DWORD_PTR are suitable to hold a pointer, i.e. they are 64-Bit in size on the 64-Bit operating system)


See also:

https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/66d61dd8-2191-4a37-b963-e49bf0dc2579

https://cplusplus.com/reference/cstdint/
Last edited on
Thanks for reply seeplus , kigar64551
Thanks for this information and explanations

What's it supposed to do? From where did you 'find it' for that search?

from google , this is open source bot .




What do I add to make this line work?
this->m_Artifacts->m_AABuffer

Please explain, what do you want this line to do ?!?


Where did this (this->) come from?

Artifacts m_Artifacts ; // Create an object of MyClass
Is there another way to create object ? or just the way above

Thanks all ...
For 'sanity', consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Artifacts {
public:
	Artifacts();

	struct sAABuffer_Helper {
		DWORD SecondDword {};
		BYTE Filler[0x200] {};
	};

	struct sAABuffer {
		DWORD FirstDword {};
		BYTE Filler[0x400] {};
		sAABuffer_Helper* pBuffer {};

		sAABuffer(DWORD First, DWORD Second) : FirstDword(First), pBuffer(new sAABuffer_Helper {}) {
			std::memset(Filler, 0, 0x400);

			pBuffer->SecondDword = Second;
			std::memset(pBuffer->Filler, 0, 0x200);
		}

		~sAABuffer() {
			delete pBuffer;
		}

		sAABuffer(const sAABuffer&) = delete;
		sAABuffer& operator=(const sAABuffer&) = delete;
	};

	void Init();

	~Artifacts() {
		delete m_AABuffer;
	}

	Artifacts(const Artifacts&) = delete;
	Artifacts& operator=(const Artifacts&) = delete;

//private:
	sAABuffer* m_AABuffer {};
};

Artifacts::Artifacts() {
	Init();
}

void Artifacts::Init() {
	// WHAT DO THESE 'MAGIC NUMBERS' mean???
	const DWORD m_lpAAPacketFirstDword { 0x7df2 };
	const DWORD m_lpAAPacketSecondDword { 0x7df3 };

	m_AABuffer = new sAABuffer(m_lpAAPacketFirstDword, m_lpAAPacketSecondDword);
}

int main() {
	Artifacts m_Artifacts;

	auto AAPacketBuffer { m_Artifacts.m_AABuffer };
}


Note that as the variable is called m_Artifacts this suggests that this belongs in another class somewhere...
Last edited on
Artifacts m_Artifacts ; // Create an object of MyClass
Is there another way to create object ? or just the way above

Create a local object (automatic storage duration), which will be destroyed automatically as soon as the current function returns:
1
2
Artifacts m_Artifacts;
m_Artifacts.something;

Create an object on the heap space, which remains "alive" even after the current function returns:
1
2
Artifacts *m_Artifacts = new Artifacts();
m_Artifacts->something;

Note that the new operator returns a pointer to object (which lives on the heap), so we have to use -> to access its members.

Also note that, the object created with new operator, must explicitly be destroyed, via the delete operator, when no longer needed!
Last edited on
Since no-one has explictly answered this (although it's covered implicitly by kigar):

Hawlong wrote:
Artifacts m_Artifacts ;
[...]
What do I add to make this line work?
this->m_Artifacts->m_AABuffer


You're treating m_Artifacts as if it were a pointer, but it's not. To access elements of a struct or class, use a dot:

this->m_Artifacts.m_AABuffer
Last edited on
What do I add to make this line work?
this->m_Artifacts->m_AABuffer


Well it could be used within a class. As a simple example (this isn't needed) (based upon my code above):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Run {
	Artifacts* m_Artifacts {};

public:
	Run() : m_Artifacts(new Artifacts) {}

	~Run() {
		delete m_Artifacts;
	}

	Run(const Run&) = delete;
	Run& operator=(const Run&) = delete;

	void run() {
		auto AAPacketBuffer { m_Artifacts->m_AABuffer };
	}
};

int main() {
	Run r;

	r.run();
}


But what are you trying to achieve???
Last edited on
Thanks so much seeplus, MikeyBoy, kigar64551

I understand. Thank you for this much information
I would like to express my thanks and appreciation for your great effort

But what are you trying to achieve???


build buffer to thiscall function ,
Last edited on
Topic archived. No new replies allowed.