Passing a "char array" as "Int8Ptr" for calllib function argument

i have a function defined in my C header
"long Function_example(char* User_string, unsigned short task1, unsigned short task2, unsigned long* return_result)"
User_string - is a value entered by user, passed to the fucntion(string in c i.e. char[])
task1 - tells the function to do "task 1"
task2 - tells the function to do "task 2"
return_result - returns a value to be used in other functions.
I loaded the dll for the header in MATLAB using loadlibrary().
libfunctionview() shows the function loaded as
return - [long,uint8Ptr,ulongPtr]
Name - Function_example
Arguments - (int8Ptr,uint16,unit16,ulongPtr)
In the MATLAB script, the user input is accepted using the input()
User_string = input('Enter string','s');
User_string is 1x16 char array, in MATLAB.
How can i pass the value in User_string to the function "Function_example"
I tried the below
trial 1 -
User_string = input('Enter string','s');
task1 = uint16(1);
task2 = uint16(1);
valuePtr = libpointer('ulongPtr',0);
return = calllib('driver','Function_example',User_string,task1,task2,valuePtr);
ERROR returned after the calllib() call.
MATLAB:libpointer:IllegalConversion
Array must be numeric or logical or a pointer to one
trial 2 -
User_string = input('Enter string','s');
task1 = uint16(1);
task2 = uint16(1);
valuePtr = libpointer('ulongPtr',0);
usrPtr = libpointer('int8Ptr', zeros(50, 1,'int8'));
usrPtr.Value = User_string;
return = calllib('driver','Function_example',usrPtr,task1,task2,valuePtr);
ERROR returned after "usrPtr.Value = User_string;".
MATLAB:libpointer:IllegalConversion
Array must be numeric or logical or a pointer to one
trial 3 -
User_string = input('Enter string','s');
task1 = uint16(1);
task2 = uint16(1);
valuePtr = libpointer('ulongPtr',0);
return = calllib('driver','Function_example',[uint8(User_string) 0],task1,task2,valuePtr);
The MATLAB crashes after the calllib() is called.
Any help appreciated to help me with passing the "User_string" contect to "Function_example" using calllib().

4 Comments

In the 3rd case, it sounds like matlab succesfully passed the arguments to the library, then the library does an invalid pointer access and takes matlab down with it. Is the library your own or one you've been supplied?
Also, is the string encoding specified? I doubt that mismatch encoding would be the issue but you never know. What string did you enter at the prompt?
thanks for the reply.
As you mentioned the system was crashing because of the wrong dll version, being used when the MATLAB script was executed.
After pointing to the correct dll, MATLAB is not crashing.
But i am not receiving back a valid data as expected.
"Also, is the string encoding specified? I doubt that mismatch encoding would be the issue but you never know. What string did you enter at the prompt?"
I havent specified any string encoding
string entered is a PXI resource string - PXI25::14::INSTR
the encoded value returned for [uint8(User_string) 0] is
disp(test2);
80 88 73 50 53 58 58 49 52 58 58 73 78 83 84 82 0
But i am not receiving back a valid data as expected.
I would think that return is just a success/failure value. The actual return value that would interest you would be the unsigned long* return_result which is stored valuePtr.Value.
If that's still not right, then possibly you're not passing the values the library expects.
"I havent specified any string encoding"
The documentation of the dll should tell you what encoding it expects for the characters. If your text is simple non-accented latin alphabet, i.e. characters whose uint8 code is less than 128, it's not something you have to worry about. But other characters you would, e.g. the € symbol is uint8(128) is Windows-1252 encoding but uint8([226, 130, 172]) in UTF-8.
Thanks a lot for your help, Guillaume.
The function call is working fine.
This is the solution that worked for me.
User_string = input('Enter string','s');
task1 = uint16(1);
task2 = uint16(1);
valuePtr = libpointer('ulongPtr',0);
return = calllib('driver','Function_example',[uint8(User_string) 0],task1,task2,valuePtr);

Sign in to comment.

Answers (0)

Categories

Asked:

on 2 Mar 2020

Community Treasure Hunt

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

Start Hunting!