从非托管c ++代码调用托管c#组件,我该如何定义config

时间:2021-10-02 20:59:41

I want to call a C# component from an unmanaged c++ service. I need to set config items expected by the C# component. What name should be config be and where should it be located.

我想从非托管的c ++服务中调用C#组件。我需要设置C#组件所需的配置项。应该配置什么名称以及它应该位于何处。

eg. parentfolder\cplusplusservice.exe

anotherfolder\csharp.dll

i need csharp.dll.config or its equivalent.

我需要csharp.dll.config或它的等价物。

Thanks,

3 个解决方案

#1


The .config file needs to be named the same as the application's name. When the unmanaged code first calls into the managed code an AppDomain is created that AppDomain controls which config file is loaded and from where.

.config文件的名称必须与应用程序的名称相同。当非托管代码首次调用托管代码时,会创建一个AppDomain,AppDomain控制加载哪个配置文件以及从哪里加载。

You will need to manually copy the .config file to the output directory and name it after the application name or create a post-build to do so.

您需要手动将.config文件复制到输出目录并在应用程序名称后面命名,或者创建一个后期构建来执行此操作。

Basically the same rules that apply to where .config files need to be for pure managed apps apply to unmanaged apps that load managed code.

基本上适用于纯托管应用程序所需的.config文件的规则适用于加载托管代码的非托管应用程序。

#2


This article shows how to use Configuration files with c#:

本文介绍如何使用c#配置文件:

http://www.devasp.net/net/articles/display/679.html

This article provides an overview of calling managed c# component from unmanaged c++ and vice versa, as well as some helper classes:

本文概述了从非托管c ++调用托管c#组件,反之亦然,以及一些帮助器类:

http://www.codeproject.com/KB/mcpp/unmanaged_to_managed.aspx

#3


It depends on how the AppDomain that is hosting your managed code is created.

这取决于托管托管代码的AppDomain是如何创建的。

For example, if you are exposing the managed code as COM, it will be loaded into the default AppDomain, and the configuration file will be in the same directory as the executable (parentfolder\cplusplusservice.exe.config in your case).

例如,如果您将托管代码公开为COM,它将被加载到默认的AppDomain中,配置文件将与可执行文件位于同一目录中(在您的情况下为parentfolder \ cplusplusservice.exe.config)。

If you are creating the AppDomain yourself, you can control where the configuration file is located.

如果您自己创建AppDomain,则可以控制配置文件的位置。

#1


The .config file needs to be named the same as the application's name. When the unmanaged code first calls into the managed code an AppDomain is created that AppDomain controls which config file is loaded and from where.

.config文件的名称必须与应用程序的名称相同。当非托管代码首次调用托管代码时,会创建一个AppDomain,AppDomain控制加载哪个配置文件以及从哪里加载。

You will need to manually copy the .config file to the output directory and name it after the application name or create a post-build to do so.

您需要手动将.config文件复制到输出目录并在应用程序名称后面命名,或者创建一个后期构建来执行此操作。

Basically the same rules that apply to where .config files need to be for pure managed apps apply to unmanaged apps that load managed code.

基本上适用于纯托管应用程序所需的.config文件的规则适用于加载托管代码的非托管应用程序。

#2


This article shows how to use Configuration files with c#:

本文介绍如何使用c#配置文件:

http://www.devasp.net/net/articles/display/679.html

This article provides an overview of calling managed c# component from unmanaged c++ and vice versa, as well as some helper classes:

本文概述了从非托管c ++调用托管c#组件,反之亦然,以及一些帮助器类:

http://www.codeproject.com/KB/mcpp/unmanaged_to_managed.aspx

#3


It depends on how the AppDomain that is hosting your managed code is created.

这取决于托管托管代码的AppDomain是如何创建的。

For example, if you are exposing the managed code as COM, it will be loaded into the default AppDomain, and the configuration file will be in the same directory as the executable (parentfolder\cplusplusservice.exe.config in your case).

例如,如果您将托管代码公开为COM,它将被加载到默认的AppDomain中,配置文件将与可执行文件位于同一目录中(在您的情况下为parentfolder \ cplusplusservice.exe.config)。

If you are creating the AppDomain yourself, you can control where the configuration file is located.

如果您自己创建AppDomain,则可以控制配置文件的位置。