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

11 Ansichten (letzte 30 Tage)
Alexander Jaiboy
Alexander Jaiboy am 2 Mär. 2020
Kommentiert: Alexander Jaiboy am 3 Mär. 2020
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 Kommentare
Guillaume
Guillaume am 2 Mär. 2020
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.
Alexander Jaiboy
Alexander Jaiboy am 3 Mär. 2020
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);

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by