- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
出于学习目的,我试图将一个 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)
我经历这个过程的原因是 the MATLAB shared library interface supports C library routines only and not C++ classes.
我的解决方法是使用中间 DLL 作为 MATLAB 和我打算访问的类的 DLL 之间的接口(interface)。有更好的方法吗?
进一步的问题:
任何人都可以解释行 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 调用约定调用该函数,该约定指定必须如何传递参数以及如何检索返回值。这link用函数指针解释 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;
}
这些函数需要extern "C" 并且您可以使用GetProcAddress 导入它们。然后,只需创建一个实例,通过指针调用其方法并最终销毁它即可。
编辑:一些实现提示
导入创建实例的函数
FARPROC lpfnCreateInstance = GetProcAddress(HMODULE (hGetProcIDDLL), "createInstance");
为函数声明一个正确的指针类型(返回一个 MyClass*,无参数)
typedef MyClass* (__stdcall * pCREATINST)();
转换 lpfnCreateInstance
pCREATINST createInstance;
createInstance = pCREATINST(lpfnCreateInstance)
创建你的实例
MyClass *myInstance = creatInstance();
然后你不需要 add 的包装器,你可以从你的指针调用它。
int res = myInstance->add(123);
你应该对 destroyInstance 做同样的事情,注意类型请注意,我无法测试此代码,但它应该是正确的方法。
关于c++ - 将 C++ DLL 加载到调用另一个 DLL 中的函数的 matlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26928016/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!