C
C#

help

Uncaught Exception C++ hostfxr Handling

Jjborean10/10/2022
Hi I have a C++ dll that I am using to host some dotnet assembly and normally things work but when something bad happens, like a missing reference assembly, it fails with an uncaught exception. Now I know the code I have is wrong but what I am hoping to figure out is how to get the exception details. I can use dotnet-dump to dump the process, then dotnet-analyse to then select the proper thread and then pe -lines to get the exception information but I am hoping to just get that as part of the hosting code and log that somewhere in a file. I've tried to wrap the function ptr in a try/catch with various exception types as well as just
c++
typedef void(CORECLR_DELEGATE_CALLTYPE * custom_entry_point_fn)(worker_args_t args);
custom_entry_point_fn dotnet_main = nullptr;
int rc = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
L"...",
L"Main",
UNMANAGEDCALLERSONLY_METHOD,
nullptr,
(void **)&dotnet_main);

worker_args_t args{...};

try
{
dotnet_main(args);
}
catch (...)
{
std::cerr << "testing" << '\n';
}
c++
typedef void(CORECLR_DELEGATE_CALLTYPE * custom_entry_point_fn)(worker_args_t args);
custom_entry_point_fn dotnet_main = nullptr;
int rc = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
L"...",
L"Main",
UNMANAGEDCALLERSONLY_METHOD,
nullptr,
(void **)&dotnet_main);

worker_args_t args{...};

try
{
dotnet_main(args);
}
catch (...)
{
std::cerr << "testing" << '\n';
}
But no matter what I try to catch it with, the debugger always displays uncaught exception without any way to get those exception details, e.g.
Exception has occurred: W32/0xE0434352 Unhandled exception at 0x00007FF8EC62FF6C (KernelBase.dll) in notepad.exe: 0xE0434352 (parameters: 0xFFFFFFFF80070002, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x00007FF8A1AF0000).
I've then tried to hook up my own handler for hostfxr_set_error_writer that calls a simple function like the below but it is never called
c++
void hostfxr_error_handler(const char_t *message)
{
return;
}
c++
void hostfxr_error_handler(const char_t *message)
{
return;
}
I've tried to wrap the C# code it's calling in just a generic try { } catch {} as well as AppDomain.CurrentDomain.UnhandledException to try and see if I can just catch the exception to handle accordingly but none of it works. Does anyone have any suggestions or hints as to what I could potentially try. Bump to try again

Looking for more? Join the community!