HelpC++ pointer to function - set address of pointer manually.

Posts 1–2 of 2 · Page 1 of 1
C++ pointer to function - set address of pointer manually.
I know how to create a pointer to a variable and set the address manually, but I need to know how to set the address to a function for my project.
I'm doing DLL injection with CheatEngine, so I need to be able to reference a function from one of the game's DLL's.

So the main executable (WE.exe) loads various DLL's, and one of them is LagerPacket.dll
Inside LagerPacket.dll there's multiple functions for dealing with LPQ files which I'm trying to unpack, however I don't know how to create a pointer to the function I want.

CreatePatchFile(void*) @ 0x23BB10

So far as I understand, I would need to do something like:

void* a = (void*) <address>

I've successfully referenced individual pieces of char data, but I haven't been able to reference a function.

If anyone could help that'd be awesome
Code:
#include "stdafx.h"
#include "string"

void(*print_func_ptr)(const std::string&);

void print_func_a(const std::string& s) 
{
	printf("A => %s\n", &s[0]);
}

void print_func_b(const std::string& s)
{
	printf("B => %s\n", &s[0]);
}

void print_func_c(const std::string& s)
{
	printf("C => %s\n", &s[0]);
}

void* just_a_ptr[3] = {
	print_func_a,
	print_func_b,
	print_func_c
};

int main()
{
	/* Test Function A */
	print_func_ptr = print_func_a;
	print_func_ptr("Test Func A");

	/* Test Function B */
	print_func_ptr = print_func_b;
	print_func_ptr("Test Func B");

	/* Test Function C */
	print_func_ptr = print_func_c;
	print_func_ptr("Test Func C");

	/* Test Pointer arithmetic */
	for (size_t i = 0; i < std::size(just_a_ptr); i++) {
		print_func_ptr = reinterpret_cast<void(*)(const std::string&)>(just_a_ptr[i]);
		print_func_ptr("Test Ptr Array");
	}

	print_func_ptr = reinterpret_cast<void(*)(const std::string&)>(just_a_ptr[0]);
	print_func_ptr("Test Ptr Array Index 0");

	getchar();
    return 0;
}
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us