从 DLL 中调用函数?

发布于 2024-10-17 17:16:59 字数 440 浏览 1 评论 0原文

我是 C# 新手,我正在尝试学习 DLL 的使用。我试图将我的对象包装在 DLL 中,然后在我的程序中使用它。

public class Foo   // its in the DLL
{
   public void Bar()
   {
      SomeMethodInMyProgram();
   } 
}

所以我尝试将其打包到 DLL 中,但我不能,因为编译器不知道 SomeMethodInMyProgram() 是什么。

我想这样使用它:

class Program // my program, using DLL
{
    static void Main(string[] args)
    {
       Foo test = new Foo();
       test.Bar();
    }
 } 

I'm new to C# and I'm trying to learn to usage of DLLs. I'm trying to wrap my objects in a DLL, and then use it in my program.

public class Foo   // its in the DLL
{
   public void Bar()
   {
      SomeMethodInMyProgram();
   } 
}

So I try to pack this to a DLL but I can't, because compiler doesn't know what the SomeMethodInMyProgram() is.

I would like to use it like:

class Program // my program, using DLL
{
    static void Main(string[] args)
    {
       Foo test = new Foo();
       test.Bar();
    }
 } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄意 2024-10-24 17:16:59

取决于什么类型的 DLL。这是内置于 .NET 中的吗?如果它是非托管代码,那么这里是一个示例,否则 Rob 的答案将起作用。

非托管 C++ dll 示例

using System;
using System.Runtime.InteropServices;

您可能需要使用 DllImport

[DllImport(@"C:\Cadence\SPB_16.5\tools\bin\mpsc.dll")]
static extern void mpscExit();

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

然后每个都按如下方式调用:

// a specific DLL method/function call
mpscExit();
// user32.dll is Microsoft, path not needed
MessageBox(new IntPtr(0), "Test", "Test Dialog", 0);  

Depends on what type of DLL. Is this built in .NET ? if it is unmanaged code then here is an example otherwise the Answer from Rob will work.

Unmanaged C++ dll example:

using System;
using System.Runtime.InteropServices;

You may need to use DllImport

[DllImport(@"C:\Cadence\SPB_16.5\tools\bin\mpsc.dll")]
static extern void mpscExit();

or

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

Then each of those are called like this:

// a specific DLL method/function call
mpscExit();
// user32.dll is Microsoft, path not needed
MessageBox(new IntPtr(0), "Test", "Test Dialog", 0);  
貪欢 2024-10-24 17:16:59

通过解决方案资源管理器添加 DLL - 右键单击​​引用 -->添加引用,然后“浏览”到您的 DLL - 那么它应该可用。

Add the DLL via the solution explorer - right click on references --> add reference then "Browse" to your DLL - then it should be available.

花开半夏魅人心 2024-10-24 17:16:59

我参加这里的聚会迟到了,但我把这个答案留给像我这样拔头发的人。所以基本上,在面对这个问题时,我没有 VS IDE 的奢侈。我试图使用 csc 通过 cmdline 编译代码。为了引用 dll,只需将编译器标志 /r:PathToDll/NameOfTheDll 添加到 csc 即可。

该命令看起来像

csc /r:PathToDll/NameOfTheDll /out:OutputExeName FileWhichIsReferencingTheDll.cs

在 FileWhichIsReferencingTheDll.cs 中添加 using namespace AppropriateNameSpace; 来访问函数(如果是静态或静态,则通过调用 class.functionName通过创建该类的对象并调用该对象上的函数)。

I am late to the party here but am leaving this answer for someone pulling his/her hair out like me. So basically, I did not have the luxury of VS IDE when facing this issue.I was trying to compile the code via cmdline using csc. In order to reference a dll, just add the compiler flag /r:PathToDll/NameOfTheDll to csc.

The command would look like

csc /r:PathToDll/NameOfTheDll /out:OutputExeName FileWhichIsReferencingTheDll.cs

In FileWhichIsReferencingTheDll.cs add using namespace AppropriateNameSpace; to access the functions (by calling class.functionName if static or by creating an object of the class and invoking the function on the object).

美羊羊 2024-10-24 17:16:59

您需要在运行时实际将 DLL 加载到应用程序中,即 DLL 的动态部分。您还需要定义 DLL 中的函数的头文件,以便您的编译知道已定义了哪些函数。我这里的知识是基于 C++ 的,所以这对于 C# 是如何工作的我不确定,但它会是这样的......

you need to actually load the DLL into your application at run time, thus the Dynamic part of DLL. You also need the header file that defines what functions are in the DLL so your compile knows what functions have been defined. My knowledge here is based on C++ so how this works for C# I am not to sure, but it will be something like that...

少跟Wǒ拽 2024-10-24 17:16:59

这是我的 DLL (AllInOne) 源代码,其中有一个名为Calculate 的类,该类具有 GetAreaofSquare 方法。

namespace AllInOne
{
    public class Calculate
    {   
        public double GetAreaOfSquare(double side)
        {
            return side * side;
        }
    }
}

我已在项目的解决方案资源管理器(它是一个控制台应用程序)中的引用中添加了此 DLL,并在系统命名空间中添加了 AllInOne。请仔细阅读“使用AllInOne”。我们可以实例化如下所示的Calculate类,然后可以使用GetAreaofSquare方法来计算Square的面积。

using AllInOne;

namespace UsingDLLinApplication
{
    public class GetResult
    {
        static void Main()
        {
            Calculate myEveryCalculation = new Calculate();
            double storeAreaOFSquare = myEveryCalculation.GetAreaOfSquare(4.5);
            Console.WriteLine("The area of Square is {0}", storeAreaOFSquare);
            Console.ReadLine();
        }
     }
}

This is my source code for DLL (AllInOne) having a class named Calculate which has a method GetAreaofSquare.

namespace AllInOne
{
    public class Calculate
    {   
        public double GetAreaOfSquare(double side)
        {
            return side * side;
        }
    }
}

I have added this DLL in the reference located in the solution explorer of the project, which is a console application, and added AllInOne in the system namespace. Please see carefully "using AllInOne". We can instantiate Calculate class as shown below and then can use the method GetAreaofSquare to calculate the area of the Square.

using AllInOne;

namespace UsingDLLinApplication
{
    public class GetResult
    {
        static void Main()
        {
            Calculate myEveryCalculation = new Calculate();
            double storeAreaOFSquare = myEveryCalculation.GetAreaOfSquare(4.5);
            Console.WriteLine("The area of Square is {0}", storeAreaOFSquare);
            Console.ReadLine();
        }
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文