How to use HANDLE VARIABLE from main in other cpp files

Pages: 12
Have a problem.
- I am making a function for my struct called READPLAYERINFORMATION();
- Inside the definition is READPROCESSMEMORY(hProcess), hProcess is the handle required.
- I need to get hProcess handle from the main.cpp to work in addressoffsets.cpp, how can I make this work? I don't know how to do this can someone help please.



ADDRESSOFFSET.CPP:
1
2
3
4
5
6
7
8
#pragma once 
#include <iostream> 
#include <Windows.h> 
#include "AddressOffsets.h" 

void Player_t::ReadPlayerInformation() 
{ 
   ReadProcessMemory(hProcess)  <

} 



MAIN.CPP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include <iostream> 
#include "Windows.h" 
#include "ProcessMemory.h" 
#include "AddressOffsets.h" 


using namespace std; 

int main() 
{ 
   DWORD ProcessID; 
   ProcessID = GetProcessID(L"csgo.exe"); 

   uintptr_t ModuleBaseAddress; 
   ModuleBaseAddress = GetModuleBaseAddress(ProcessID, L"client_panorama.dll"); 

   HANDLE hProcess; 
   hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessID); 
Last edited on
Is this a valid program?
x.cpp
1
2
3
4
5
6
#include <iostream>

void foo( int bar )
{
  std::cout << bar;
}


y.cpp
1
2
3
4
5
6
7
void foo( int bar );

int main ()
{
  int hProcess = 42;
  foo( hProcess );
}

Can line 5 of file x.cpp use the value 42 that function call on line 6 of y.cpp has as argument?

Yes.
What are you on about? I don't think you understand. My question and your answer has nothing to do at all. That is simple stuff. This is different.

I am trying to define ReadPlayerInformation function in AddressOffsets.cpp.

And ReadProcessMemory(hProcess, etc, etc) is the definition to ReadPlayerInformation function.

But ReadProcessMemory function requires a handle to make it work.
The handle is called hProcess which belongs and is already defined in main.cpp.

HANDLE hProcess is in main.cpp.

How do I make handle hProcess from main.cpp work in AddressOffsets.cpp? Because I want to use it inside my readprocessmemory function.
Last edited on
Pass it as a function parameter.
Any example on how to do it with my code?
1
2
3
void Player_t::ReadPlayerInformation(HANDLE hProcess) 
{ 
   ReadProcessMemory(hProcess);
Doing it like that, are we still using hProcess from the main.cpp? Or are we creating a new hProcess Handle Variable?

Because I need to use the same handle variable that is used in main.cpp
Do you know what a function is? Do you know about passing parameters to functions?
What are you on about? I don't think you understand. My question and your answer has nothing to do at all. That is simple stuff. This is different.

How is int foo; different from HANDLE bar;?
The int and the HANDLE are both types.
The foo and the bar are both variables.
I see no difference there. The language sees no difference there.


You can pass data to a function by value or by reference.
See http://www.cplusplus.com/doc/tutorial/functions/


You definitely can pass a HANDLE to a function. If you could not, then
ReadProcessMemory( hProcess ); would be an error.


The only detail of intrigue is, are HANDLE objects copyable? If yes, does it even matter?

If they are not copyable, then
1
2
3
4
HANDLE foo;
HANDLE bar = foo; // error, not copyable
HANDLE& gaz = foo; // ok
const HANDLE& yak = foo; // ok 


If they are copyable, does it matter?
1
2
3
4
5
HANDLE foo;
HANDLE bar = foo; // ok
ReadProcessMemory( bar );
// is foo == bar now?
// if not and that matters, then you don't want to use the copy 


Either way:
1
2
3
4
HANDLE foo;
HANDLE& gaz = foo;
ReadProcessMemory( gaz );
assert( foo == bar );


If the HANDLE does not change in ReadProcessMemory( hProcess );
then this is enough:
1
2
3
void Player_t::ReadPlayerInformation( const HANDLE & hProcess ) 
{ 
   ReadProcessMemory(hProcess);
How is int foo; different from HANDLE bar;?

A HANDLE is a typedef for void*
Then why does it say hProcess is undefined?

hProcess is in main.cpp
I am in addressoffsets.cpp where I am defining a function where I need to use hProcess from the main.cpp

- How can addressoffsets.cpp use hProcess from main.cpp when nothing is included? It is not linked or anything.

I am doing what you say and it says it is not defined or whatever. Please read over my question again.
Last edited on
Here I post all my code for people who don't understand:

MAIN.CPP :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
#include "Windows.h"
#include "ProcessMemory.h"
#include "AddressOffsets.h"


using namespace std;

int main()
{
	
	DWORD ProcessID = GetProcessID(L"csgo.exe");

	uintptr_t ModuleBaseAddress = GetModuleBaseAddress(ProcessID, L"client_panorama.dll");

	HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessID); 




ADDRESSOFFSETS.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
#pragma once
#include <iostream>
#include <Windows.h> 
using namespace std;

// Address & Offsets //
const DWORD LocalPlayerOffset;
const DWORD LocalEntityOffset; 
const DWORD FlagsOffset;
const DWORD ForceJumpOffset;


struct Player_t
{
	uintptr_t LocalPlayerBasePointer;
	int FlagsValue;
	int ForceJumpValue;

	void ReadPlayerInformation();

	



}MyPlayer;





ADDRESSOFFSETS.CPP : want to use the same hProcess in main.cpp here!

1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include <iostream>
#include <Windows.h> 
#include "AddressOffsets.h"


void Player_t::ReadPlayerInformation()
{
	ReadProcessMemory(HANDLE hProcess)

}



I still cannot use the HANDLE hProcess from main.cpp , and use it in addressoffsets.cpp - Please tell me what is going on this is really stressful
Last edited on
Repeater's code (and mine) do show what to do. Who is not paying attention here?

1
2
3
4
struct Player_t
{
  void ReadPlayerInformation( HANDLE snafu );
};


1
2
3
4
void Player_t::ReadPlayerInformation( HANDLE snafu )
{
  ReadProcessMemory( snafu );
}
You think I only want to pass the handle within the same cpp file lol. I need the handle hProcess which stores the process info.

ReadProcessMemory needs a handle from a process. If I do your way the handle SNAFU is just a empty variable. It has no process linked to it at all.
Where as in my main.cpp I have defined the HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessID);

Now I need the same handle hProcess from main throughout all cpp files!

But still you are not using the same handle from main with all of it. Maybe you should pay more attention.
Last edited on
pLearner,

Can you spot the difference?
HANDLE snafu
snafu
Where do you use each? And why?


Maybe you should pay more attention.

Hmm



1
2
3
4
void AskNicely( int number )
{
   PoliteReply( number );
}

Can you spot the difference?
int number
number
Where do you use each? And why?
Last edited on
You think I only want to pass the handle within the same cpp file lol.

You have no idea what I'm thinking, but I assure you that it is not printable.

I see that we're done here.
Yeh people who don't know try to teach something when you don't understand.

Learn how ReadProcessMemory works mate. It needs a handle input "hProcess".

You telling me to pass a empty variable, make a new empty variable HANDLE and pass it in lol when I need the exact copy of the hProcess that was in main.cpp, and I want to use it throughout all cpp files. Because hProcess in main is what has stored my process ID.


Dunno how much more I need to tell you don't expect much this not for your type of programming I assume.
Last edited on
I sense a lil' bit of arrogance from someone ostensibly asking for help.

1
2
3
4
5
void Player_t::ReadPlayerInformation()
{
	ReadProcessMemory(HANDLE hProcess)

}

This doesn't make sense, it isn't valid syntax. Pass your HANDLE object as a parameter of your ReadPlayerInformation method.
void Player_t::ReadPlayerInformation(HANDLE the_name_of_the_local_handle_variable)
Otherwise, you have to make your HANDLE object a global variable (or global variable with a fancy wrapper on it that people like to call a "Singleton"), which I do not suggest.

Edit and Edit 2:

My question and your answer has nothing to do at all. That is simple stuff. This is different.
Passing data to a function is not complicated. I think you're confused on the nature of passing by value. Say the data in the HANDLE object (regardless of actual type) is 13245. When you pass your handle function by value to a function, you are copying that value 13245. In this scenario, that value represents the "handle" to the actual data you want, which is more than just one number. You can think of a "handle" as another word for "pointer".

That being said, there can be times that make this more complicated, in general. Say the data that your HANDLE points to outlives the scope of the function it was created it, then you will have to do something a bit more complicated to not have a memory leak, but based on what you've shown us so far, this does not appear to be the case.
Last edited on
Learn how ReadProcessMemory works mate. It needs a handle input "hProcess".

This, coming from someone who doesn't know how to call a function?

You have copy-pasted some code from the internet with no idea at all what you're doing, and when people tell you how to fix it, instead of trying to understand, you keep repeating incorrect things about how functions work.

You have a choice now. You can stop repeating things that are wrong, and accept that you need to learn how to call a function in C++. You can learn it, and then you can make your code work.

Or, you can say something like "lol" again, and pretend that you're super-cool and you know what you're doing and it's all these people here with experience and knowledge who are just too stupid to help you, and move on. This option is easiest for everyone else. I can guarantee you that everyone else in this thread could have already rewritten your code for you such that it worked. The only reason they have not done so is your attitude. So what's more important to you; seeming super-cool to yourself, or getting your code working?
Last edited on
1
2
3
4
5
6
7
8
9
/* filename: main.cpp */

#include "other.cpp"

int main()
{
    int x;
    ref = x;
}


1
2
3
4
5
6
7
8
/* filename: other.cpp */

int& ref;

void f() {
    // do something with ref (i.e: do something with x)
}
Pages: 12