How to pass a string (LPSTR) command to my C++ function?
39 views (last 30 days)
I have a C++-dll, which I have loaded with loadlibrary. I can also list the functions in the dll, which tells me that the call is
[int16, int8Ptr] = wsaSetSCPI(int64 (handle), int8Ptr (command))
command is basically a SCPI command string that I need to pass in and never see again. My call looks like this
command = 'INPUT:MODE SH';
[error, command] = calllib('wsaInterface', 'wsaSetSCPI', hndl, command)
Error using calllib
Array must be numeric or logical or a pointer to one
‘ or “ does not make a difference here. I also tried converting it around a bit, but didn’t find the right way to pass it. I believe that when using the ‘ ‘ notation, it is passed as a char array, so essentially a int8 array. In the C++ dll the command parameter is defined as LPSTR.
What surprises me, is that MATLAB does not seem to mind the LPSTR, because in another function call I can pass an empty, preallocated char array by reference and get back reasonable responses written into it. If anything, I would expect problems with that.
Can you please point me in the right direction on how to pass the command on to my function?
Philip Borghesani on 14 Nov 2018
Edited: Philip Borghesani on 14 Nov 2018
There are two ways to work around this and you almost got it.
Option 1: When casting from a char to a uint8 type in matlab to pass as a string you must manualy add the NULL :
[uint8('*IDN?') 0] %concatinate a null on onto end of string
Option two: Fix the function decleration so that MATLAB will accept a character array as input. There are two ways to do this: You can create a modified header file using char * or other type that matlab will translate to a cstring or you can create a loader file with the mfilename option to loadlibrary and manualy modify that as needed.
For a quick fix I recommend option 1. If you want to simplify the process and make other fixes I suggest a custom loader file.
More Answers (1)
Guillaume on 13 Nov 2018
I think you need to go back through the documentation of the function that you're using. I would never expect to have to pass a string to a function that ask for a int8Ptr, particularly since it's implementation-defined in C++ whether plain char is signed or unsigned (whereas int8 is signed) so a cast from char to int8 may not even be valid. int8Ptr and LPSTR are really not the same types.
It seems to me your function expects an array of signed bytes which you can get in matlab using the function int8 (not uint8) but I expect matlab to do the cast itself when you pass a numerical array to the function.