-1

Ive been working on making a memory reader / writer for awhile now and ive ran into some problems.

I made a simple Memory.h / Memory.cpp to handle memory calls. I have my Main.cpp calling a "GetProcessID" and "OpenProcess". This all works fine. However i decided to split up things across multiple cpp files.

For instance i have attached some code..

Main.cpp:

#include <Windows.h>
#include <iostream>
#include "Other.h"
#include "Memory.h"

Using namespace std;

Memory Mem;

int main(){

Mem.GetProcessID(ProcessName); // Getting Process ID
cout << "Main.cpp ProcID : " << Mem.ProcID;
TestCall();


}

In this i get the process ID I expected. In Other.h:

#include "Other.cpp"
void TestCall();

in Other.cpp:

#include "Other.h"
#include "Memory.h"
#include <windows.h>
#include <iostream>

using namespace std;

Memory Mem1;

void TestCall()
{
cout << "Other.cpp ProcID: " << Mem1.ProcID;
}

I get both couts come through on console however main displays the ProcID, Other.cpp displays 0 unless i also add:

Mem1.GetProcessID(ProcessName);

to other.cpp.

Is there a way i can "share" the process id? I also go on to getting base module address and opening a handle. these are also not "Shared". I don't really want to have to open 2 handles, 2 base address calls etc.

Things i have tried...

1) Putting this into Memory.h:

class Memory{
Public:
DWORD ProcID;
}

incase anyone is wondering... This is my memory.h and memory.cpp

Memory.h (this is in the public class)

DWORD GetProcessID(LPCTSTR ProcessName);

Memory.cpp

DWORD Memory::GetProcessID(LPCTSTR ProcessID) 
{
HWND gWindow = FindWindow(NULL, ProcessID);
DWORD ProcIDBuffer;
GetWindowThreadProcessId(gWindow, &ProcIDBuffer);
return Memory::ProcID = ProcIDBuffer; 
}

Thanks in advance!

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
Goat
  • 3
  • 2
  • 4
    Don't `#include` source files. Least of all in header files. – Some programmer dude May 23 '19 at 14:11
  • And please take some time to read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve]. – Some programmer dude May 23 '19 at 14:12
  • Ive removed the #includes in my .h files. Thanks. But still no joy, I will keep trying! – Goat May 23 '19 at 14:15
  • 1
    Why do you expect `Mem1.ProcID` to somehow magically acquire the same value as `Mem.ProcID`? Those are two distinct, unrelated variables. When you write `int x; int y; x = 42;` , I assume you don't expect `y` to somehow also get a value of 42. – Igor Tandetnik May 23 '19 at 14:19
  • The normal ways of sharing state are to take that state as a parameter in the functions using it or to have those functions be member functions of a class containing that state. Are these options not viable in this case? – chris May 23 '19 at 14:21
  • In Main.cpp i defined Memory as Mem. In Other.cpp i defined Memory as Mem1 – Goat May 23 '19 at 14:21
  • Those are two different objects. It does not matter if you named them the same or not. They will not share their values. – drescherjm May 23 '19 at 14:22
  • I cant define Memory as Mem in Other.cpp as i get the linker error. I thought defining it literally only makes it into a "shortcut" – Goat May 23 '19 at 14:24
  • 1
    Understand what @IgorTandetnik mentioned. That is very important. – drescherjm May 23 '19 at 14:25

1 Answers1

0

One way: in other.cpp, change:

Memory Mem1;

to:

extern Memory Mem;

And then change:

Mem1.ProcID

to:

Mem.ProcID

But really, you should read a good book

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Thank you for this. Am i right in saying that because we have already defined Memory as Mem in main.cpp everywhere else will become extern Memory Mem as its already been defined? – Goat May 23 '19 at 14:33
  • Yes, that is correct. For that reason, it is common to put the `extern` declaration in a header file. – Paul Sanders May 23 '19 at 14:35