4

I've injected a DLL into an existing game.

I can import functions and variables like this:

typedef const wchar_t* (__fastcall *get_string_t)(const char*, int, const char*, int);
get_string_t real_get_string = reinterpret_cast<get_string_t>(0x6f9ec0);
int& g_cx = *reinterpret_cast<int*>(0x8523e0);

but these function pointers and variables add an indirection and have cumbersome syntax. Is there a way to import the original functions from the executable such that the linker can find them and I can use normal (extern) declarations?

3

Yes, you could add an Export Table to the original executable, with named symbols (which you define) pointing to the addresses for those functions/globals/etc.

Your DLL could then import those function addresses by Export Table symbol name at runtime.

But whether you go with this plan or your current plan above, be careful about calling the EXE's functions from your DLL. Be sure you understand the function's side-effects, use of global variables, etc. since it may not be thread-safe to call it from your DLL.

  • Note that even with this approach you would still need to use typedef, etc. – Jason Geffner Sep 12 '14 at 19:15
  • Why would typedefs be needed? A normal declaration should be enough. And how would I import data (variables)? I'd prefer to resolve this at link time, that'd also avoid having to update the exe. – XTF Sep 13 '14 at 15:15
0

Do you mean calling functions from host executable, in which you have injected your DLL?

If this is the case I don't suggest you using direct addresses because there is possibility that the image is loaded at different address than intended (especially if 'IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE' flag is set). Better use 'GetModuleHandle' to get process image base and call your functions using RVA plus the value returned.

The above will be true if the target functions aren't in the EAT of the host image. If this is the case you can just declare their names and use those functions in your source if you have the proper host image libraries.

This is the best I can say because your question is a bit unclear.

EDIT: The code you have written in OP, can be re-written as follows:

const wchar_t* (__fastcall *get_string)(const char*, int, const char*, int) = (decltype(get_string))0x6f9ec0;

int& g_cx = *(int*)0x8523e0;

If your compiler supports C++11 or above because of the 'decltype' keyword.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.