C#-Windows Service中的SendInput()无法正常工作,但返回1-Win03 [英] C# - SendInput() from Windows Service not working but returns 1 - Win03

查看:182
本文介绍了C#-Windows Service中的SendInput()无法正常工作,但返回1-Win03的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

从Windows服务生成应用程序时,我可以通过pinvoke读取窗口标题,通过互操作进行自动化等操作,但是即使调用返回了1,SendInput()消息也似乎消失了.此时要执行的操作是将逃逸击键发送到对话框.

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!

(顺便说一句:我确实意识到MSFT不支持/批准这种自动化.)

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

以防万一其他人遇到这个问题,这可行:

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);

推荐答案

我相信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.

这篇关于C#-Windows Service中的SendInput()无法正常工作,但返回1-Win03的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆