S-Function Builder: LoadLibrary failed with error 127

16 次查看(过去 30 天)
I am trying to load DLL file("main_functions.dll") explicitly on simulink using s-function builder, but it failed with error 127. However, when the DLL file is loaded on MATLAB through "loadlibrary" function, it works well. The DLL file is compiled separately with MinGW64 GCC compiler, not from MATLAB coder.
Environments:
  • Windows 10
  • MinGW64 GCC Compiler(Matlab and DLL Compiler)
To verify the DLL, It was tested on MATLAB with following code successfully
result = libpointer('singlePtr', rand([1,19]));
loadlibrary("main_functions", "main_functions.h");
calllib('main_functions', 'setup');
calllib('main_functions', 'predict', input_data, result)
Here is my s-function builder code
#include <windows.h>
#include "mex.h"
void predict_params_Start_wrapper(void **pW)
{
/* Start_BEGIN */
/*
* Custom Start code goes here.
*/
SetLastError(0);
HMODULE dllhandle;
//setup_func f_setup_func;
dllhandle = LoadLibrary("main_functions.dll");
if (dllhandle != NULL) {
mexPrintf("Successfully loaded\n");
}else {
mexPrintf("Failed to load %d\n", GetLastError());
}
}
It returns "Failed to load 127". But when the DLL file is loaded on MATALB, it returns "Successfully loaded".
  2 个评论
Robert Hartley
Robert Hartley 2021-8-27
For what it's worth I'm experiencing the same problem. My code previously ran without issue in Matlab 2016b but no longer in 2020b.
Do we know what else loadlibrary (Matlab's version) does besides actually load the library? Paths and what not? Is there a way to turn on some verbose output?
관웅 윤
관웅 윤 2021-8-30
I have not tried different version of Matlab, because of the compatability with a third-party software in my case. Verbose output means logging compile steps? There is an option to show log in S-function builer.

请先登录,再进行评论。

采纳的回答

Yongjian Feng
Yongjian Feng 2021-8-3
On Windows, the env var PATH is used to find the dll to load. Maybe you need to play with this env var? Also does this main_functions.dll depend on other dlls? If so the PATH needs to point to those dlls as well.
  2 个评论
관웅 윤
관웅 윤 2021-8-4
编辑:관웅 윤 2021-8-4
Thank you for the answer :) I copied "main_functions.dll" to "C:\Windows\System32" which is added on the PATH variable initially, but it did not work. To figure out dll dependency, I analyzed "main_functions.dll" with "Dependencies" program. But only api-ms-win-* dlls is not found and these dll is not the problem as followed this link.
Here is a list of the missing dlls from "Dependencies"
  • api-ms-win-core-comm-l1-1-0.dll
  • api-ms-win-core-wow64-l1-1-1.dll
  • api-ms-win-core-wow64-l1-1-0.dll
  • api-ms-win-core-xstate-l2-1-0.dll
  • api-ms-win-core-xstate-l2-1-1.dll
  • api-ms-win-core-psapi-l1-1-0.dll
  • api-ms-win-core-psapi-ansi-l1-1-0.dll
  • api-ms-win-core-stringansi-l1-1-0.dll
Yongjian Feng
Yongjian Feng 2021-8-4
One way to narrow it down is to use a "HelloWorld" c++ dll. Write a simple C++ dll with basically no/minimum dependency, and see if it can be loaded by s-funciton builder.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 C Shared Library Integration 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by