C++ - Accessing Member Variable, 2 seperate Struct

Memory scanning, code injection, debugger internals and other gamemodding related discussion
Post Reply
Learner
Noobzor
Noobzor
Posts: 11
Joined: Thu Aug 23, 2018 9:16 am
Reputation: 0

C++ - Accessing Member Variable, 2 seperate Struct

Post by Learner » Thu Sep 27, 2018 6:59 am

- I have 2 different structs here as shown below.

- Struct2 has a function called Function() where inside definition is another windowsapi function. This function needs a member variable from Struct1 which is HANDLE hProcess.

- What are the different ways to access the member variable hProcess from Struct1 in Struct2 then call it from Main? Please list a couple different methods on achieving what I want, besides using extern.




Struct1.cpp

Code: Select all

struct People
{
HANDLE hProcess;
};


Struct 2.cpp

Code: Select all

 
struct Animal 
{
void Function()
{
WindowsApiFunction(Needs HANDLE hProcess from Struct1.cpp)
}
};


Main.cpp

Code: Select all

Animal AnimalObject;
AnimalObject.function()

User avatar
TheyCallMeTim13
Administration
Administration
Posts: 1125
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 192
Contact:

Re: C++ - Accessing Member Variable, 2 seperate Struct

Post by TheyCallMeTim13 » Thu Sep 27, 2018 11:42 am

Look into constructors, you need to setup a constructor and pass the handle as an argument.
https://www.tutorialspoint.com/cplusplu ... ructor.htm

Learner
Noobzor
Noobzor
Posts: 11
Joined: Thu Aug 23, 2018 9:16 am
Reputation: 0

Re: C++ - Accessing Member Variable, 2 seperate Struct

Post by Learner » Fri Sep 28, 2018 12:56 pm

Yes I have but still don’t know how it will work because Struct1.cpp stores the HANDLE hProcess which stores the process handle of my process.

I need to use the exact same member variable hProcess from Struct1 inside of Struct2.

Can you show an example how to make a constructor work with my example? No one teaches how to do it across 2 seperate source files.

User avatar
TheyCallMeTim13
Administration
Administration
Posts: 1125
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 192
Contact:

Re: C++ - Accessing Member Variable, 2 seperate Struct

Post by TheyCallMeTim13 » Fri Sep 28, 2018 1:29 pm

Learner wrote:
Fri Sep 28, 2018 12:56 pm
Yes I have but still don’t know how it will work because Struct1.cpp stores the HANDLE hProcess which stores the process handle of my process.

I need to use the exact same member variable hProcess from Struct1 inside of Struct2.

Can you show an example how to make a constructor work with my example? No one teaches how to do it across 2 seperate source files.
The link I gave you shows you how to do it. And if I write the code for you, you'll never learn this stuff for yourself (it's literally against my religion to not teach, and just give stuff away). So again, don't just look at constructors, actually write some and work with them til you understand it. It's a really important part of programming, learning to work with parameters in various ways. I think you're trying to jump straight into something more complex than you should, when you seem to lack some of the most basic things needed to write C++.
Here is another link for learning C++: https://www.learncpp.com/

Learner
Noobzor
Noobzor
Posts: 11
Joined: Thu Aug 23, 2018 9:16 am
Reputation: 0

Re: C++ - Accessing Member Variable, 2 seperate Struct

Post by Learner » Sat Sep 29, 2018 1:45 pm

TheyCallMeTim13 wrote:
Fri Sep 28, 2018 1:29 pm
Yes I have read the basics I understand it.

But for my code it is abit different because I have 2 seperate classes in 2 seperate source files.

Struct1 : is already storing my process handle inside its member variable called HANDLE hProcess.

Struct2 : has a function definition and inside definition is another function which requires the exact copy of Struct1 member variable hProcess.

How can I pass arguments into a constructor so that I can use Struct1 hProcess in Struct2?

User avatar
TheyCallMeTim13
Administration
Administration
Posts: 1125
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 192
Contact:

Re: C++ - Accessing Member Variable, 2 seperate Struct

Post by TheyCallMeTim13 » Sat Sep 29, 2018 3:48 pm

Learner wrote:
Sat Sep 29, 2018 1:45 pm
...
Yes I have read the basics I understand it.

But for my code it is abit different because I have 2 seperate classes in 2 seperate source files.

Struct1 : is already storing my process handle inside its member variable called HANDLE hProcess.

Struct2 : has a function definition and inside definition is another function which requires the exact copy of Struct1 member variable hProcess.

How can I pass arguments into a constructor so that I can use Struct1 hProcess in Struct2?
...
Have you written any constructors yet? Post what you have written or even what you tried. But so far you really haven't showed much of anything. But again, create the class constructors, then pass the handle to both classes upon creation of them.

Learner
Noobzor
Noobzor
Posts: 11
Joined: Thu Aug 23, 2018 9:16 am
Reputation: 0

Re: C++ - Accessing Member Variable, 2 seperate Struct

Post by Learner » Tue Oct 02, 2018 6:33 am

TheyCallMeTim13 wrote:
Sat Sep 29, 2018 3:48 pm


Have you written any constructors yet? Post what you have written or even what you tried. But so far you really haven't showed much of anything. But again, create the class constructors, then pass the handle to both classes upon creation of them.

Does that mean I need to create another new variable in the animal struct called hProcess when passing with constructor?

Code: Select all

struct Animal 
{
Handle hProcess;
Animal(HANDLE h)
{
hProcess = h;
}
void Function()
{
WindowsApiFunction(Needs HANDLE hProcess from Struct1.cpp)
}
};

User avatar
TheyCallMeTim13
Administration
Administration
Posts: 1125
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 192
Contact:

Re: C++ - Accessing Member Variable, 2 seperate Struct

Post by TheyCallMeTim13 » Tue Oct 02, 2018 1:11 pm

Learner wrote:
Tue Oct 02, 2018 6:33 am
...
Does that mean I need to create another new variable in the animal struct called hProcess when passing with constructor?
...
Yes. Something like this.

Code: Select all

#include <iostream>
 
using namespace std;
class Line {
   public:
      void setLength( double len );
      double getLength( void );
      Line(double len);  // This is the constructor
 
   private:
      double length; // The variable to store the length
};
 
// Member functions definitions including constructor
Line::Line( double len) {
   cout << "Object is being created, length = " << len << endl;
   length = len;
}
void Line::setLength( double len ) {
   length = len;
}
double Line::getLength( void ) {
   return length;
}

// Main function for the program
int main() {
   Line line(10.0);
 
   // get initially set length.
   cout << "Length of line : " << line.getLength() <<endl;
   
   // set line length again
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}
just do it with both classes and pass the same handle when you create the instances.

Post Reply

Who is online

Users browsing this forum: No registered users