How can i use C++ DLL in Simulink?

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.

Answers (1)

Yongjian Feng
Yongjian Feng on 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 Comments

Yes, right now I tried to add an addheder with libraries in which an error of not found type occurs. The problem was not resolved, the list of errors remains the same. Although these are gray errors (warnings) with the result
Defaulting to type error.
Defaulting to type voidPtr.
But the main errors occur in the main header, the code of which I attached in this thread.
So by the way, if you comment out
/*#include <Windows.h>*/
all warnings related to not found types will disappear. There will be gray warnings about my main header. Such as:
Type 'WORD' was not found.
Type 'int)voidInit(WORD' was not found.
Type 'size=' was not found.
e.t.c.
There is a feeling that this dll cannot be connected to matlab, due to the fact that it is written in C ++
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.
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 ?))

Sign in to comment.

Categories

Find more on Test Model Components in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 12 Jul 2021

Commented:

on 13 Jul 2021

Community Treasure Hunt

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

Start Hunting!