Jump to content


Photo
- - - - -

Reference Vars in a header


  • Please log in to reply
4 replies to this topic

#1 aqrit

aqrit

    Member

  • Members
  • 80 posts

Posted 13 November 2011 - 09:20 PM

*edit #2

I "have to" add the static keyword when I include a header containing a reference var into more than one source file... to prevent a "already defined in some other .obj error"

whats the deal with the static keyword in this context?
is their some way to make it a const?

the whole pointer derefernce thing gets inlined, as far as I can tell...

// header.h
static DWORD& g_dwHealth = (*((DWORD*)0x00600000));
static HMODULE ( __stdcall *& xGetModuleHandleA )( char* szName ) = *( (HMODULE(__stdcall **)(char*)) 0x004610FC);

these reference vars allow me to really cleanup the syntax in my injected code
I can use data_vars of the target as if they were just regular globals
especially useful when calling a pointer to a pointer to a function :)

#2 aqrit

aqrit

    Member

  • Members
  • 80 posts

Posted 18 November 2011 - 06:52 PM

Any C/C++ Gurus?
*bump* edited OP

#3 KEMiCZA

KEMiCZA

    Administrator

  • Administrators
  • 400 posts
  • LocationBelgium

Posted 19 November 2011 - 12:58 PM

I don't really understand your question., but I think your problem is this. If you get errors saying that it has been used multiple times try adding "#pragma once" on top of your header file.

#4 aqrit

aqrit

    Member

  • Members
  • 80 posts

Posted 19 November 2011 - 05:56 PM

sorry that got lost when I edited
I am using #pragma once and include guards

The problem is that "static" means that
every source file gets it own internal copy of the var
so if I had 15 source files I might have the same var 15 times
where as, I could declare a const pointer instead and have only one const pointer...
(if I understand that right?)

I am tempted to just use - #define GAME_health (*((DWORD*)0x00600000))
but I'm looking for the ideal way to do it and I'd rather have a real var in a namespace - game::health

#5 gamer123

gamer123

    Posting Well

  • Members
  • PipPipPip
  • 32 posts

Posted 20 November 2011 - 12:47 PM

You should use the keyword "extern" instead.

// declare the actual global variables in only one file
DWORD& g_dwHealth = (*((DWORD*)0x00600000));
HMODULE (__stdcall *& xGetModuleHandleA)(char* szName) = *((HMODULE(__stdcall **)(char*)) 0x004610FC);

// refer to those global variables that is declared elsewhere in your project
// use this defination in all other files
extern DWORD& g_dwHealth;
extern HMODULE (__stdcall *& xGetModuleHandleA)(char* szName);

refer:
http://wiki.answers...._of_extern_in_C
http://faq.cprogramm...4&id=1043284376




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users