How to pass a string (LPSTR) command to my C++ function?
Mostra commenti meno recenti
Hello all,
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)
It returns:
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?
Risposta accettata
Più risposte (1)
Guillaume
il 13 Nov 2018
1 voto
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.
4 Commenti
Peter Foerster
il 14 Nov 2018
I'm very confused. In your question you talk about wsaSetSCPI with signature
[int16, int8Ptr] = wsaSetSCPI(int64 (handle), int8Ptr (command))
But now you talk about wsaGetSCPI_s. They're not the same functions and don't have the same signature at all.
Which of the two functions is causing problem?
You should pass a char vector (1xn) (or a scalar string since R2016b) to a function that expects LPSTR pointers. You should pass a numerical array (possibly as int8 if matlab doesn't autoconvert it) to a function that expects a int8Ptr, .
edit: Also, I just realised that none of your functions are expecting pointers to const. Do the functions actually modify the pointed to values? Is the dll actually written in C++ or in C?
Peter Foerster
il 14 Nov 2018
Guillaume
il 14 Nov 2018
I would think that the lib.pointer issue is different from your dll errors issue.
What is the actual C++ signature of the 3 functions? And what are you giving to loadlibrary for matlab to find the signature? The C++ header file? A proto file? It's very puzzling that matlab translate LPSTR into Int8Ptr but that would be all down to what you're telling loadlibrary.
If your functions do not modify the inputs, then really they should be defined as pointers to const. On the C++ side, it will enable some compiler optimisations. For matlab it will make a major difference in the function signature. The reason you've got all these int8Ptr as outputs of your functions is because the pointers are non-const. For matlab, a pointer to const is the same as passing by value, so it's very straight-forward. A pointer to non-const is pass-by-reference so matlab needs to put extra machinery in place to translate that to pass-by-value of the matlab world.
Since you're the author of the dll, can't you attach a debugger to matlab to see what actual value the dll receives?
Categorie
Scopri di più su C Shared Library Integration in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!