0

My user is an Administrator (I see it in the configuration panel), the below code throws a Win32Exception in which it says Access Denied, how can I change this (Win7 32 bits) ?

static Guid VideoGuid = new Guid("4d36e968-e325-11ce-bfc1-08002be10318");

[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
static void Main(string[] args)
{
    SafeDeviceHandle handle = null;
    try
    {
        handle = NativeMethods.SetupDiGetClassDevs(ref VideoGuid, IntPtr.Zero, IntPtr.Zero, NativeMethods.DIGCF.PRESENT);
        var data = new NativeMethods.SP_DEVINFO_DATA().Initialize();
        var param = new NativeMethods.SP_PROPCHANGE_PARAMS().Initialize();
        param.ClassInstallHeader.InstallFunction = 0x12;
        param.StateChange = NativeMethods.DICS.ENABLE; // 0x01
        param.Scope = NativeMethods.DICS_GLOBAL.GLOBAL; // 0x01
        param.HwProfile = 0;

        RunWin32Method(() => NativeMethods.SetupDiEnumDeviceInfo(handle, 0u, out data));
        RunWin32Method(() => NativeMethods.SetupDiSetClassInstallParams(handle, ref data, ref param, (UInt32)Marshal.SizeOf(param)));
        RunWin32Method(() => NativeMethods.SetupDiChangeState(handle, ref data));
    }
    catch
    {
        var w = new Win32Exception(Marshal.GetLastWin32Error());
    }
    finally
    {
        if (handle != null && (!handle.IsInvalid))
            handle.Close();
    }
}

static void RunWin32Method(Func<bool> f)
{
    if (!f())
    {
        Debug.WriteLine(new Win32Exception(Marshal.GetLastWin32Error()).Message);
    }
}

If you want more code, just ask :-)

Thanks

Arnaud F.
  • 8,252
  • 11
  • 53
  • 102
  • Did you add a manifest to ensure the program runs with elevated UAC permissions? – Hans Passant Apr 06 '12 at 17:08
  • possible duplicate of [How to force C# App to run as administrator on Windows 7](http://stackoverflow.com/questions/2818179/how-to-force-c-sharp-app-to-run-as-administrator-on-windows-7) – Hans Passant Apr 06 '12 at 17:09
  • @HansPassant: how I do that? Never heard it :/ – Arnaud F. Apr 06 '12 at 17:10
  • Second comment I posted. – Hans Passant Apr 06 '12 at 17:12
  • @HansPassant: I need to start automatically this application when opening my session, so pushed in "Start" folder from the taskbar, but at opening it doesn't work. When I click on the shortcut, it demands me if I authorize the app to run under admin, can I change this behavior? – Arnaud F. Apr 06 '12 at 18:52
  • Yes, Windows refuses to display the UAC prompt at startup. The user has no chance to guess whether that's legitimate or not since she didn't do anything to get the program launched. There's no workaround for that, you need to consider a different activation mode. Like a service. – Hans Passant Apr 06 '12 at 18:54
  • If you post your comments as an answer, I'll accept is an good answer, thanks a lot for all this explanations. :-) – Arnaud F. Apr 06 '12 at 19:04

1 Answers1

1

Recapping the comment trail, a user in the Administrator group doesn't have admin rights on Vista/Server 2008 and later unless the process runs elevated. A manifest is required to get Windows to display the UAC elevation prompt.

This cannot work for programs that are started at login by the Run registry key or the Startup folder. Windows refuses to display the elevation prompt because the user cannot accurately guess exactly what program asked for the elevation. Code-signing the program with a certificate may fix this since that permits Windows to verify and display the program owner, never actually tried that.

Workarounds for such programs are activating it as a service or a scheduled task. Neither of which requires the manifest. The theory behind this seeming oddity is that it already requires elevation to get a service or scheduled task installed.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Just something more, the service is created, i ran it under my user account but the UI isn't displayed (because non-interactivity with desktop?). I need to find a workaround for this now. Under Win7+, nothing is easy... – Arnaud F. Apr 06 '12 at 20:15