Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 from Matlab (at least for Vista and later), but I'm wondering if there is a more "native" Matlab solution.

like image 465
Barry Wark Avatar asked Aug 31 '11 16:08

Barry Wark


1 Answers

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.

like image 80
Andrew Janke Avatar answered Oct 02 '22 00:10

Andrew Janke