将一个C++ DLL 加载到 matlab 中,该 matlab 调用另一个 DLL 中的函数

Load a C++ DLL into matlab which calls functions in another DLL

本文关键字:matlab DLL 调用 函数 另一个 加载 一个 C++      更新时间:2023-10-16

出于学习目的,我正在尝试将一个DLL加载到MATLAB中,该DLL调用在另一个DLL中定义的函数。我是所有这些的新手,还没有弄清楚我将如何做到这一点,也没有设法找到任何相关的资源。

我在C++中编写了一个小 DLL,如下所示:

//example_dll.h
#ifndef EXAMPLE_DLL_H
#define EXAMPLE_DLL_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BUILDING_EXAMPLE_DLL
#define EXAMPLE_DLL __declspec(dllexport)
#else
#define EXAMPLE_DLL __declspec(dllimport)
#endif
int EXAMPLE_DLL Double(int x);
#ifdef __cplusplus
}
#endif
#endif  // EXAMPLE_DLL_H

和源文件:

//example_dll.cpp
#include <stdio.h>
#include "example_dll.h"
int Double(int x)
{
        return 2 * x;
}

这是我使用 MinGW w64 构建的,并使用 loadlibrary('example_dll') 加载到 matlab 中,没有任何问题。

我现在想定义函数

int Double(int x)
{
        return 2 * x;
}

在另一个 DLL 中(我们称之为 DLL2),并从我的example_dll调用该函数。最简单的方法是什么?

我希望有一个简短的示例代码(最好用于运行时动态链接,并且不使用模块定义(.def)文件)或指向互联网上相关资源的链接。谢谢!

简单示例的解决方案:

我想我得到了解决方案。无论如何,它似乎都在工作。

我创建了一个名为 interface_DLL 的 DLL,我将其加载到 MATLAB 中,并从中调用了我的函数example_dll

在这里:

//interface_dll.h
#ifndef INTERFACE_DLL_H
#define INTERFACE_DLL_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BUILDING_INTERFACE_DLL
#define INTERFACE_DLL __declspec(dllexport)
#else
#define INTERFACE_DLL __declspec(dllimport)
#endif

int INTERFACE_DLL Quadruple(int x);
#ifdef __cplusplus
}
#endif
#endif  // INTERFACE_DLL_H

和源文件:

//interface_dll.cpp
#include <windows.h>
#include <stdio.h>
#include "interface_dll.h"
#include "example_dll.h"
int Quadruple(int x)
{
    /* get handle to dll */ 
    HINSTANCE hGetProcIDDLL = LoadLibrary("C:\Users\uidr0605\Documents\ExampleDLL\example_dll.dll"); 
    /* get pointer to the function in the dll*/
    FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"Double");
    /*
    Define the Function in the DLL for reuse. This is just prototyping the dll's function.
    A mock of it. Use "stdcall" for maximum compatibility.
    */
    typedef int (__stdcall * pICFUNC)(int);
    pICFUNC Double;
    Double = pICFUNC(lpfnGetProcessID);
    /* The actual call to the function contained in the dll */
    int intMyReturnVal = Double(x);
    intMyReturnVal = Double(intMyReturnVal);
    /* Release the Dll */
    FreeLibrary(hGetProcIDDLL);
    /* The return val from the dll */
    return intMyReturnVal; 
}

我从 MATLAB 加载它,如下所示:

%loadDLL.m
path = 'C:PathtoDLL';
addpath(path);
loadlibrary('interface_dll')
i = 2;
x = calllib('interface_dll', 'Quadruple', i)

我经历此过程的原因是 MATLAB 共享库接口仅支持 C 库例程,不支持C++类。

我对解决方法的想法是使用中间 DLL 充当 MATLAB 和我打算访问的类的 DLL 之间的接口。有没有更好的方法

更多问题:

谁能解释一下这里应用的线路typedef int (__stdcall * pICFUNC)(int);的重要性?如果我想在example_dll中调用类中的函数,我必须添加什么或必须进行哪些更改?

编辑:我将以下代码添加到example_dll头文件中:

class EXAMPLE_DLL MyClass
{
public: 
        int add2(int);
};

#ifdef __cplusplus
extern "C" {
#endif

MyClass EXAMPLE_DLL *createInstance(){
        return new MyClass();
}
void EXAMPLE_DLL destroyInstance(MyClass *ptrMyClass){
        delete ptrMyClass;
}
#ifdef __cplusplus
}
#endif

进一步的问题 1

以下定义

typedef int (__stdcall * pICFUNC)(int);

定义一个新类型 pICFUNC,它是指向函数的指针,该函数采用 int 并返回 int。此外,必须根据__stdcall调用约定调用该函数,该约定指定必须如何传递参数以及如何检索返回值。此链接解释了带有函数指针的 typedef。另请查看以下部分,将 typedef 与类型转换一起使用,因为在线

Double = pICFUNC(lpfnGetProcessID);

pICFUNC用于投射。

进一步的问题2

下面举一个非常微不足道的例子来给出一个想法。如果你example_dll有一个名为MyClass的类,它有一个方法

int add(int num);

您可以实现以下函数:

MyClass *createInstance(){
    return new MyClass();
}
void destroyInstance(MyClass *ptrMyClass){
    delete ptrMyClass;
}

这些函数需要是外部"C",您可以使用 GetProcAddress 导入它们。然后,只需创建一个实例,通过指针调用其方法并最终销毁它。

编辑:实施的一些提示

导入函数以创建实例

FARPROC lpfnCreateInstance = GetProcAddress(HMODULE (hGetProcIDDLL), "createInstance");

为函数声明正确的指针类型(返回 MyClass*,无参数)

typedef MyClass* (__stdcall * pCREATINST)();

Cast lpfnCreateInstance

pCREATINST createInstance;
createInstance = pCREATINST(lpfnCreateInstance)

创建您的实例

MyClass *myInstance = creatInstance();

然后你不需要一个包装器来添加,你可以从你的指针调用它。

int res = myInstance->add(123);

你应该对 destroyInstance 做同样的事情,小心类型请注意,我无法测试此代码,但它应该是正确的方法。