Loading C++ shared library DLL into matlab

41 views (last 30 days)
Multiplexer
Multiplexer on 15 Dec 2017
Edited: Walter Roberson on 19 Dec 2017
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.

Answers (2)

Guillaume
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
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
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.

Sign in to comment.


Philip Borghesani
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
  1. If you are using 64 bit MATLAB you want the files from x64 not x86.
  2. The line near line 64: extern "C" { needs a protective guard it is c++ code
  3. This file needs windows.h add
#include <windows.h>
to the top of it
  1 Comment
Multiplexer
Multiplexer on 18 Dec 2017
Thank you for reply, by protective guard you meant "C" guard or include guard or something else?
The command window output was too large but i figured out how to save it to diary file, I attached the output to this comment, with the original SDKDLL.h file, the first error to appear was:
loadlibrary('SDKDLL','SDKDLL')
{Error using loadlibrary
Failed to preprocess the input file.
Output from preprocessor is:C:\Users\Username\Desktop\SDK_Download\CoolerMaster
SDK\SDK\x64\SDKDLL.h:1:4: 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 started reading about the extern "C" and based on this topic https://stackoverflow.com/questions/2168241/is-it-required-to-add-extern-c-in-source-file-also i added protective guard around extern "C" in line 61, 63, 218, 220, 5, 6, 222 and added #include windows.h
Called it again with loadlibrary and here is first error that appeared, full log is in attachments as well as modified library.
loadlibrary('SDKDLL','SDKDLL')
{Error using loadlibrary
Failed to preprocess the input file.
Output from preprocessor is:C:\Users\Username\Desktop\SDK_Download\CoolerMaster
SDK\SDK\x64\SDKDLL.h:1:4: 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 also tried moving #include windows.h inside the extern "C" {...} but error remained.
I started reading about extern "C" itself too, looking at this thread https://stackoverflow.com/questions/2168241/is-it-required-to-add-extern-c-in-source-file-also
>> loadlibrary('SDKDLL','SDKDLL')

Sign in to comment.

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!