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

5 次查看(过去 30 天)
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.

采纳的回答

Mohammad Sami
Mohammad Sami 2020-4-1
编辑:Mohammad Sami 2020-4-1
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 个评论
Mohammad Sami
Mohammad Sami 2020-4-1
编辑:Mohammad Sami 2020-4-1
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);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Naming Conventions 的更多信息

产品


版本

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by