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

Fix error and crash related to using inside a DLL #147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions pecoff.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ backtrace_initialize (struct backtrace_state *state,
#ifdef HAVE_WINDOWS_H
HMODULE nt_dll_handle;

module_handle = (uintptr_t) GetModuleHandle (NULL);
module_handle = (uintptr_t) GetModuleHandle (state->filename);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
module_handle = (uintptr_t) GetModuleHandle (state->filename);
module_handle = (uintptr_t) GetModuleHandleA (state->filename);

#endif

ret = coff_add (state, descriptor, error_callback, data,
Expand Down Expand Up @@ -1061,31 +1061,34 @@ backtrace_initialize (struct backtrace_state *state,
#endif

#ifdef HAVE_WINDOWS_H
nt_dll_handle = GetModuleHandleW (L"ntdll.dll");
if (nt_dll_handle)
if ((uintptr_t) GetModuleHandle (NULL) == module_handle)
{
LDR_REGISTER_FUNCTION register_func;
const char register_name[] = "LdrRegisterDllNotification";
register_func = (void*) GetProcAddress (nt_dll_handle,
register_name);

if (register_func)
{
PVOID cookie;
struct dll_notification_context *context
= backtrace_alloc (state,
sizeof (struct dll_notification_context),
error_callback, data);

if (context)
{
context->state = state;
context->data = data;
context->error_callback = error_callback;

register_func (0, &dll_notification, context, &cookie);
}
}
nt_dll_handle = GetModuleHandleW (L"ntdll.dll");
if (nt_dll_handle)
{
LDR_REGISTER_FUNCTION register_func;
const char register_name[] = "LdrRegisterDllNotification";
register_func = (void*) GetProcAddress (nt_dll_handle,
register_name);

if (register_func)
{
PVOID cookie;
struct dll_notification_context *context
= backtrace_alloc (state,
sizeof (struct dll_notification_context),
error_callback, data);

if (context)
{
context->state = state;
context->data = data;
context->error_callback = error_callback;

register_func (0, &dll_notification, context, &cookie);
}
}
}
}
#endif /* defined(HAVE_WINDOWS_H) */

Expand Down