I'm trying to declare a variable in a header file Declarations.h
which will be accessed from multiple files... like in C# it's a public static uint variableXXX
then you can access it from everywhere.
Declarations.h:
#pragma once
#ifndef CLIENT_CONST_H
#define CLIENT_CONST_H
static DWORD LocalPlayerPointer, LocalPlayerAddress, battle_list = 0;
So the variables LocalPlayerPointer, LocalPlayerAddress
are those who I want to be accessible from everywhere.
dllmain.cpp:
#include "Includes.h"
LocalPlayerPointer = (DWORD)((moduleBase dwLocalPlayer));
LocalPlayerAddress = *(DWORD*)LocalPlayerPointer;
if (LocalPlayerAddress == 0) return;
Includes.h:
#include <Windows.h>
#include <iostream>
#include <cassert>
#include <vector>
#include <list>
#include <string>
#include <cstdint>
#include "Utils.h"
#include "Hook/Minhook/include/MinHook.h"
#include "ImGui/imgui.h"
#include "ImGui/imgui_impl_opengl3.h"
#include "ImGui/imgui_impl_win32.h"
#include "functions/functions.h"
#include "../Declarations.h"
#include "../Constants.h"
#include "../Offsets.h"
#include <chrono>
#include <sstream>
#include "../hooks.h"
#include "../targeting.h"
#pragma region OpenGL
#ifdef _WIN64
#define GWL_WNDPROC GWLP_WNDPROC
#endif
#define GLEW_STATIC
#if defined _M_X64
#include "Hook/GLx64/glew.h"
#pragma comment(lib, "Source/Hook/GLx64/glew32s.lib")
#elif defined _M_IX86
#include "Hook/GLx86/glew.h"
#pragma comment(lib, "Source/Hook/GLx86/glew32s.lib")
#endif
#include <gl/gl.h>
#pragma comment(lib,"opengl32.lib")
#pragma endregion
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
typedef BOOL(__stdcall* twglSwapBuffers) (_In_ HDC hDc);
typedef LRESULT(CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
So, until here everything is fine. From dllmain.cpp I can access the variables LocalPlayerPointer, LocalPlayerAddress, battle_list
and if I write to them, the values are reflected correctly.
The problem is when I try to read/write those variables from another file.
Let's put as example a new file called targeting.cpp.
targeting.h
#pragma once
#include "Source/Includes.h"
void Targeting();
targeting.cpp
#include "Source/Includes.h"
void Targeting(){
std::cout << LocalPlayerPointer << std::endl;
std::cout << LocalPlayerAddress << std::endl;
}
From targeting.cpp the std::cout displays 0.
What I'm doing wrong to access that variable like a public static var?
I want to access the same variable doesn't matters from which file I access to it.
EDIT:
After changing static DWORD LocalPlayerPointer, LocalPlayerAddress, battle_list = 0;
to extern DWORD LocalPlayerPointer, LocalPlayerAddress, battle_list = 0;
I'm getting this errors:
Error LNK2001 unresolved external symbol "unsigned long LocalPlayerPointer" (?LocalPlayerPointer@@3KA) Tibia OpenGL Imgui C:\Users\Adrian\Documents\Tibia OpenGL Imgui\OpenGLTemplate\dllmain.obj 1
Error LNK2005 "unsigned long battle_list" (?battle_list@@3KA) already defined in hooks.obj Tibia OpenGL Imgui C:\Users\Adrian\Documents\Tibia OpenGL Imgui\OpenGLTemplate\dllmain.obj 1
Here is my hooks.h:
#pragma once
#include "Source/Includes.h"
void InitHooks();
extern DWORD BattleHookGateway;
extern DWORD __fastcall BattleHook(DWORD* pThis, DWORD battleList, DWORD localplayer);
My hooks.cpp:
#include "Source/Includes.h"
DWORD BattleHookGateway;
#pragma region InitHooks
void InitHooks() {
// get battle list hook
BattleHookGateway = (DWORD)TrampHook32((char*)BattleListHook, (char*)BattleHook, 5);
}
#pragma endregion
#pragma region Target
DWORD __fastcall BattleHook(DWORD* pThis, DWORD battleList, DWORD localplayer) {
WriteLine("Hooked :D");
DWORD battle_list = *(DWORD*)battleList;
Targeting(battle_list);
return ((_BattleListHook)BattleHookGateway)(battleList, localplayer);
}
#pragma endregion
EDIT 2:
My problem now was that I was trying to declare them with = 0;
So after removing the inizialization in header file and init in another cpp file it worked.
CodePudding user response:
In the header file put
extern DWORD battle_int;
in one C file put the definition
DWORD battle_int = 0;
dont put 'static' thats specifically means 'private to this file'