Using a windows dll in a C++ S-function?

2 views (last 30 days)
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 Comments
James116
James116 on 3 Feb 2014
Another detail which may be of great help would be that the dll was originally compiled from Visual Basic 6.

Sign in to comment.

Accepted Answer

James116
James116 on 4 Feb 2014
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.

More Answers (0)

Categories

Find more on Block and Blockset Authoring in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!