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

조회 수: 12 (최근 30일)
James116
James116 2014년 2월 3일
답변: James116 2014년 2월 4일
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일
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일
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개)

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by