📜  hostbuilder添加环境变量——C编程语言(1)

📅  最后修改于: 2023-12-03 15:15:32.538000             🧑  作者: Mango

在C编程语言中使用HostBuilder添加环境变量

在开发应用程序时,我们通常需要访问环境变量来读取或设置配置信息。在C编程语言中,可以使用HostBuilder来方便地添加和管理环境变量。

HostBuilder简介

HostBuilder是在.NET Core 2.1中引入的一种构建和配置应用程序的方式,它提供了许多有用的功能,例如配置日志、添加服务和配置环境变量等。

使用HostBuilder可以将应用程序与.NET Core Runtime分离,并使用一组配置文件和环境变量来配置应用程序,从而使它更加灵活和可配置。

如何添加环境变量

添加环境变量可以使用ConfigureEnvironment方法,该方法接受一个Action委托,该委托可以将一个IConfigurationBuilder对象传递给它,使其能够添加环境变量。

下面是一个简单的示例,展示如何添加一个名为“MY_VAR”的环境变量,其值为“Hello World!”:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>

#include <limits.h>
#include <stdint.h>
#include <stdarg.h>

#include <netinet/in.h>

#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>

#include <errno.h>

#include "coreclrhost.h"

int main(int argc, char **argv)
{
    char *dotnet_path = "/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.0/"; // replace with your dotnet core runtime path
    char *dll_path = "/path/to/your/dll"; // replace according to your requirements

    void *libcoreclr;
    int (*create_delegate)(void*, const char*, const char*, const char*, void**);
    int (*execute_assembly)(void*, int, const char**, const char**, int*);

    char tpa_path[2048];
    char app_path[2048];
    char dotnet_cmd[2048];

    sprintf(tpa_path, "%s/%s", dotnet_path, "mscorlib.dll");
    sprintf(tpa_path + strlen(tpa_path), ":%s/%s", dotnet_path, "System.Private.CoreLib.dll");
    sprintf(tpa_path + strlen(tpa_path), ":%s/%s", dotnet_path, "System.Runtime.dll");
    sprintf(tpa_path + strlen(tpa_path), ":%s/%s", dotnet_path, "System.Console.dll");
    sprintf(tpa_path + strlen(tpa_path), ":%s", dll_path);

    sprintf(app_path, "%s/%s", dll_path, "entrypoint.dll");

    putenv("CORECLR_PATH=/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.0/");

    struct coreclr_initialize_params init_params;
    memset(&init_params, 0, sizeof(init_params));
    init_params.size = sizeof(struct coreclr_initialize_params);
    init_params.domain_id = 1;
    init_params.assembly_paths = tpa_path;
    init_params.assembly_naming_scheme = "dnlib";
    init_params.managed_assembly_search_paths = dll_path;
    init_params.callback_hooks = NULL; 

    void* host_handle;
    unsigned int domain_id;

    int hr = coreclr_initialize(NULL, "SampleHost", &init_params, &host_handle, &domain_id);

    if (hr >= 0)
    {
        void *delegate_handle = NULL;
        create_delegate = (int (*)(void*, const char*, const char*, const char*, void**))coreclr_get_function_pointer(host_handle, &create_delegate, "coreclr_create_delegate");
        execute_assembly = (int (*)(void*, int, const char**, const char**, int*))coreclr_get_function_pointer(host_handle, &execute_assembly, "coreclr_execute_assembly");

        hr = create_delegate(host_handle, domain_id, "entrypoint", "EntryPoint", (void**)&delegate_handle);

        if (hr >= 0)
        {
            const char* arg_keys[] = { "MY_VAR" };
            const char* arg_values[] = { "Hello World!" };
            int exit_code = 0;
            hr = execute_assembly(host_handle, domain_id, 1, arg_keys, arg_values, &exit_code);

            if (hr < 0)
            {
                printf("Failed to execute assembly, hr=%d.\n", hr);
            }
        }
        else
        {
            printf("Failed to create delegate, hr=%d.\n", hr);
        }
    }
    else
    {
        printf("Failed to initialize CoreCLR, hr=%d.\n", hr);
    }

    coreclr_shutdown(host_handle, domain_id);

    return 0;
}

请注意,putenv函数被用于添加System.Runtime.Loader环境变量。这将告诉.NET Core应用程序加载程序如何解析程序集接口。

总结

通过使用HostBuilder,我们可以方便地添加和管理环境变量,并使应用程序更加灵活和可配置。在开发.NET Core应用程序时,强烈建议使用这一功能,以便更好地管理应用程序的配置信息。