C++如何给windows服务提供交互控制台

beyondlwm 2012-05-23 03:01:28
刚学习写了一个windows服务程序
现在想这个程序提供一个控制台用于交互

我知道这两者有悖论存在,但是还是希望能有方法做到这一点。

另外,我用的是c++,不是.net

谢谢。
...全文
293 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
beyondlwm 2012-05-23
  • 打赏
  • 举报
回复
已经搞定,要用freopen来对控制台进行重定向。。谢谢各位!
Lactoferrin 2012-05-23
  • 打赏
  • 举报
回复
服务不应该有界面,使用计划任务,或者HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
beyondlwm 2012-05-23
  • 打赏
  • 举报
回复
啰嗦一下,我在服务的处理函数里面alloc了一个console,但是在处理scanf语句的时候,发现scanf直接跳过去了。

如果是普通的win32程序就没问题


服务程序为什么会不允许scanf工作呢?有什么办法解决吗?还是我需求有问题?
cyclejdm 2012-05-23
  • 打赏
  • 举报
回复
只要允许服务和可见桌面交互就可以了,可以在程序里写,也可以直接在服务管理其里面设置
赵4老师 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
已得手!只需要把SERVICE_INTERACTIVE_PROCESS 设置好就ok了!多谢!!!
[/Quote]
信赵老师,得solution!
beyondlwm 2012-05-23
  • 打赏
  • 举报
回复
已得手!只需要把SERVICE_INTERACTIVE_PROCESS 设置好就ok了!多谢!!!
beyondlwm 2012-05-23
  • 打赏
  • 举报
回复
这个代码我看过的,而且经过两位的点拨,已经成功显示messagebox了,但是小弟想要的是allocconsle

还是出不来,有什么办法么?多谢多谢
www_adintr_com 2012-05-23
  • 打赏
  • 举报
回复
http://www.vckbase.com/vckbase/vckbase12/vc/nonctrls/system_30/1230004.htm
赵4老师 2012-05-23
  • 打赏
  • 举报
回复
the service application must be marked as "interactive". This allows your service to interact with the desktop, and allows the debugger window to appear on your desktop.

This again requires modifying a registry key: you must bitwise-or the type entry for your service with 0x100 (this is the value for SERVICE_INTERACTIVE_PROCESS according to Winnt.h). The exact location and name of this registry entry varies. For example:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyServiceKey
beyondlwm 2012-05-23
  • 打赏
  • 举报
回复
试了,不管是createprocess还是allocconsole,仍然看不到。。。
麻烦您仔细说说了。
www_adintr_com 2012-05-23
  • 打赏
  • 举报
回复
服务中使用的窗口是显示在另外一个桌面上的, 但是没有办法把这个桌面显示出来, 所以它无法和用户交互. 程序里只要获得了窗口句柄, 应该是可以和那个窗口进行交互了, 不过我没试过, 不确定. 如果要在服务程序里显示界面, 需要打开用户的 WindowStation, 即 WinSta0 和用户的 desktop "default". 相关代码:


HWINSTA hwinsta = OpenWindowStation("winsta0", FALSE,
WINSTA_ACCESSCLIPBOARD |
WINSTA_ACCESSGLOBALATOMS |
WINSTA_CREATEDESKTOP |
WINSTA_ENUMDESKTOPS |
WINSTA_ENUMERATE |
WINSTA_EXITWINDOWS |
WINSTA_READATTRIBUTES |
WINSTA_READSCREEN |
WINSTA_WRITEATTRIBUTES);

SetProcessWindowStation(hwinsta)



HDESK hdesk = OpenDesktop("default", 0, FALSE,
DESKTOP_CREATEMENU |
DESKTOP_CREATEWINDOW |
DESKTOP_ENUMERATE |
DESKTOP_HOOKCONTROL |
DESKTOP_JOURNALPLAYBACK |
DESKTOP_JOURNALRECORD |
DESKTOP_READOBJECTS |
DESKTOP_SWITCHDESKTOP |
DESKTOP_WRITEOBJECTS);

SetThreadDesktop(hdesk);

beyondlwm 2012-05-23
  • 打赏
  • 举报
回复
感谢楼上,其实我之所以要写服务,是想写一个程序开机自动启动而已。
这种功能其实蛮常见的,比如各种杀毒软件什么的。


但是我现在服务写好了,却不知道如何打开我的console,我试过直接在服务里allocConsole自然是不行,我另外又试在服务里调用createProcess来启动我的另外的console程序,也不行。


求解决方案,太痛苦了。


另外是打算用console做指令解析,所以到时候是一堆字符串的交互。


另外,服务支持窗口消息么?想想貌似应该不支持吧?
赵4老师 2012-05-23
  • 打赏
  • 举报
回复
用调试器(OD,WINDBG等)调试服务程序
To debug the initialization code of a service application, the debugger must be attached when the service is started. This is accomplished by creating a registry key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\ProgramName


The ProgramName is the image file for the service application you are debugging. Do not specify a path. For example, the ProgramName might look like MyService.exe.

Under this key create a string data value called Debugger. The value of this string should be set to the full path of the debugger that will be used. For example,

c:\Debuggers\windbg.exe



In addition to setting this registry key, the service application must be marked as "interactive". This allows your service to interact with the desktop, and allows the debugger window to appear on your desktop.

This again requires modifying a registry key: you must bitwise-or the type entry for your service with 0x100 (this is the value for SERVICE_INTERACTIVE_PROCESS according to Winnt.h). The exact location and name of this registry entry varies. For example:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyServiceKey


Finally, you need to adjust the service application timeout. Otherwise, the service application will kill the debugger within 20 seconds after starting. Adjusting the timeout involves setting an entry in the following registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control


Under this key, create a DWORD data value called ServicesPipeTimeout. Set this entry to the amount of time in milliseconds that you want the service to wait before timing out. For example, 60,000 is one minute, while 86,400,000 is 24 hours.

设置ServicesPipeTimeout后需要重启系统才生效

Now, when the service is started, the debugger will also start. When the debugger starts, it will stop at the initial process breakpoint, before the service has begun running. This allows you to set breakpoints or otherwise configure your debugging session to let you monitor the startup of your service. Another option is to place calls to the DebugBreak function in your service from the point at which you would like to break into the debugger. (For more information, see DebugBreak in the Platform SDK documentation.)

If your service is running with other services in a Service Host Process, you may need to isolate the service into its own Service Host Process.
www_adintr_com 2012-05-23
  • 打赏
  • 举报
回复
可以用任何进程间的通信机制啊, 比如说窗口消息, 共享内存, 管道, 文件, 当然用网络的 socket 消息也可以.
beyondlwm 2012-05-23
  • 打赏
  • 举报
回复
发送时指用网络消息?
www_adintr_com 2012-05-23
  • 打赏
  • 举报
回复
另外写一个控制台程序, 他把收到的命令发送给服务程序就可以了.

64,662

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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