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

How to build a plog in a dynamic link library #292

Open
EricYoung121 opened this issue Oct 19, 2024 · 4 comments
Open

How to build a plog in a dynamic link library #292

EricYoung121 opened this issue Oct 19, 2024 · 4 comments
Assignees
Labels

Comments

@EricYoung121
Copy link

I used a dynamic link library to create a console to record the data generated by the dll, but it could not output the data after I initialized it, or the program crashed after I called PLOG_INFO. Is there any way to create a console using a dynamic link library and call it?

@SergiusTheBest
Copy link
Owner

Could you post the code you've used?

@EricYoung121
Copy link
Author

class logger
{
public:
	explicit logger(std::string_view console_title, file file) : 
		m_file_path(file)
	{
		if ((m_did_console_exist = AttachConsole(GetCurrentProcessId())) == false)
			AllocConsole();
		 
		if((m_console_handle = GetStdHandle(STD_OUTPUT_HANDLE)) != nullptr)
		{
			SetConsoleTitleA(console_title.data());
			SetConsoleOutputCP(CP_UTF8);

			m_console_out.open("CONOUT$", std::ios_base::out | std::ios_base::app);

			plog::init(plog::debug, m_file_path.c_str());
			plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
			plog::get()->addAppender(&consoleAppender);
		}
		g_logger = this;
	}
   };

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
    if (ul_reason_for_call == DLL_PROCESS_ATTACH)
    {
        DisableThreadLibraryCalls(hModule);
        g_main_thread = CreateThread(nullptr, 0,
            [](PVOID)->DWORD
            {      
                  std::make_unique<logger>("TEST", g_file_manager.get_project_file("./cout.log"));
                  PLOG_VERBOSE << "TEST VERBOSE MESSAGE";

                  CloseHandle(g_main_thread);
                  FreeLibraryAndExitThread(g_hmodule, 0);
            }, nullptr, 0, &g_main_thread_id);

Since I wouldn't use it in a dynamic link library, I tried to create a console program and use it just like I would in the main function, but it crashed my program directly

@SergiusTheBest
Copy link
Owner

@EricYoung121 Thanks for the code. I'll try it tomorrow when I have access to a Windows machine. Meanwhile it would be helpful if you post a crash stack trace.

@SergiusTheBest
Copy link
Owner

@EricYoung121 I can see a couple of variable life-time issues:

  • std::make_unique<logger>("TEST", g_file_manager.get_project_file("./cout.log")); - the result should be stored in a variable that lives long enough or else it will be immediately destroyed
  • plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender; - should be stored in a variable that lives long enough, in your case in as a member of logger

Also be careful with DisableThreadLibraryCalls:

Do not call this function from a DLL that is linked to the static C run-time library (CRT): https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-disablethreadlibrarycalls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants