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);
Nilendra Gadre
Nilendra Gadre 2020 年 4 月 1 日
Thank You so much.
This worked.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNaming Conventions についてさらに検索

製品


リリース

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by