Loading C++ shared library DLL into matlab
32 views (last 30 days)
Show older comments
Hello,
I want to load a C++ DLL library into Matlab (and ultimately use functions from there in Simulink). I know there is a function called loadlibrary(SHRLIB,HDRFILE) but i was told it works only with pure C libraries (when i tried to load my library it kept spewing errors:
...C:\Users\Username\Desktop\SDK_Download\CoolerMasterSDK\SDK\x86\SDKDLL.h:421:1:
warning: null character(s) ignored...
(the same error would fill whole command window and it would start at ...\SDKDLL.h:348:1: and end at \SDKDLL.h:421:1:)
Is there such function but specifically for C++ libraries? Or other means to load external C++ DLL library? Im attaching the library i want to load. I have MATLAB Support for MinGW-w64 C/C++ Compiler version 17.2.0 installed using Add-On Manager.
0 Comments
Answers (2)
Guillaume
on 18 Dec 2017
Edited: Guillaume
on 18 Dec 2017
Solely from looking at this:
warning: null character(s) ignored
/ / S D K D l l . h : S D K D l l D L L m a i n h e a d e r f i l e
I would suspect that the file uses UTF-16 encoding but is being read as UTF-8. This would explain the warning about the null characters and the letter spacing in the comment.
It may be that before all else, all the files need to be converted to UTF-8 with an editor that support both unicode encodings.
It may be that UTF-16 is the result of your editing, in which case, make sure your editor saves as UTF-8.
7 Comments
Guillaume
on 19 Dec 2017
Edited: Walter Roberson
on 19 Dec 2017
@Multiplexer,
I've just installed the support package for mingw and get the same errors as you get. I don't really have time to investigate why it's not compiling windows.h properly but in any case, it's only there to define a few types, so you could just omit the #include <windows.h> and define these types explicitly:
//replacements for types used by the DLL normally defined in windows.h
typedef char TCHAR; %because _UNICODE is not defined
typedef long LONG;
typedef unsigned char BYTE;
typedef unsigned long DWORD;
#define CALLBACK __stdcall
But you also have the issue that your header file is definitively C++ and not valid C. The structures have constructors (not allowed in C), the structures are type definitions (so struct XX {...}; needs to be replaced by typedef struct XX {...} XX; to be valid C syntax), the enums are also type definitions (so enum XX {...}; needs to be replaced by typedef enum XX {...} XX;, one function has an optional input with default value (not allowed in C)
Even after fixing all of these, and not getting any compilation error or warning, loadlibray then errors with 'SDK.dll' is not a valid shared library. It may be because of an incorrect modification I've done or it may be that indeed that dll can't be loaded by matlab.
Attached, my modified SDKDLL.h (renamed to .txt so it can be uploaded). Unfortunately, I won't have time to investigate this further today.
edit: formatting gone awry
Philip Borghesani
on 19 Dec 2017
Guillaume, I wish there was a way to plus 1 a comment. I think you correctly covered all the major issues.
I expect the reason the dll does not load for you is that you don't have the needed hardware and driver installed to use the Cooler master SDK.
Philip Borghesani
on 15 Dec 2017
Edited: Walter Roberson
on 19 Dec 2017
Loadlibrary works for C or C++ libraries that have C interfaces. It appears to me (from a quick look at the header file) that the CoolerMasterSDK has a C interface that is partly broken and was never actually tested from C.
It is possible that the header can be fixed to be compatible with Loadlibrary but it may take a bit of work. Many of the problems I see have been addressed in one or more questions here. If you wish more help I suggest posting the line you used to load the library and the first error message you see here.
To get you started
- If you are using 64 bit MATLAB you want the files from x64 not x86.
- The line near line 64: extern "C" { needs a protective guard it is c++ code
- This file needs windows.h add
#include <windows.h>
to the top of it
See Also
Categories
Find more on C++ Shared Library Integration in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!