C# - 来自 Windows 服务的 SendInput() 不起作用,但返回 1 - Win03

发布于 2024-10-03 12:55:50 字数 657 浏览 2 评论 0原文

这是一个有点不寻常的问题。我目前正在努力实现 Office 应用程序的自动化。基本上问题是,当我通过 RDP 执行应用程序时,我的应用程序能够与 Office 应用程序正确交互,但如果我将应用程序设置为从 Windows 服务生成,则无法使用 SendInput() 发送击键。操作系统是 Win03 标准。

当从 Windows 服务生成应用程序时,我能够按照 pinvoke 的预期读取窗口标题、通过互操作实现自动化等,但即使调用返回 1,SendInput() 消息似乎也会消失。我正在尝试此时要做的是将转义击键发送到对话框。

我尝试过将击键作为虚拟键和扫描码发送。

有什么想法或建议吗?提前致谢!

(顺便说一句:我确实意识到这种自动化不受 MSFT 支持/认可。)

编辑: 以防万一其他人遇到这个问题,这有效:

[DllImport("user32")]

public static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);


bool returnVal = NativeWin32.PostMessage(popup, 0x0100, new IntPtr(0x1b), IntPtr.Zero);

This is a somewhat unusual problem. I'm presently working on an effort to automate an Office application. Basically the issue is that my app is able to interact with the Office app correctly when I execute my app via RDP, but I am unable to send keystrokes using SendInput() if I set up the application to spawn from a windows service. OS is Win03 Std.

When spawning the app from a windows service I'm able to read window captions, automate through the interop, etc. as expected via pinvoke, but the SendInput() messages seemingly vanish even though the call returns a 1. All I'm trying to do at this point is send an escape keystroke to a dialog.

I've tried sending the keystrokes both as virtual keys and scancodes.

Any ideas or tips? Thanks in advance!

(BTW: I do realize that this sort of automation isn't supported/sanctioned by MSFT.)

EDIT:
Just in case anyone else has this issue, this works:

[DllImport("user32")]

public static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);


bool returnVal = NativeWin32.PostMessage(popup, 0x0100, new IntPtr(0x1b), IntPtr.Zero);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好菇凉咱不稀罕他 2024-10-10 12:55:50

我相信 SendInput 会将按键发送到当前活动窗口(或具有焦点的窗口)。通常,Windows 服务将与不同的桌面(即交互式桌面)关联 - 因此从 Windows 服务启动的应用程序也将与后台桌面关联 - 因此没有活动窗口(或具有焦点的窗口),这可能是 sendinout 不起作用的原因。您也许可以尝试使用 SendMessagePostMessage 来模拟按键单击 - 您将需要窗口句柄。

I believe that SendInput will send keys to current active window (or window that has focus). Typically, windows services would be associated with different desktop (that interactive desktop) - so App launched from windows service would also be associated with the background desktop - so no active window (or window with focus) and that's may be the reason sendinout not working. You can perhaps try simulating key click by using SendMessage or PostMessage - you will need window handle for that.

悲喜皆因你 2024-10-10 12:55:50

从 Windows Vista 开始,不再允许服务与桌面交互。这意味着他们没有桌面上下文来发送键盘事件。

As of Windows Vista, services are no longer allowed to interact with the desktop. Which means they would not have a desktop context to send keyboard events to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文