From 88862d3d54def7a0b03ca03da81ef02ae9e65059 Mon Sep 17 00:00:00 2001 From: Elinor Fung <47805090+elinor-fung@users.noreply.github.com> Date: Tue, 9 Apr 2019 14:06:37 -0700 Subject: [PATCH] Simplify nethost API for getting hostfxr path (dotnet/core-setup#5734) - Remove nethost_ prefix on exported function - Collapse buffer size / required size into one inout parameter Commit migrated from https://github.com/dotnet/core-setup/commit/cb2db83f1ad2a4c8ca48a930c77656f29fca9a52 --- src/installer/corehost/cli/nethost/nethost.cpp | 18 +++++++++--------- src/installer/corehost/cli/nethost/nethost.h | 21 ++++++++++----------- .../corehost/cli/test/nativehost/nativehost.cpp | 12 ++++++------ .../HostActivationTests/NativeHosting/Nethost.cs | 2 +- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/installer/corehost/cli/nethost/nethost.cpp b/src/installer/corehost/cli/nethost/nethost.cpp index 637a3eb..4cdc466 100644 --- a/src/installer/corehost/cli/nethost/nethost.cpp +++ b/src/installer/corehost/cli/nethost/nethost.cpp @@ -18,13 +18,12 @@ namespace } } -NETHOST_API int NETHOST_CALLTYPE nethost_get_hostfxr_path( - char_t * result_buffer, - size_t buffer_size, - size_t * out_buffer_required_size, +NETHOST_API int NETHOST_CALLTYPE get_hostfxr_path( + char_t * buffer, + size_t * buffer_size, const char_t * assembly_path) { - if (out_buffer_required_size == nullptr) + if (buffer_size == nullptr) return StatusCode::InvalidArgFailure; trace::setup(); @@ -42,11 +41,12 @@ NETHOST_API int NETHOST_CALLTYPE nethost_get_hostfxr_path( size_t len = fxr_path.length(); size_t required_size = len + 1; // null terminator - *out_buffer_required_size = required_size; - if (result_buffer == nullptr || buffer_size < required_size) + size_t input_buffer_size = *buffer_size; + *buffer_size = required_size; + if (buffer == nullptr || input_buffer_size < required_size) return StatusCode::HostApiBufferTooSmall; - fxr_path.copy(result_buffer, len); - result_buffer[len] = '\0'; + fxr_path.copy(buffer, len); + buffer[len] = '\0'; return StatusCode::Success; } \ No newline at end of file diff --git a/src/installer/corehost/cli/nethost/nethost.h b/src/installer/corehost/cli/nethost/nethost.h index c7f3edc..25c82b8 100644 --- a/src/installer/corehost/cli/nethost/nethost.h +++ b/src/installer/corehost/cli/nethost/nethost.h @@ -35,14 +35,14 @@ // Get the path to the hostfxr library // // Parameters: -// result_buffer +// buffer // Buffer that will be populated with the hostfxr path, including a null terminator. // // buffer_size -// Size of result_buffer in char_t units -// -// out_buffer_required_size -// Minimum required size in char_t units for a buffer to hold the hostfxr path +// [in] Size of buffer in char_t units. +// [out] Size of buffer used in char_t units. If the input value is too small +// or buffer is nullptr, this is populated with the minimum required size +// in char_t units for a buffer to hold the hostfxr path // // assembly_path // Optional. Path to the compenent's assembly. Whether or not this is specified @@ -52,16 +52,15 @@ // // Return value: // 0 on success, otherwise failure -// 0x80008098 - result_buffer is too small (HostApiBufferTooSmall) +// 0x80008098 - buffer is too small (HostApiBufferTooSmall) // // Remarks: // The full search for the hostfxr library is done on every call. To minimize the need -// to call this function multiple times, pass a large result_buffer (e.g. PATH_MAX). +// to call this function multiple times, pass a large buffer (e.g. PATH_MAX). // -extern "C" NETHOST_API int NETHOST_CALLTYPE nethost_get_hostfxr_path( - char_t * result_buffer, - size_t buffer_size, - size_t * out_buffer_required_size, +extern "C" NETHOST_API int NETHOST_CALLTYPE get_hostfxr_path( + char_t * buffer, + size_t * buffer_size, const char_t * assembly_path); #endif // __NETHOST_H__ \ No newline at end of file diff --git a/src/installer/corehost/cli/test/nativehost/nativehost.cpp b/src/installer/corehost/cli/test/nativehost/nativehost.cpp index ce50fa4..8e4b149 100644 --- a/src/installer/corehost/cli/test/nativehost/nativehost.cpp +++ b/src/installer/corehost/cli/test/nativehost/nativehost.cpp @@ -30,7 +30,7 @@ int main(const int argc, const pal::char_t *argv[]) } const pal::char_t *command = argv[1]; - if (pal::strcmp(command, _X("nethost_get_hostfxr_path")) == 0) + if (pal::strcmp(command, _X("get_hostfxr_path")) == 0) { const pal::char_t *assembly_path = nullptr; if (argc >= 3) @@ -47,23 +47,23 @@ int main(const int argc, const pal::char_t *argv[]) #endif pal::string_t fxr_path; - size_t len = 0; - int res = nethost_get_hostfxr_path(nullptr, 0, &len, assembly_path); + size_t len = fxr_path.size(); + int res = get_hostfxr_path(nullptr, &len, assembly_path); if (res == StatusCode::HostApiBufferTooSmall) { fxr_path.resize(len); - res = nethost_get_hostfxr_path(&fxr_path[0], fxr_path.size(), &len, assembly_path); + res = get_hostfxr_path(&fxr_path[0], &len, assembly_path); } if (res == StatusCode::Success) { - std::cout << "nethost_get_hostfxr_path succeeded" << std::endl; + std::cout << "get_hostfxr_path succeeded" << std::endl; std::cout << "hostfxr_path: " << tostr(pal::to_lower(fxr_path)).data() << std::endl; return 0; } else { - std::cout << "nethost_get_hostfxr_path failed: " << std::hex << std::showbase << res << std::endl; + std::cout << "get_hostfxr_path failed: " << std::hex << std::showbase << res << std::endl; return 1; } } diff --git a/src/installer/test/HostActivationTests/NativeHosting/Nethost.cs b/src/installer/test/HostActivationTests/NativeHosting/Nethost.cs index 337c0ca..e946476 100644 --- a/src/installer/test/HostActivationTests/NativeHosting/Nethost.cs +++ b/src/installer/test/HostActivationTests/NativeHosting/Nethost.cs @@ -13,7 +13,7 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting { public class Nethost : IClassFixture { - private const string GetHostFxrPath = "nethost_get_hostfxr_path"; + private const string GetHostFxrPath = "get_hostfxr_path"; private const int CoreHostLibMissingFailure = unchecked((int)0x80008083); private static readonly string HostFxrName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr"); -- 2.7.4