Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook macro & std::string api for C #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ExtcanaRy
Copy link

@ExtcanaRy ExtcanaRy commented Sep 25, 2023

Example:

#include <modloader/cutils.h>
#include <modloader/log.h>

THOOK(on_initialize_logging, void,
		"_ZN15DedicatedServer17initializeLoggingEv",
		uintptr_t this)
{
	on_initialize_logging.original(this);
	
	char array[32];  // sizeof(std::string) == 32
	void *sstr_new = NULL;
	void *sstr_array = array;
	void *sstr_malloc = malloc(32);

	std_string_string(&sstr_new, "Constructed using operator new allocated memory");
	std_string_string(&sstr_array, "Constructed using array allocated memory");
	std_string_string(&sstr_malloc, "Constructed using malloc allocated memory");

	// The block of memory returned by std_string_c_str will be freed after
	// the std::string object is destructed, use strdup to save it if needed!
	char *msg = strdup(std_string_c_str(sstr_new));

	modloader_logd("std::string", std_string_c_str(sstr_new));
	modloader_logd("std::string", std_string_c_str(sstr_array));
	modloader_logd("std::string", std_string_c_str(sstr_malloc));

	std_string_destroy(sstr_new, true);
	std_string_destroy(sstr_array, false);
	std_string_destroy(sstr_malloc, false);
	
	modloader_logw("std::string", msg); // print again
	free((void *)msg);
	free(sstr_malloc);
}

Out:

11:45:14 Debug [std::string] Constructed using operator new allocated memory
11:45:14 Debug [std::string] Constructed using array allocated memory
11:45:14 Debug [std::string] Constructed using malloc allocated memory
11:45:14 Warn  [std::string] Constructed using operator new allocated memory

@ExtcanaRy ExtcanaRy reopened this Oct 3, 2023
@ExtcanaRy ExtcanaRy marked this pull request as ready for review October 3, 2023 03:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant