I have a C++ source code that is completely functional.
It uses functions from a windows DLL.
When I try to create a Simulink s-function using the legacy code tool,
these dll functions are not accessible. I get a segmentation fault.
If I take the calls to the functions out of the source code, it will run fine.
I have already changed my system path to where the dll is located. I have also checked for dependencies in the dll.
My C++ compiler is Visual Studio 2010. Matlab 32 bit 2011b
Here is how I use the legacy code tool:
clc
clear
def = legacy_code('initialize')
def.Options.language = 'C++';
def.OutputFcnSpec=['double y1 = positionfunct(void)']
def.IncPaths = {'C:\mtlb11b\bin\sfunctionblock'}
def.SrcPaths = {'C:\mtlb11b\bin\sfunctionblock'}
def.LibPaths = {'C:\mtlb11b\bin\sfunctionblock'}
def.HeaderFiles = {'Sfile.h','windows.h'}
def.SourceFiles = {'CodeForSFile.cpp'}
def.SFunctionName = 'GetPosition'
legacy_code('sfcn_cmex_generate',def)
legacy_code('compile',def)
legacy_code('Slblock_generate',def)
legacy_code('sfcn_tlc_generate',def)
def.SampleTime = 'parameterized'
Here is how I am obtaining the functions in C++:
static HINSTANCE hGetProcIDDLL = LoadLibrary("C:/mtlb11b/bin/sfunctionblock/PCI_VME.dll");
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"OPT");
typedef int (__stdcall * pICFUNC2)(char*);
pICFUNC2 OPT;
OPT = pICFUNC2(lpfnGetProcessID);
What are some things that I can try to get this to work? Should this work or should I be using a different approach?

댓글 수: 4

James116
James116 2014년 2월 3일
편집: James116 2014년 2월 3일
I just tried a different approach, using a c++ mex function (Although part of what I am trying to do is in Simulink, I can still do stuff in matlab). Still crashes though. So if there is advice you can give me on using dll files in either method, it would be helpful.
When I make the mex function I use the command
mex -v 'thecodefor.cpp' 'PCI_VME.lib'
to ensure that the library is linked. The library is also in the same folder as a whole bunch of dll files from some software that it originally came with, so if there is a dependency, I would think that it would solve itself.
Update: I have now also tried the loadlibrary function to put the library directly into matlab. I don't have a header though, and with the headers I am writing it doesn't work (segmentation error again).
James116
James116 2014년 2월 3일
It is really quite frustrating. I thought that when you wrote a mex or generated a S-function, that you were just interfacing the existing C/C++ code to Matlab and that the nature of how the code was executed wouldn't change.
I'm missing something. Maybe it is simple, maybe it isn't.
James116
James116 2014년 2월 3일
Another detail which may be of great help would be that the dll was originally compiled from Visual Basic 6.
James116
James116 2014년 2월 4일
bump

댓글을 달려면 로그인하십시오.

 채택된 답변

James116
James116 2014년 2월 4일

0 개 추천

I eventually solved the problem myself. It was quite trivial. I was indexing out of an array of a return string from one of my dll functions.
Always look for the basic errors.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Simulink Coder에 대해 자세히 알아보기

질문:

2014년 2월 3일

답변:

2014년 2월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by