开发者

Can I determine if Matlab is running as with elevated privileges in Windows?

开发者 https://www.devze.com 2023-04-01 06:59 出处:网络
I would like to detect if the current Matlab session is running with elevated privileges (i.e. the user started it with \"Run as Administrator\") under Windows. Ideally the solution would work on XP a

I would like to detect if the current Matlab session is running with elevated privileges (i.e. the user started it with "Run as Administrator") under Windows. Ideally the solution would work on XP and Windows 7, but I'm happy to have two solutions if necessary.

This answer suggests that I may be able to get the information I need via the .Net external interface f开发者_StackOverflowrom Matlab (at least for Vista and later), but I'm wondering if there is a more "native" Matlab solution.


The "Matlab .NET Bridge" is for going the other way - calling Matlab from .NET. Calling .NET classes from Matlab can be done pretty directly using the .NET external interfaces support.

function out = isWindowsAdmin()
%ISWINDOWSADMIN True if this user is in admin role.
wi = System.Security.Principal.WindowsIdentity.GetCurrent();
wp = System.Security.Principal.WindowsPrincipal(wi);
out = wp.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);

That should work on any Windows version with .NET installed. The more "native" way would probably require writing a MEX to call win32 API functions, which would be more work. Works on my XP machine.

0

精彩评论

暂无评论...
验证码 换一张
取 消