为了学习目的,我正在尝试将一个调用另一个DLL中定义的函数的DLL加载到MATLAB中。我是新手,还没有弄清楚如何做到这一点,也没有找到任何相关资源。
我编写了一个小的C++ DLL,大致如下:
在另一个 DLL 中(我们称之为 DLL2),并从我的 example_dll 调用该函数,最简单的方法是什么?
我希望能够提供一小段示例代码(最好是用于运行时动态链接,并且不使用模块定义 (.def) 文件),或者提供一个相关资源的链接。谢谢!
简单示例的解决方案:
我认为我已经找到了解决方案。它似乎已经起作用了。
我创建了一个名为 interface_DLL 的 DLL,将其加载到 MATLAB 中,并从中调用了 example_dll 中的函数。
这是代码:
我正在进行这个过程的原因是MATLAB共享库接口仅支持C库例程,而不支持C++类。 我想到的解决方法是使用一个中间DLL作为MATLAB和我想要访问其类的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:\Path\to\DLL\';
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