Hi, I have created a basic tool to calculate few of the formulas which I was working on. Now I have converted into a simple script and want to put a license kind of function which will show "license expired" error upon exceeding the date.
this so far I could put together: in Tool_validity i have:
function Tool_validity
if now > datenum('2018-01-01') % this code stops working Jan 1, 2018
disp('Please update your License') ;
return
end
and in my script I have put:
Tool_validity;
Thing is, after the message, the tool still proceeds to next steps. I don't want it to proceed further. Please let me know how can I make it work.

6 件のコメント

Guillaume
Guillaume 2018 年 2 月 27 日
People will of course work around such restriction by changing their clock to an earlier date, run your code, then set their clock back up.
Alternatively, they could just shadow the now function so that it returns an earlier date.
adi kul
adi kul 2018 年 2 月 27 日
well I am okay with system date change.
Can you explain a bit on "they could just shadow the now function so that it returns an earlier date." ??
Guillaume
Guillaume 2018 年 2 月 27 日
You just need to put an .m file called now.m in your user directory that returns whatever date you want for it to shadow the built-in now. Because your user folder has higher priority in the path, that now.m will be used by any code that tries to call now.
Rik
Rik 2018 年 2 月 27 日
You can use this Q&A to use the built-in anyway.
Guillaume
Guillaume 2018 年 2 月 27 日
Sure. But the user could also just replace the default now.m (in my version, under C:\Program Files\MATLAB\R2017b\toolbox\matlab\timefun\now.m) by their own to once again work around the license check.
My point is that sort of license checks are extremely fragile and won't stop much.
Rik
Rik 2018 年 2 月 27 日
You could also either hard-code the implementation of now (so t=datenum(builtin('clock'));), or get the time from the internet.
But even if you do, your m-file can be changed, p-code can be decompiled (and reduces the number of releases that will run your code), and time servers can be spoofed. It is always an arms race. Every creator will almost always be on the losing end.

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

 採用された回答

Guillaume
Guillaume 2018 年 2 月 27 日

0 投票

Well, yes disp only displays the message then proceeds with the next line. error aborts the code:
if now > datenum('2018-01-01') % this code stops working Jan 1, 2018
error('Please update your License') ;
end

5 件のコメント

adi kul
adi kul 2018 年 2 月 27 日
well that doesn't look good. I want to display just one line. This gives error as if code is not compiled well
adi kul
adi kul 2018 年 2 月 27 日
this throw:
Error using Tool_validity (line 5)
Please update your License
Rik
Rik 2018 年 2 月 27 日
If you don't want this to return an error, you will have use my second suggestion: add an output, which you use in your main script to stop execution.
And shadowing a function means that someone can write their own function and save it as now.m. This will cause your Tool_validity to use that function instead of the built-in. You could use str=which('now','-all'); to catch this trick.
Guillaume
Guillaume 2018 年 2 月 27 日
編集済み: Guillaume 2018 年 2 月 28 日
The only way to stop code executing in matlab is with the error function (or with debug commands but that's certainly not what you want). Unfortunately, in matlab there is no way to stop error from displaying the call stack, so yes it may display a lot of red text.
The only other option is for your license check function to return a flag that is propagated back to the main function/script which would immediately return if the flag is set.
%main code:
%...
isvalid = validate_license;
if ~isvalid
fprintf(2, 'License outdated. Please update); %fprintf(2, ...) to output in red
return; %quit main code
end
%continue since license is valid
%...
function isvalid = validate_license
isvalid = now <= datenum('2018-01-01')
end
edited for typo
adi kul
adi kul 2018 年 2 月 28 日
This actually helped. Thanks Guillaume!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeManage Products についてさらに検索

タグ

質問済み:

2018 年 2 月 27 日

編集済み:

2018 年 2 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by