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