C# 创建了 DLL .NET 7,DllImport 不起作用

问题描述 投票:0回答:1

我不明白为什么会出现这个错误: enter image description here

EdiUseDLL:

using System;
using System.Runtime.InteropServices;

class Program
{
    const string Path = @"C:\Users\Maxim\Desktop\EdiUseDLL\EdiLibraryExample\bin\Release\net7.0\EdiLibraryExample.dll";
    [DllImport(Path, CallingConvention = CallingConvention.Cdecl)]
    public static extern int Add(int a, int b);

    static void Main()
    {
        int result = Add(5, 3);
        Console.WriteLine("Result of addition: " + result); 
    }
}

EdiLibrary示例:

namespace EdiLibraryExample
{
    public class Class1
    {
        public static int Add(int a, int b)
        {
            return a + b;
        }

    }
}

enter image description here

如何创建可以在 c# 中与 DLLImport 一起使用的 dll

希望你能帮助我)

c# dll dllimport dllexport dllregistration
1个回答
0
投票

有两种 DLL:托管和非托管。

托管指的是由CLR管理。通常,从 C# 编译的 DLL 是托管 DLL,可以直接被另一个 C# 项目引用,也可以使用

Assembly.LoadFrom
等方法动态加载。

其他 DLL 称为非托管 DLL,通常由其他编程语言(例如 C++)编译而成。在 C# 项目中使用此类 DLL 需要诸如

DllImportAttribute
之类的属性来加载它们。

如果 DLL 是从 C++/CLI 项目编译而来,则它可以包含托管代码和非托管代码。

© www.soinside.com 2019 - 2024. All rights reserved.