可当通过COM叫我用/访问来自.NET code,在app.config我用、可当、COM、NET

2023-09-04 00:11:04 作者:仰起头不流泪

我有,我想从Excel VBA调用(这部分是工作的罚款).NET库的现有集。这些库依赖于在app.config设置。我知道我可以进入一个excel.exe.config文件(放在同一个目录中EXCEL.EXE)这些设置,但是这并没有真正似乎是一个非常易于管理的解决方案给我,因为我可以看到造成冲突,如果多个应用程序要做到这一点。

我的问题很简单:有什么办法COM暴露的dll指的是各自的配置文件?

样的app.config:

 < XML版本=1.0编码=UTF-8&GT?;
<结构>
  <的appSettings>
    <添加键=测试值=世界,你好! />
  < /的appSettings>
< /结构>
 

C#示例:

 命名空间ExcelVbaFacing code
{
    公共类SimpleAppSettingReader
    {
        公共字符串的GetValue(字符串键)
        {
            返回ConfigurationManager.AppSettings [关键];
        }
    }
}
 

示例VBA:

 公用Sub测试()
    昏暗测试作为新SimpleAppSettingReader
    昏暗sampleValue作为字符串
    sampleValue = Test.GetValue(测试)
    MSGBOX值:+ sampleValue +',vbOKOnly
结束小组
 
自己如何搭建服务器

解决方案

您确实可以指定将要使用的.NET配置API的配置文件,但你必须这样做,在创建应用程序域之前(这是不一样容易,因为它的声音。)这是ASP.NET如何工作的例子。它设置为使用位于虚拟目录下的web.config文件中的AppDomain,而不是需要一个w3p.exe.config或什么不是。

您可以做到这一点,但你需要创建一个次要的AppDomain。这本身就是一个很大的有做多的加载项和程序集引用的理由是一个好主意。但事情可以放在 AppDomain中的设置是的app.config文件的名称/路径。

要创建新的AppDomain,您可以使用一个垫片,这是一个有点样板C ++ code的配置并启动的AppDomain和的code从这一点上休息,可管理code。

有MSDN上写的一篇文章由安德鲁白教堂,并呼吁的隔离办公室扩展与COM沉向导。我不会撒谎,这不是一个简单的概念,但也不算太糟糕。有创建的C ++项目为您哪些加载.NET插件到一个新的AppDomain一个向导。前必须先加载它,所以你会希望把按照向导创建的code完成的应用程序域的配置。

  // CLRLoader.cpp
HRESULT CCLRLoader :: CreateLocalAppDomain()
{

    ...喀嚓...

    //配置的AppDomain使用特定的配置文件
    pDomainSetup-> put_ConfigurationFile(的CComBSTR(szConfigFile));

    ...喀嚓...

}
 

另外值得一提的是,如果/当你转换为VSTO项目,VSTO运行库做这一切的AppDomain配置为您服务。每个VSTO插件运行在自己的AppDomain拥有自己的私人app.config文件。

I have an existing set of .net libraries that I wish to call from Excel VBA (that part is working fine). These libraries rely on settings in the app.config. I know I can enter these settings in a excel.exe.config file (placed in the same directory as the excel.exe), but this doesn't really seem like a very manageable solution to me, as I can see causing conflicts if more than one application wants to do this.

My question is simple: is there any way of COM exposed dlls referring to their respective config files?

Sample app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="test" value="Hello world!" />
  </appSettings>
</configuration>

Sample c#:

namespace ExcelVbaFacingCode
{
    public class SimpleAppSettingReader
    {
        public string GetValue(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }
    }
}

Sample VBA:

Public Sub Test()
    Dim Test As New SimpleAppSettingReader
    Dim sampleValue As String
    sampleValue = Test.GetValue("test")
    MsgBox "Value: '" + sampleValue + "'", vbOKOnly
End Sub

解决方案

You can indeed specify the configuration file that will be used by the .NET configuration API's but you have to do it before the AppDomain is created (which is not as easy as it sounds.) This is how ASP.NET works for example. It sets up the AppDomain to use a "web.config" file located in the virtual directory as opposed to requiring an w3p.exe.config or what not.

You can do this too but you need to create a secondary AppDomain. This in itself is a good idea for a lot of reasons that have to do with multiple add-ins and assembly references. But one of the things you can put in the AppDomain's setup is the name/path of the "app.config" file.

To create the new AppDomain you can use a "shim" which is a bit of boilerplate C++ code that configures and starts the AppDomain and the rest of the code from that point on can be managed code.

There's an article on MSDN written by Andrew Whitechapel and called Isolating Office Extensions with the COM Shim Wizard. I won't lie, it's not a trivial concept, but it's also not too bad. There's a wizard that creates the C++ project for you which loads the .NET addin into a new AppDomain. The configuration of the AppDomain must be done before it is loaded so you'll want to put the following in the wizard-created code.

// CLRLoader.cpp
HRESULT CCLRLoader::CreateLocalAppDomain()
{

    ... snip ...

    // Configure the AppDomain to use a specific configuration file
    pDomainSetup->put_ConfigurationFile(CComBSTR(szConfigFile));

    ... snip ...

}

It's also worth noting that if/when you convert to a VSTO project, the VSTO runtime does all this AppDomain configuration for you. Each VSTO addin runs in its own AppDomain with its own private app.config file.