MATLAB Answers

0

Loading C++ shared library DLL into matlab

Asked by Multiplexer on 15 Dec 2017
Latest activity Edited by 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.

  0 Comments

Sign in to comment.

2 Answers

Answer by Guillaume
on 18 Dec 2017
Edited by 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

I installed Visual Studio 2017 Community, new compiler is recognized, here are errors that show up now with with loadlibrary(), full log in attachment.
diary on
loadlibrary('SDKDLL','SDKDLL')
[Warning: Warnings messages were produced while parsing. Check the functions you intend to use for
correctness. Warning text can be viewed using:
[notfound,warnings]=loadlibrary(...)]
[> In loadlibrary]
Error loading library intermediate output follows.
The actual error is at the end of this output.
(...)
*********
{Error using loadlibrary
Building SDKDLL_thunk_pcwin64 failed. Compiler output is:
cl -I"P:\Programy\MATLAB\R2017b\extern\include" /Zp8 /W3 /nologo
-I"C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64"
-I"C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64" "SDKDLL_thunk_pcwin64.c" -LD
-Fe"SDKDLL_thunk_pcwin64.dll"
SDKDLL_thunk_pcwin64.c
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(25): error C2061: syntax error:
identifier 'KEY_COLOR'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(31): error C2059: syntax error:
'}'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(35): error C2061: syntax error:
identifier 'COLOR_MATRIX'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(37): error C2061: syntax error:
identifier 'KeyColor'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(37): error C2059: syntax error:
';'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(37): error C2059: syntax error:
'['
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(38): error C2059: syntax error:
'}'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(112): error C2146: syntax
error: missing ')' before identifier 'devIndex'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(112): error C2061: syntax
error: identifier 'devIndex'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(112): error C2059: syntax
error: ';'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(112): error C2059: syntax
error: ')'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(132): error C2061: syntax
error: identifier 'GetDeviceLayout'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(132): error C2059: syntax
error: ';'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(132): error C2059: syntax
error: '<parameter-list>'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(152): error C2146: syntax
error: missing ')' before identifier 'iEffectIndex'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(152): error C2061: syntax
error: identifier 'iEffectIndex'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(152): error C2059: syntax
error: ';'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(152): error C2059: syntax
error: ')'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(162): error C2143: syntax
error: missing ')' before '='
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(162): error C2072:
'RefreshLed': initialization of a function
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(162): error C2059: syntax
error: ')'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(182): error C2146: syntax
error: missing ')' before identifier 'colorMatrix'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(182): error C2061: syntax
error: identifier 'colorMatrix'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(182): error C2059: syntax
error: ';'
C:\Users\Username\Desktop\SDK_Download\CoolerMaster SDK\SDK\x64\SDKDLL.h(182): error C2059: syntax
error: ')'
SDKDLL_thunk_pcwin64.c(59): error C2065: 'DEVICE_INDEX': undeclared identifier
SDKDLL_thunk_pcwin64.c(59): error C2146: syntax error: missing ';' before identifier 'p0'
SDKDLL_thunk_pcwin64.c(59): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(60): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(60): error C2065: 'DEVICE_INDEX': undeclared identifier
SDKDLL_thunk_pcwin64.c(60): error C2143: syntax error: missing ')' before 'const'
SDKDLL_thunk_pcwin64.c(60): error C2100: illegal indirection
SDKDLL_thunk_pcwin64.c(60): error C2059: syntax error: ')'
SDKDLL_thunk_pcwin64.c(61): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(62): error C2061: syntax error: identifier 'DEVICE_INDEX'
SDKDLL_thunk_pcwin64.c(72): error C2061: syntax error: identifier 'LAYOUT_KEYBOARDThunk'
SDKDLL_thunk_pcwin64.c(72): error C2059: syntax error: ';'
SDKDLL_thunk_pcwin64.c(72): error C2059: syntax error: '<parameter-list>'
SDKDLL_thunk_pcwin64.c(89): error C2065: 'EFF_INDEX': undeclared identifier
SDKDLL_thunk_pcwin64.c(89): error C2146: syntax error: missing ';' before identifier 'p0'
SDKDLL_thunk_pcwin64.c(89): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(90): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(90): error C2065: 'EFF_INDEX': undeclared identifier
SDKDLL_thunk_pcwin64.c(90): error C2143: syntax error: missing ')' before 'const'
SDKDLL_thunk_pcwin64.c(90): error C2100: illegal indirection
SDKDLL_thunk_pcwin64.c(90): error C2059: syntax error: ')'
SDKDLL_thunk_pcwin64.c(91): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(92): error C2061: syntax error: identifier 'EFF_INDEX'
SDKDLL_thunk_pcwin64.c(96): warning C4013: 'falseThunk' undefined; assuming extern returning int
SDKDLL_thunk_pcwin64.c(96): error C2143: syntax error: missing ')' before 'type'
SDKDLL_thunk_pcwin64.c(96): error C2099: initializer is not a constant
SDKDLL_thunk_pcwin64.c(122): error C2065: 'COLOR_MATRIX': undeclared identifier
SDKDLL_thunk_pcwin64.c(122): error C2146: syntax error: missing ';' before identifier 'p0'
SDKDLL_thunk_pcwin64.c(122): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(123): error C2065: 'p0': undeclared identifier
SDKDLL_thunk_pcwin64.c(123): error C2065: 'COLOR_MATRIX': undeclared identifier
SDKDLL_thunk_pcwin64.c(123): error C2143: syntax error: missing ')' before 'const'
SDKDLL_thunk_pcwin64.c(123): error C2100: illegal indirection
SDKDLL_thunk_pcwin64.c(123): error C2059: syntax error: ')'
SDKDLL_
@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
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.


Answer by Philip Borghesani on 15 Dec 2017
Edited by 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

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.