简体   繁体   English

在 C# 程序中调用 C# DLL,未找到入口点

[英]Calling a C# DLL in a C# program, entry point not found

I am using the following C# DLL.我正在使用以下 C# DLL。

    using System;

namespace LibraryTest
{
    public class Class1
    {
        public int callNr(int nr)
        {
            if (nr == 5)
            {
                return 555;
            }
            return 0;
        }
    }
}

And using it like this in the program:并在程序中像这样使用它:

    using System;
using System.Runtime.InteropServices;

namespace Builder.Store
{
    public class testIndicator : Indicator
    {
        [DllImport(@"C:\LibraryTest.dll", EntryPoint = "callNr")]
        public static extern int callNr(int nr);
        
        public override void Calculate()
        {
            int value = callNr(5);
            
            //do stuff...
        }
    }
}

Only result is "unable to find entry point in DLL" error.唯一的结果是“无法在 DLL 中找到入口点”错误。 My research: I don't have dumpbin in my VS, but I used dotPeek, the result is that the DLL matches the source code.我的研究:我的 VS 中没有 dumpbin,但是我使用了 dotPeek,结果是 DLL 与源代码匹配。 I used Dependency Walker, DLL seems to be fine but it is not pointing out an entry point, screenshot attached.我使用了 Dependency Walker,DLL 似乎很好,但它没有指出入口点,附上截图。

https://i.stack.imgur.com/RR4UH.jpg https://i.stack.imgur.com/RR4UH.jpg

The program I'm using is a stand-alone third party software, which allows custom file inputs (I can't add DLL references to the actual program).我使用的程序是独立的第三方软件,它允许自定义文件输入(我无法向实际程序添加 DLL 引用)。 I'm at my wits' end.我不知所措。 Any pointers and/or obvious mistakes?任何指针和/或明显的错误?

As mentioned in comments DllImport is used when interacting with native code.正如评论中提到的,在与本机代码交互时使用DllImport

To call a method in a .NET Dll you have to do the following:要在 .NET Dll 中调用方法,您必须执行以下操作:

// First load the assembly
var assembly = Assembly.LoadFrom(@"C:\LibraryTest.dll");

// Get the type that includes the method you want to call by name (must include namespace and class name)
var class1Typetype = assembly.GetType("LibraryTest.Class1");

// Since your method is not static you must create an instance of that class.
// The following line will create an instance using the default parameterless constructor.
// If the class does not have a parameterless constructor, the following line will faile
var class1Instance = Activator.CreateInstance(class1Typetype);

// Find the method you want to call by name
// If there are multiple overloads, use the GetMethod overload that allows specifying parameter types
var method = class1Typetype.GetMethod("callNr");

// Use method.Invoke to call the method and pass the parameters in an array, cast the result to int
var result = (int)method.Invoke(class1Instance, new object[] { 5 });

This technique of calling methods by name is called "Reflection".这种按名称调用方法的技术称为“反射”。 You can google the term "C# Reflection" to find many resources explaining how it works.您可以在谷歌上搜索术语“C# 反射”以找到许多解释其工作原理的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM