我有这样的情况:
它是这样订购的:
问题是,如果托管DLL无法从插件DLL加载,除非它位于可执行的dir (app1,app2)中。我非常希望它位于插件 dir中,因为从逻辑上讲,它应该从插件中使用,而不是从应用程序中使用,而且为了避免重复,插件目录不能作为应用程序的子目录,因为我有两个应用程序。
.NET应用程序可以配置一个.config文件,以启用来自其他目录的程序集绑定,但这还是一个C++应用程序。此外,我还读过有关配置文件的文章,它写到,它只能用于子目录。我引用:(从这个链接)
privatePath必需属性。 指定可能包含程序集的应用程序基目录的子目录。用分号分隔每个子目录。
澄清一下:我在插件中运行其他DLL(插件目录位于%PATH%环境中)没有问题,我只是加载托管DLL时遇到了问题。
因此,我的问题是:如何使托管DLL能够从自定义dir中运行,而这个dir不是可执行文件的子目录?
我正在寻找一个工作的解决方案,没有重复的文件或目录。
示例:
C# (托管) DLL
using System;
namespace PrintSample
{
public class CPrintSample
{
void print()
{
Console.WriteLine("Hello");
}
}
}CLR (参考在预处理器中定义的C# PrintSample添加和CLR_PRINT_SAMPLE_EXPORT )
clr_sample.h
#pragma once
#ifdef CLR_PRINT_SAMPLE_EXPORT
#define CLR_PS_API __declspec(dllexport)
#else
#define CLR_PS_API __declspec(dllimport)
#endif
class IPrintSample
{
public:
virtual void Print()=0;
};
CLR_PS_API IPrintSample* Factory();clr_sample.cpp
#include <msclr/marshal.h>
#include <msclr/auto_gcroot.h>
#include "clr_sample.h"
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace msclr::interop;
using namespace PrintSample;
class CClrPrintSample : public IPrintSample
{
msclr::auto_gcroot<PrintSample::CPrintSample^> bridge;
public:
CClrPrintSample ()
{
bridge = gcnew PrintSample::CPrintSample();
}
~CClrPrintSample ()
{
delete bridge;
GC::Collect();
}
void Print()
{
bridge->print();
}
};
IPrintSample* CLR_PS_API Factory()
{
return new CClrPrintSample();
}c++ (控制台应用程序)(链接到clr项目)
#include "clr_sample.h"
int main()
{
IPrintSample* ps = Factory();
ps->Print();
delete ps;
return 0;
}发布于 2022-02-16 08:07:19
您可以将plugins dir创建为app1/plugins & app2/plugins的符号链接。但它只能在NTFS上使用。见mklink.exe医生。
你能接受这个解决方案吗?
https://stackoverflow.com/questions/50371778
复制相似问题