How to read and write to global variables of shared library(using loadlibrary)?

13 views (last 30 days)
A C dll contains functions fn1(),fn2() etc which take some input arguments and give some outputs.
The processing and output of the fn1() depends on global varaibles (exported in dll header).
When the dll is loaded using
>> loadlibrary('dllname','dllname.h')
>> m = libfunctions('dllname', '-full')
Output is:
>> m =
'fn1'
'fn2'
'lib.pointer globalVar1'
'lib.pointer globalVar2'
.
.
Question is how to modify value of this globalVar1 in matlab?
Ex:
prev_value = globalVar1;
globalVar1 = newValue;
output = calllib('dllname','fn1',arg1);
Similarly, how to modify if the global variables are structures?
Thanks in Advance.

Accepted Answer

Mohammad Sami
Mohammad Sami on 1 Apr 2020
Edited: Mohammad Sami on 1 Apr 2020
You need to get the pointer first. Try the following to see if it works.
Also you did not specify what data type the pointer is pointing to in the question.
globalpointer1 = calllib('dllname','globalVar1');
prev_value = globalpointer1.Value;
globalpointer1.Value = int8(2); % assuming a data type and value.
output = calllib('dllname','fn1',arg1);
Structures should be returned as libstruct objects. See the libstruct documentations for details.
  3 Comments
Mohammad Sami
Mohammad Sami on 1 Apr 2020
Edited: Mohammad Sami on 1 Apr 2020
try this way.
globalpointer1 = calllib('dllname','globalVar1');
setdatatype(globalpointer1,'int32Ptr',1,1);
prev_value = globalpointer1.Value;
globalpointer1.Value = int32(2); % assuming a data type and value.
% arg1 ....
output = calllib('dllname','fn1',arg1);

Sign in to comment.

More Answers (0)

Products


Release

R2013b

Community Treasure Hunt

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

Start Hunting!