c++.netcoreclr

How to set up AppContext.BaseDirectory when hosting CoreCLR with hostfxr


I'm following this sample/tutorial to host CoreCLR in my native app using hostfxr, I've successfully set up the runtime and got the function pointer to call into managed code.

However, in the managed application, the AppContext.BaseDirectory property is always an empty string. I tried passing the main exe path with hostfxr_initialize_parameters.host_path, still the same. Any idea how to change it?


Solution

  • After asking this on the dotnet discord, I figured out that some runtime properties like this can be set with hostfxr_set_runtime_property_value

    Should look like this:

    // after hostfxr_initialize_for_*
    auto set_prop_fptr = (hostfxr_set_runtime_property_value_fn)get_export(lib, "hostfxr_set_runtime_property_value");
    set_prop_fptr(cxt, L"APP_CONTEXT_BASE_DIRECTORY", L"C:\\foo");