How can i use C++ DLL in Simulink?

7 Ansichten (letzte 30 Tage)
Lapenkov Yaroslav
Lapenkov Yaroslav am 12 Jul. 2021
I want to warn you that I am new to matlab and I hope for your calmness and kindness. <3
At work I was assigned the task: Connect .dll to matlab. This DLL came from another department, and I have no feedback with it.
So, what i have ?! DLL, Header files, config files for input values(xml) and description in docx format. In doxc i can check description for function, for example for ModelStart(). I dont have guide how to use this dll in docx!
Since my company's policy does not allow me to download, upload or send files to myself, I will attach below a piece of code from the main header file.
#pragma once
#include <windows.h>
#include <memory.h>
struct ModelFrame {
WORD chan;
WORD size;
ModelFrame(WORD c, WORD s)
{ Init(c,s); }
void Init(WORD c, WORD s){
chan = c;
size = s;
}
WORD Size()
{ return size; }
void Load(const void * src){
ModelFrame * m = (ModelFrame *) src;
chan = m->chan;
if (m->size < size){
if (m->size > 4)
memcpy((char *)this + 4, (char *)src+4, m->size - 4);
memset((char *)this + m->size, 0, size - m->size);
}
else if (size > 4)
memcpy ((char *)this + 4, (char *)src + 4, size - 4);
}
void Index(int idx)
{ chan = (chan&~3) + ((idx + 1)&3); }
int Index()
{ return ((chan&3) - 1)&3; }
};
typedef void (*PROGRESS_FUNC)(floar prg, char * msg);
typedef bool (*MODEL_MESSAGE)(const void * msg, DWORD length);
#ifdef MODEL_DLL
#define EXPORT_MODEL __declspec(dllexport)
#else
#define EXPORT_MODEL __declspec(dllimport)
void EXPORT_FFS ModelStart(double step, const char * dir, HWND window, MODEL_MESSAGE customRecv = 0, PROGRESS_FUNC pf = 0, const char * iface = 0, const chzr * addr = 0. int basePort = 32000);
#endif
If I understand correctly, the header file is written in C ++, which means that the DLL is also in C ++. I looked at a number of discussions on the forums about connecting dll to matlab and it all boiled down to using LoadLibrary() directly in the matlab console, or in a c mex function. Both of these methods gave me the same errors:
And i recieve big list of errors from dll like this two bellow:
Type 'LARGE_INTEGER' was not found. (winnt.h and ktmtypes.h)
Type 'REASON_CONTENT'/'DCB'/'PCONTEXT' was not found. (winbase.h)
And many other errors, that some types were not found from
winioct1.h , wtypes.h, rpcndr.h, wincrypt.h, prsht.h, winspool.h, objid1.h, oaid1.h, msxm1.h
and others...
So my main question is:
Can I connect this dll to matlab for further use or not? And if I can, then what should be my actions, in which direction to go, where to look for a solution to my problem? Or mb i should say to my boss, that this task is impossible?)
In addition:
>> mex -setup
MEX configured to use 'Microsoft Visual C++ 2019 (C)' for C language compilation.

Antworten (1)

Yongjian Feng
Yongjian Feng am 12 Jul. 2021
Did you try to use 'addheader' of loadlibrary? The error seems to be related to header files.
https://www.mathworks.com/help/matlab/ref/loadlibrary.html
  3 Kommentare
Yongjian Feng
Yongjian Feng am 12 Jul. 2021
C++ dll should work. The problem here seems to be the long dependency of header files.
It seems like the MEX approach is easier here.
Lapenkov Yaroslav
Lapenkov Yaroslav am 13 Jul. 2021
What u mean about "long dependency of header files"? It include only <windows.h> , that have nested includes of header files, to which i get a list of warnings. How should i solve it? Write more than 15 includes instead of one #include <windows.h>?
What about MEX approach, what should i do in this way? Bcs i dont found any ways, where i can use .dll in MatLab without transmutation it to LIB, with using LoadLibrary.
And what about errors from my main header file, why MatLab hate my structure name, type WORD and other things ?))

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by