c# 调用 SendKeys.Send 时,抛异常:Win32Exception 拒绝访问

oldhunter 2021-02-09 05:21:23
在项目中(winform项目)使用 SendKeys.Send("测试模拟键盘输入") 时,抛异常:Win32Exception (0x80004005): 拒绝访问。

请问题有什么解决办法吗?有没有排查原因的思路?

我在另一个DEMO程序中(新建的一个winform项目),测试是正常的。
...全文
1088 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
desperaso 2021-03-11
  • 打赏
  • 举报
回复
引用 7 楼 橘子皮zzz 的回复:
[quote=引用 6 楼 desperaso 的回复:][quote=引用 4 楼 oldhunter 的回复:][quote=引用 2 楼 qq_24002977 的回复:]以管理员权限运行

[/code][/quote]

你这个有的机器上会导致打不开,闪退,删除这段右键管理员就可以打开[/quote]

这个设置管理员权限方式没什么问题,如果觉得有问题可以修改app.manifest文件。
如果修改app.manifest文件还有问题,检查代码或给微软报bug。
橘子皮... 2021-03-11
  • 打赏
  • 举报
回复
引用 6 楼 desperaso 的回复:
[quote=引用 4 楼 oldhunter 的回复:][quote=引用 2 楼 qq_24002977 的回复:]以管理员权限运行
这个不太现实,就是普通的打开一个客户端软件。而不能要求用户“右击 -> 以管理员身份运行”。 除非在软件运行中,可以切换到管理员权限。可以经由用户同意。[/quote] 很麻烦么?

///////////////Winform的
static void Main(string[] Args)
{
	try
  {
  	System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
    Application.EnableVisualStyles();
    System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
    if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
    {
    	Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Test());
    }
    else
    {
    	System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
      startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
      startInfo.Arguments = String.Join("" "", Args);
      startInfo.Verb = ""runas"";
      System.Diagnostics.Process.Start(startInfo);
      System.Windows.Forms.Application.Exit();
    }
	}
}
                    
                    
////////////WPF的
public static void Main()
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(identity);
                    
        if (principal.IsInRole(WindowsBuiltInRole.Administrator))
        {
            App app=new App();
            app.Run();
        }
        else
        {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.WorkingDirectory = Environment.CurrentDirectory;
            startInfo.FileName = Assembly.GetExecutingAssembly().Location;
            startInfo.Verb = ""runas"";
            try
            {
                System.Diagnostics.Process.Start(startInfo);
            }
            catch
            {
                return;
            }
            Application.Current.Shutdown();
        }
    }
    
    ///////或者编译时修改程序的app.manifest文件(百度好多)
[/quote] 你这个有的机器上会导致打不开,闪退,删除这段右键管理员就可以打开
desperaso 2021-03-10
  • 打赏
  • 举报
回复
引用 4 楼 oldhunter 的回复:
[quote=引用 2 楼 qq_24002977 的回复:]以管理员权限运行


这个不太现实,就是普通的打开一个客户端软件。而不能要求用户“右击 -> 以管理员身份运行”。
除非在软件运行中,可以切换到管理员权限。可以经由用户同意。[/quote]

很麻烦么?

///////////////Winform的
static void Main(string[] Args)
{
try
{
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
Application.EnableVisualStyles();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Test());
}
else
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
startInfo.Arguments = String.Join("" "", Args);
startInfo.Verb = ""runas"";
System.Diagnostics.Process.Start(startInfo);
System.Windows.Forms.Application.Exit();
}
}
}


////////////WPF的
public static void Main()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);

if (principal.IsInRole(WindowsBuiltInRole.Administrator))
{
App app=new App();
app.Run();
}
else
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Assembly.GetExecutingAssembly().Location;
startInfo.Verb = ""runas"";
try
{
System.Diagnostics.Process.Start(startInfo);
}
catch
{
return;
}
Application.Current.Shutdown();
}
}

///////或者编译时修改程序的app.manifest文件(百度好多)
oldhunter 2021-03-10
  • 打赏
  • 举报
回复
补充:是需要向 CefSharp 浏览器控件中,通过模拟按钮,输入字符。

原项目的程序中,测试抛异常:Win32Exception (0x80004005): 拒绝访问。
在另一个DEMO程序中(新建的一个winform项目),测试是正常的。
oldhunter 2021-02-19
  • 打赏
  • 举报
回复
引用 2 楼 qq_24002977 的回复:
以管理员权限运行


这个不太现实,就是普通的打开一个客户端软件。而不能要求用户“右击 -> 以管理员身份运行”。
除非在软件运行中,可以切换到管理员权限。可以经由用户同意。
失落的神庙 2021-02-18
  • 打赏
  • 举报
回复
除了楼上说的,还有不要在云主机上运行,如果一定要,得采用特殊方式进行远程,关闭mstsc后按键操作就不行了
慢节奏qukq 2021-02-17
  • 打赏
  • 举报
回复
以管理员权限运行
oldhunter 2021-02-13
  • 打赏
  • 举报
回复
目前问题仍未解决,期待回复!

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧