gpt4 book ai didi

c++ - Matlab:dll 的 C++ 头文件

转载 作者:搜寻专家 更新时间:2023-10-31 01:48:33 55 4
gpt4 key购买 nike

我有一个非常简单的 dll 库头文件,但它是用 C++ 编写的。任何人都可以帮助我以与 Matlab( native C)中的“LoadLibrary”命令兼容的方式对其进行编辑吗?我意识到这不是一个普遍的问题,但更有可能缺乏我的知识。但如果解决方案很简单,我将不胜感激任何建议。

// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the TRACKERERRORSDLL_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// TRACKERERRORSDLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef TRACKERERRORSDLL_EXPORTS
#define TRACKERERRORSDLL_API __declspec(dllexport)
#define TRACKERERRORSDLL_VB __declspec(dllexport) __stdcall
#else
#define TRACKERERRORSDLL_API __declspec(dllimport)
#define TRACKERERRORSDLL_VB __declspec(dllimport) __stdcall
#endif

#include <string>
using namespace std;

bool TRACKERERRORSDLL_API GetTPIErrorDescription_wstring(long errorNumber,
basic_string<__wchar_t> & shortDescription,
basic_string<__wchar_t> & longDescription,
basic_string<__wchar_t> & solutionDescription,
bool & isAutoRecoverOnGreenState);

bool TRACKERERRORSDLL_API GetTPIErrorDescription_wstring(long errorNumber,
basic_string<unsigned short> & shortDescription,
basic_string<unsigned short> & longDescription,
basic_string<unsigned short> & solutionDescription,
bool & isAutoRecoverOnGreenState);

bool TRACKERERRORSDLL_API GetTPIErrorDescription_string(long errorNumber,
string & shortDescription,
string & longDescription,
string & solutionDescription,
bool & isAutoRecoverOnGreenState);

bool TRACKERERRORSDLL_API GetTPIErrorDescription_CString(long errorNumber,
CString & shortDescription,
CString & longDescription,
CString & solutionDescription,
bool & isAutoRecoverOnGreenState);

bool TRACKERERRORSDLL_VB GetTPIErrorDescription_VB(int errorNumber,
LPSTR* shortDescription,
LPSTR* longDescription,
LPSTR* solutionDescription,
bool* isAutoRecoverOnGreenState);

库的下载链接(64 位): https://docs.google.com/file/d/0BzzppV2CG8ZldzFRVzJUa252MHc/edit?usp=sharing

Matlab R2013a 64位

最佳答案

您可以调用的唯一函数是 GetTPIErrorDescription_VB。所有其他人都使用您无法访问的 C++ 类。因此,我建议您执行以下操作:

  1. 从头文件中删除所有其他函数。
  2. 删除 #includeusing 行。
  3. 删除 #ifdef 并将 TRACKERERRORSDLL_VB 替换为 __stdcall
  4. 要么包含 windows.h,要么为 Win32 类型添加一些 #define 语句。
  5. 可能会处理 bool 类型,具体取决于 MATLAB 是否知道如何处理它。如果 MATLAB 无法识别它,请将 bool 替换为 int

此时对 loadlibrary 的调用应该可以工作,然后您只需要编写调用 calllib 的代码。

生成的头文件可能如下所示:

#define LPSTR char*

__declspec(dllimport) bool __stdcall GetTPIErrorDescription_VB(
int errorNumber,
LPSTR* shortDescription,
LPSTR* longDescription,
LPSTR* solutionDescription,
bool* isAutoRecoverOnGreenState
);

最后,请注意 LPSTR* 是一个相当令人惊讶的类型。提示DLL要分配char* C字符串,然后通过三个描述参数返回给你。这提出了内存分配问题。谁来释放内存?它甚至需要被释放,还是静态的?这些问题需要通过查阅 DLL 文档来解决。

关于c++ - Matlab:dll 的 C++ 头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17651796/

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