C# SetupDiChangeState抛出访问被拒绝

C# SetupDiChangeState抛出访问被拒绝,c#,pinvoke,C#,Pinvoke,我的用户是管理员(我在配置面板中看到),下面的代码抛出一个Win32异常,其中显示拒绝访问,我如何更改它(Win32位) 静态Guid VideoGuid=新Guid(“4d36e968-e325-11ce-bfc1-08002be10318”); [SecurityPermission(SecurityAction.Demand,UnmanagedCode=true)] 静态void Main(字符串[]参数) { SafeDeviceHandle=null; 尝试 { handle=Nati

我的用户是管理员(我在配置面板中看到),下面的代码抛出一个Win32异常,其中显示
拒绝访问
,我如何更改它(Win32位)

静态Guid VideoGuid=新Guid(“4d36e968-e325-11ce-bfc1-08002be10318”);
[SecurityPermission(SecurityAction.Demand,UnmanagedCode=true)]
静态void Main(字符串[]参数)
{
SafeDeviceHandle=null;
尝试
{
handle=NativeMethods.SetupDiGetClassDevs(参考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(句柄,0u,输出数据));
RunWin32Method(()=>NativeMethods.SetupDiSetClassInstallParams(handle,ref data,ref param,(UInt32)Marshal.SizeOf(param));
RunWin32Method(()=>NativeMethods.SetupDiChangeState(句柄,引用数据));
}
抓住
{
var w=新的Win32Exception(Marshal.GetLastWin32Error());
}
最后
{
if(handle!=null&(!handle.IsInvalid))
handle.Close();
}
}
静态void RunWin32Method(Func f)
{
如果(!f())
{
WriteLine(新的Win32Exception(Marshal.GetLastWin32Error()).Message);
}
}
如果您想要更多代码,只需询问:-)


感谢

回顾评论线索,管理员组中的用户在Vista/Server 2008及更高版本上没有管理员权限,除非进程运行升级。需要获取窗口以显示UAC高程提示

对于在登录时通过运行注册表项或启动文件夹启动的程序,这不起作用。Windows拒绝显示高程提示,因为用户无法准确猜测程序要求的高程。使用证书对程序进行代码签名可能会解决此问题,因为这允许Windows验证和显示程序所有者,但实际上从未尝试过


这些程序的解决方法是将其作为服务或计划任务激活。两者都不需要清单。这种看似奇怪的现象背后的理论是,它已经需要提升才能安装服务或计划任务。

您是否添加了清单以确保程序以提升的UAC权限运行?可能与@HansPassant重复:我是如何做到的?从未听说过:/Second评论我发布。@HansPassant:我需要在打开会话时自动启动此应用程序,所以从任务栏中按入“开始”文件夹,但在打开时它不起作用。当我点击快捷方式时,它要求我是否授权应用程序在admin下运行,我可以更改此行为吗?更重要的是,服务已创建,我在我的用户帐户下运行,但UI未显示(因为与桌面不交互?)。我现在需要找到一个解决办法。在Win7+下,没有什么是容易的。。。
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);
    }
}