gpt4 book ai didi

c# - 如何调试使用自定义 .NET Core 2 主机加载的程序集?

转载 作者:行者123 更新时间:2023-12-02 10:24:26 27 4
gpt4 key购买 nike

为了使用托管附加功能扩展 native 应用程序,我创建了一个自定义 .NET Core 2.0 主机,与 official docs 中描述的非常相似。 .

到目前为止,它运行良好并加载了我的程序集。它还成功运行了ICLRRuntimeHost2.CreateDelegate 委托(delegate)给的方法。 ;为了测试,我用 System.IO.File.WriteAllText 将“Hello World”写入了一个新文件中。与预期的内容一起出现。

但是,我对如何在加载的程序集中实际调试托管代码感到有些困惑。

  • 简单地在那里设置断点不会触发。
  • 我还尝试搜索特定的与调试相关的属性值对以传递给 ICLRRuntimeHost2.CreateAppDomainWithManager方法,但没有找到。
  • 调用Debugger.Break确实会导致 MSVC++ 调试器中断(“bla.exe 已触发断点。”),但不会进入我的托管代码或理解其中的任何内容。我想这只是破坏调试器的一种常见的系统原生方式。
  • 或者是否有类似 Python 中已知的“鸡蛋文件”之类的东西,可以让 Visual Studio 将托管代码与调试器“连接”起来?

  • 如前所述,我的代码与文档非常相似,在这里供引用。 Run基本上加载 coreclr 库,从中获取 GetClrRuntimeHost 函数,实例化应用程序域并运行委托(delegate)的托管代码。如果少数调用的额外方法或使用的成员的实现有任何问题,我会根据要求添加它们。
    void ClrHost::Run(wstring const& assembly, wstring const& type, wstring const& method)
    {
    // Load the CoreCLR.dll and retrieve the GetCLRRuntimeHost function.
    wstring coreClrFilePath = _runtimeFolder / "coreclr.dll";
    HMODULE coreClr = LoadLibraryExW(coreClrFilePath.c_str(), NULL, 0);
    if (!coreClr)
    throw new ClrHostException(L"Could not load CoreCLR.dll.");
    FnGetCLRRuntimeHost fnGetClrRuntimeHost = (FnGetCLRRuntimeHost)GetProcAddress(coreClr, "GetCLRRuntimeHost");
    if (!fnGetClrRuntimeHost)
    throw new ClrHostException(L"Could not find GetCLRRuntimeHost function.");

    // Instantiate and set up a runtime host.
    if (FAILED(fnGetClrRuntimeHost(IID_ICLRRuntimeHost2, (IUnknown**)&_runtimeHost)))
    throw new ClrHostException(L"Could not retrieve ICLRRuntimeHost2 instance.");
    STARTUP_FLAGS startupFlags = static_cast<STARTUP_FLAGS>(
    STARTUP_FLAGS::STARTUP_CONCURRENT_GC
    | STARTUP_FLAGS::STARTUP_SINGLE_APPDOMAIN
    | STARTUP_FLAGS::STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN);
    if (FAILED(_runtimeHost->SetStartupFlags(startupFlags)))
    throw new ClrHostException(L"Could not set runtime host startup flags.");
    if (FAILED(_runtimeHost->Start()))
    throw new ClrHostException(L"Could not start runtime host.");

    // Instantiate the AppDomain with the configured settings.
    int appDomainFlags = APPDOMAIN_ENABLE_PLATFORM_SPECIFIC_APPS
    | APPDOMAIN_ENABLE_PINVOKE_AND_CLASSIC_COMINTEROP
    | APPDOMAIN_DISABLE_TRANSPARENCY_ENFORCEMENT;
    wstring tpaAssemblies = ConcatenatePaths(GetAssembliesFromFolder(_runtimeFolder));
    wstring assemblyFolders = ConcatenatePaths(_assemblyFolders);
    wstring nativeLibFolders = ConcatenatePaths(_nativeLibFolders);
    LPCWSTR propertyKeys[] = {
    L"TRUSTED_PLATFORM_ASSEMBLIES",
    L"APP_PATHS",
    L"APP_NI_PATHS",
    L"NATIVE_DLL_SEARCH_DIRECTORIES",
    L"PLATFORM_RESOURCE_ROOTS",
    L"AppDomainCompatSwitch"
    };
    LPCWSTR propertyValues[] = {
    tpaAssemblies.c_str(),
    assemblyFolders.c_str(),
    assemblyFolders.c_str(),
    nativeLibFolders.c_str(),
    assemblyFolders.c_str(),
    L"UseLatestBehaviorWhenTFMNotSpecified"
    };
    if (FAILED(_runtimeHost->CreateAppDomainWithManager(L"IDA.NET AppDomain", appDomainFlags, NULL, NULL,
    sizeof(propertyKeys) / sizeof(LPCWSTR), propertyKeys, propertyValues, &_appDomainID)))
    {
    throw new ClrHostException(L"Could not create AppDomain.");
    }

    // Get a delegate for the managed static method.
    void *fnDelegate = NULL;
    HRESULT hr = _runtimeHost->CreateDelegate(_appDomainID, assembly.c_str(), type.c_str(), method.c_str(),
    (INT_PTR*)&fnDelegate);
    if (FAILED(hr))
    throw new ClrHostException(L"Could not run " + type + L"." + method + L" in " + assembly + L".");

    // Execute the managed code.
    ((RunSignature*)fnDelegate)();
    }

    最佳答案

    我刚刚意识到它比预期的要简单!

    现在,我总是使用我的原生 C++ 项目(承载 .NET Core 的项目)作为 Visual Studio 中的启动项目。

    但是,通过创建启动配置文件来启动外部 native 可执行文件而不是“启动”我的库,将启动项目更改为托管 C# 库,可以调试和逐步执行托管代码。

    您可以在“Debug”下的项目属性中设置这样的启动配置文件,或者添加一个典型的.NET Core Properties\launchSettings.json到您的托管项目根目录,存储如下内容:

    {
    "profiles": {
    "Any profile name (typically the project name)": {
    "commandName": "Executable",
    "executablePath": "C:\\FullNativeExecutablePath\\AndFileName.exe",
    "workingDirectory": "C:\\FullNativeExecutablePath"
    }
    }
    }

    关于c# - 如何调试使用自定义 .NET Core 2 主机加载的程序集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49945692/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com