Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#中创建的DLL,试图导入到另一个C#项目(DLL不能位于根目录上) 在几个小时的搜索和捆绑我能找到的东西之后(嗨),因为我能找到的是C++上的DLL进入C项目,而这不是我想要的。_C#_Dllimport_Dllexport - Fatal编程技术网

在C#中创建的DLL,试图导入到另一个C#项目(DLL不能位于根目录上) 在几个小时的搜索和捆绑我能找到的东西之后(嗨),因为我能找到的是C++上的DLL进入C项目,而这不是我想要的。

在C#中创建的DLL,试图导入到另一个C#项目(DLL不能位于根目录上) 在几个小时的搜索和捆绑我能找到的东西之后(嗨),因为我能找到的是C++上的DLL进入C项目,而这不是我想要的。,c#,dllimport,dllexport,C#,Dllimport,Dllexport,我正在尝试运行该程序,但希望DLL文件位于另一个目录中(例如,程序位于c:\Programs\program上,DLL位于c:\Libs上) 我在C#中创建了一个DLL,其中有各种方法是公共的,我希望在我的程序中可以访问这些方法 DLL我有如下内容: [ComVisible(true)] [Guid("6E873AA1-1AD6-4325-BBFD-EF22A3EB80CC")] public interface ITraducoes { [ComVisible(true)] v

我正在尝试运行该程序,但希望DLL文件位于另一个目录中(例如,程序位于c:\Programs\program上,DLL位于c:\Libs上)

我在C#中创建了一个DLL,其中有各种方法是公共的,我希望在我的程序中可以访问这些方法

DLL我有如下内容:

[ComVisible(true)]
[Guid("6E873AA1-1AD6-4325-BBFD-EF22A3EB80CC")]
public interface ITraducoes
{
    [ComVisible(true)]
    void changeLang(String lang);
    [ComVisible(true)]
    void translate(Control form);
    [ComVisible(true)]
    string toolTipText(Control form, String toolTip);
    [ComVisible(true)]
    string[] listText(Control form, String lista);
    [ComVisible(true)]
    string[] getMessagesArray();
    [ComVisible(true)]
    Hashtable getMessagesTable();
}
#region Traducoes Class
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("AE53E0A3-8261-4706-8861-0249F7390642")]
//[ProgId("ITranslate.Traducoes")]
public class Traducoes : ITraducoes
{...
[DllImport("ITranslate.dll", CallingConvention = CallingConvention.Cdecl)]
extern static void translate(Control form);
[DllImport("ITranslate.dll")]
extern static string[] getMessagesArray();
public Form1(string[] args){
    InitializeComponent();                  
    translate(this);
    msg = getMessagesArray();
在节目中我有这样的东西:

[ComVisible(true)]
[Guid("6E873AA1-1AD6-4325-BBFD-EF22A3EB80CC")]
public interface ITraducoes
{
    [ComVisible(true)]
    void changeLang(String lang);
    [ComVisible(true)]
    void translate(Control form);
    [ComVisible(true)]
    string toolTipText(Control form, String toolTip);
    [ComVisible(true)]
    string[] listText(Control form, String lista);
    [ComVisible(true)]
    string[] getMessagesArray();
    [ComVisible(true)]
    Hashtable getMessagesTable();
}
#region Traducoes Class
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("AE53E0A3-8261-4706-8861-0249F7390642")]
//[ProgId("ITranslate.Traducoes")]
public class Traducoes : ITraducoes
{...
[DllImport("ITranslate.dll", CallingConvention = CallingConvention.Cdecl)]
extern static void translate(Control form);
[DllImport("ITranslate.dll")]
extern static string[] getMessagesArray();
public Form1(string[] args){
    InitializeComponent();                  
    translate(this);
    msg = getMessagesArray();
然后我这样叫他:

[ComVisible(true)]
[Guid("6E873AA1-1AD6-4325-BBFD-EF22A3EB80CC")]
public interface ITraducoes
{
    [ComVisible(true)]
    void changeLang(String lang);
    [ComVisible(true)]
    void translate(Control form);
    [ComVisible(true)]
    string toolTipText(Control form, String toolTip);
    [ComVisible(true)]
    string[] listText(Control form, String lista);
    [ComVisible(true)]
    string[] getMessagesArray();
    [ComVisible(true)]
    Hashtable getMessagesTable();
}
#region Traducoes Class
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("AE53E0A3-8261-4706-8861-0249F7390642")]
//[ProgId("ITranslate.Traducoes")]
public class Traducoes : ITraducoes
{...
[DllImport("ITranslate.dll", CallingConvention = CallingConvention.Cdecl)]
extern static void translate(Control form);
[DllImport("ITranslate.dll")]
extern static string[] getMessagesArray();
public Form1(string[] args){
    InitializeComponent();                  
    translate(this);
    msg = getMessagesArray();
我希望我没有错过任何东西,但如果我错过了,我会尽我所能提供它

编辑

忘了说,我犯了这个错误

在DLL“ITranslate.DLL”中找不到名为“translate”的入口点


不能使用DllImport引用C#库

相反,在项目中添加对DLL的引用,并将其与任何其他引用(例如,
System.Windows.Forms
等)一样使用


您不必让类
ComVisible
或任何东西—所有.NET程序集都公开其所有对象和元数据(使用
private
和此类关键字有一定的限制,但最终,您也可以通过一些工作获得这些对象和元数据).

你的问题是什么?你不能在你的项目中引用dll吗?我正在尝试导入dll,通常我们只是添加一个引用(然后它将复制到项目的根目录)。我试图将DLL放在不同的根目录(不是项目的根目录)是的,但是添加一个引用,它会将DLL复制到项目的根目录中。但我不想那样。例如,相同的DLL可能被其他程序使用,并且需要在程序目录中具有相同的DLL。我想要的是共享该DLL,让DLL位于ex.c:\Libs上,程序去那里获取它。@user3310091您可以使用
AssemblyResolve
事件或应用程序清单来实现这一点。或者更好的是,用SNK在你的库上签名,这样就可以直接从GAC那里使用。请看一下.NET程序集背后的思想,您正在尝试解决旧问题旧方法-.NET旨在解决DLL地狱问题(它的成功程度是一个热烈争论的问题:D)谢谢,可以提供一个例子吗D@user3310091尝试使用这个app.config/web.config部分-包括一个示例。包含DLL的目录也必须位于程序目录的根目录上(在问这个问题之前,我确实查看了一下:D)