8

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.

Community
  • 1
  • 1
Barry Wark
  • 107,306
  • 24
  • 181
  • 206
  • Can you answer your question with pure java code? Then there may be a more "native" solution (I assume you mean, one that doesn't require any external libraries to be installed....) – John Aug 31 '11 at 20:54

1 Answers1

7

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.

Andrew Janke
  • 23,508
  • 5
  • 56
  • 85