gpt4 book ai didi

c# - 无法导入.NET Core 3.0加载的DLL

转载 作者:行者123 更新时间:2023-11-30 19:01:00 25 4
gpt4 key购买 nike

我有一个 .NET Core 3 项目,它使用一个包装器库,该库使用 Mosquitto 与 MQTT 代理进行通信。

包装器定义为:

public static class MosquittoWrapper
{
public const string MosquittoDll = "mosq";

[StructLayout(LayoutKind.Sequential)]
public struct mosquitto_message
{
public int mid;
public string topic;
public byte[] payload;
public int payloadlen;
public int qos;
[MarshalAs(UnmanagedType.U1)]
public bool retain;
}

public delegate void ConnectCallbackDelegate(int result);
public delegate void MessageCallbackDelegate(mosquitto_message mesage);

[DllImport(MosquittoDll, CallingConvention = CallingConvention.Cdecl)]
public static extern int mosq_init();

[DllImport(MosquittoDll, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
internal static extern int mosq_set_tls_psk(string psk, string identity, string? ciphers);

[DllImport(MosquittoDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern void mosq_set_callback(ConnectCallbackDelegate? connect_callback, MessageCallbackDelegate? message_callback);

[DllImport(MosquittoDll, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
internal static extern int mosq_connect(string host, int port, int keepalive);

[DllImport(MosquittoDll, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
internal static extern int mosq_subscribe_topic(string topic);

[DllImport(MosquittoDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern void mosq_runloop(int timeout, int max_packets, int sleep_on_reconnect);

[DllImport(MosquittoDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int mosq_destroy();
}

header 定义为:

#pragma once

#ifndef MOSQUITTO_H_
#define MOSQUITTO_H_

#include <mosquitto.h>

#ifdef _WIN32
# ifdef LIBRARY_EXPORTS
# define LIBRARY_API __declspec(dllexport)
# else
# define LIBRARY_API __declspec(dllimport)
# endif
#elif
# define LIBRARY_API
#endif


typedef void (*CONNECT_CALLBACK)(int);
typedef void (*MESSAGE_CALLBACK)(const struct mosquitto_message*);

// return value 0 represents success. Or ENOMEM, EINVAL when error.
LIBRARY_API int mosq_init();
LIBRARY_API int mosq_set_tls_psk(char* psk, char* identity, char* ciphers);
LIBRARY_API void mosq_set_callback(CONNECT_CALLBACK connect_callback, MESSAGE_CALLBACK message_callback);
LIBRARY_API int mosq_connect(char* host, int port, int keepalive);
LIBRARY_API int mosq_subscribe_topic(char* topic);
LIBRARY_API void mosq_runloop(int timeout, int max_packets, int sleep_on_reconnect);
LIBRARY_API int mosq_destroy();

#endif // MOSQUITTO_H_

问题是当我尝试启动项目时,.NET Core 抛出异常。 System.DllNotFoundException:“无法加载 DLL 'mosq' 或其依赖项之一:找不到指定的模块。 (0x8007007E)'

这根本没有意义。该 dll 及其依赖项被复制到加载 mosq dll 的文件夹中。此外,运行的进程显式地写入有关相关dll加载的符号。我还尝试将 dll 放在不同的文件夹中,但没有用。可能会出现什么问题?

最佳答案

dotnet core 中的 DLL 导入有效 same as it did in .net framework

转到https://mosquitto.org/并下载(源/dll)。在这里,我获取了 Windows 二进制文件“mosquitto.dll”。

使用上述 dll 的包装器生成 C# 程序(API: https://mosquitto.org/api/files/mosquitto-h.html )。

这里我将仅使用 init 函数作为概念证明

using System;
using System.Runtime.InteropServices;

namespace aac
{
class Program
{
static void Main(string[] args)
{
// Just test the init function
MosquittoWrapper.mosquitto_lib_init();
Console.WriteLine("success");
}
}

public static class MosquittoWrapper
{
// Same name as DLL
public const string MosquittoDll = "mosquitto";

// https://mosquitto.org/api/files/mosquitto-h.html#mosquitto_lib_init
[DllImport(MosquittoDll, CallingConvention = CallingConvention.Cdecl)]
public static extern int mosquitto_lib_init();
}
}

然后构建项目

PS C:\Users\bburns\code\scratch\aac\aac> dotnet build
Microsoft (R) Build Engine version 16.3.0+0f4c62fea for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

Restore completed in 20.94 ms for C:\Users\bburns\code\scratch\aac\aac\aac.csproj.
aac -> C:\Users\bburns\code\scratch\aac\aac\bin\Debug\netcoreapp3.0\aac.dll

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:01.61

然后将 mosquitto.dll 放在与执行程序集相同的文件夹中

 bin\Debug\netcoreapp3.0

运行程序:

PS C:\Users\bburns\code\scratch\aac\aac> dotnet run
success

请注意,如果名称不匹配或找不到 dll,您将生成 DllFileNotFoundException

public const string MosquittoDll = "mosquittoz";

构建并运行

PS C:\Users\bburns\code\scratch\aac\aac> dotnet run
Unhandled exception. System.DllNotFoundException: Unable to load DLL 'mosquittoz' or one of its dependencies: The specified module could not be found. (0x8007007E)
at aac.MosquittoWrapper.mosquitto_lib_init()
at aac.Program.Main(String[] args) in C:\Users\bburns\code\scratch\aac\aac\Program.cs:line 10

关于c# - 无法导入.NET Core 3.0加载的DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58654177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com