I have a .NET Core 3 project which uses a wrapper library that uses Mosquitto to talk with a MQTT Broker.
The wrapper is defined as:
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();
}
And the header is defined as:
#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_
The issue is when I tried to start the project, .NET Core throws an exception. System.DllNotFoundException: 'Unable to load DLL 'mosq' or one of its dependencies: The specified module could not be found. (0x8007007E)'
This doesn't make sense at all. The dll and its dependencies are copied to the folder which loads mosq dll. Moreover, the running process writes symbol loaded about related dlls explicitly. I also tried to toss the dll around different folders but it's of no use. What could go wrong?