~ruther/NosSmooth.Local

b9a0a7143727a84910992e29d2d345e1e9869f85 — Rutherther 2 years ago ce0260d
feat: remove asserts and return an error as thread exit code
M src/Inject/NosSmooth.Inject/nossmooth.cpp => src/Inject/NosSmooth.Inject/nossmooth.cpp +10 -4
@@ 105,13 105,15 @@ int LoadAndCallMethod(LoadParams* params)
{
    if (!load_hostfxr())
    {
        assert(false && "Failure: load_hostfxr()");
        return 0;
        return 2;
    }

    load_assembly_and_get_function_pointer_fn load_assembly_and_get_function_pointer = nullptr;
    load_assembly_and_get_function_pointer = get_dotnet_load_assembly(params->runtimeConfigPath);
    assert(load_assembly_and_get_function_pointer != nullptr && "Failure: get_dotnet_load_assembly()");
    if (load_assembly_and_get_function_pointer == nullptr)
    {
        return 3;
    }

    typedef void (CORECLR_DELEGATE_CALLTYPE* main_entry_point_fn)();
    main_entry_point_fn main = nullptr;


@@ 122,7 124,11 @@ int LoadAndCallMethod(LoadParams* params)
        UNMANAGEDCALLERSONLY_METHOD,
        nullptr,
        (void**)&main);
    assert(rc == 0 && main != nullptr && "Failure: load_assembly_and_get_function_pointer()");
    if (rc != 0 || main == nullptr)
    {
        return 4;
    }

    main();
    return 1;
}
\ No newline at end of file

M src/Inject/NosSmooth.Injector/Errors/InjectionFailedError.cs => src/Inject/NosSmooth.Injector/Errors/InjectionFailedError.cs +2 -1
@@ 12,4 12,5 @@ namespace NosSmooth.Injector.Errors;
/// The injection could not be finished successfully.
/// </summary>
/// <param name="DllPath">The path to the dll.</param>
public record InjectionFailedError(string DllPath, string Reason) : ResultError($"Could not inject {DllPath} dll into the process. {Reason}");
\ No newline at end of file
public record InjectionFailedError(string DllPath, string Reason, InjectionResult? Result = null) : ResultError
    ($"Could not inject {DllPath} dll into the process. {Reason}, {Result}");
\ No newline at end of file

A src/Inject/NosSmooth.Injector/InjectionResult.cs => src/Inject/NosSmooth.Injector/InjectionResult.cs +41 -0
@@ 0,0 1,41 @@
//
//  InjectionResult.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace NosSmooth.Injector;

/// <summary>
/// A result obtained from NosSmooth.Inject.
/// </summary>
public enum InjectionResult
{
    /// <summary>
    /// Successful result.
    /// </summary>
    Ok = 1,

    /// <summary>
    /// Hostfxr.dll was not found, is .NET installed? It should be as this is a .NET application injecting the dll...
    /// </summary>
    HostfxrNotFound = 2,

    /// <summary>
    /// A runtimeconfig.json of the assembly to be injected was not found.
    /// </summary>
    /// <remarks>
    /// Be sure to include <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
    /// in a library that will be injected.
    /// </remarks>
    RuntimeConfigNotFound = 3,

    /// <summary>
    /// The specified class or type was not found.
    /// </summary>
    /// <remarks>
    /// Be sure to put it in this format: "namespace.type.method, assembly",
    /// see samples.
    /// </remarks>
    ClassOrMethodNotFound = 4
}
\ No newline at end of file

M src/Inject/NosSmooth.Injector/NosInjector.cs => src/Inject/NosSmooth.Injector/NosInjector.cs +5 -1
@@ 150,12 150,16 @@ public class NosInjector
            }

            var functionResult = injector.CallFunction(nosSmoothInjectPath, "LoadAndCallMethod", loadParams);

            injector.Eject(nosSmoothInjectPath);

            if (functionResult != 1)
            {
                return new InjectionFailedError
                (
                    dllPath,
                    $"Couldn't initialize the nethost or call the main function, did you specify the class and method correctly? Result: {functionResult}"
                    $"Couldn't initialize the nethost or call the main function, did you specify the class and method correctly? Result: {functionResult}",
                    (InjectionResult)functionResult
                );
            }


M src/Inject/NosSmooth.Injector/NosSmooth.Inject.dll => src/Inject/NosSmooth.Injector/NosSmooth.Inject.dll +0 -0
M src/Inject/NosSmooth.Injector/NosSmooth.Injector.csproj => src/Inject/NosSmooth.Injector/NosSmooth.Injector.csproj +3 -3
@@ 5,8 5,8 @@
    <AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb;.dll</AllowedOutputExtensionsInPackageBuildOutputFolder>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <VersionPrefix>1.0.3</VersionPrefix>
    <PackageReleaseNotes>Support relative path when injecting. Inluce NosSmooth.Inject.dll in package and copy it to output.</PackageReleaseNotes>
    <VersionPrefix>1.1.0</VersionPrefix>
    <PackageReleaseNotes>Remove asserts from Inject and obtain errors from thread exit code.</PackageReleaseNotes>
  </PropertyGroup>

  <ItemGroup>


@@ 21,7 21,7 @@

  <ItemGroup>
    <Content Include="NosSmooth.Inject.dll" Pack="true" PackageCopyToOutput="true">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>

Do not follow this link