WINDOWS服务程序中,如何创建一个无模式对话框,并可以处理WINDOWS消息?

sdcer777 2012-07-31 12:25:02
默认情况下,WINDOWS服务程序是没有界面的,而是后台运行。但有的时候,由于某些特殊的需求,需要在服务程序中创建一个无模式对话框,用于接收WINDOWS消息,如何创建?

我曾经尝试着按照通常的方法在服务程序中来创建一个无模式对话框,即new, create, show,但结果是失败,会出现断言。

那么,如何在WINDOWS服务程序中创建一个无模式对话框,并能正确接收和处理WINDOWS消息?即:有正常的消息处理机制?
最好是有一个例子。谢谢!


...全文
225 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
redui 2012-08-01
  • 打赏
  • 举报
回复
SESSION的问题,WINXP及以前,服务和桌面共享SESSION 0,从VISTA开始,服务位于SESSION 0,桌面默认位于SESSION 1,跨SESSION是无法访问窗口的。这个帖子里有代码,服务程序如何用桌面SESSION创建一个新的副本进程,在这个副本进程中能正常创建显示窗口,原服务进程是肯定不行的。
sdcer777 2012-08-01
  • 打赏
  • 举报
回复

此问题还是没有解决。

换个角度来说,我的要求就是,如何在系统登陆以前,就运行一个窗口程序 。


有没有办法?我觉得服务看来是不太好做(在WINDOWS XP下似乎是可以,但是到了VISTA和WIN7以后,基本上就不太行了)。那有没有别的什么办法?

lqfcu2 2012-07-31
  • 打赏
  • 举报
回复
服务程序跟普通用户程序是在不同的会话跟桌面环境下的,WINXP服务跟第一个登录的用户都是0会话,XP以后就完全隔离了服务程序跟用户程序,服务程序运行在0会话,第一个登录的用户会话是1,你可以在服务程序中,使用CreateProcessAsUser 函数来创建一个用户环境的新进程,再通过内在共享啊,PIPE啊,SOCKET啊通信就行了~
yyyyy_3 2012-07-31
  • 打赏
  • 举报
回复
创建服务时必须用交互方式, 其他没变化.
yyyyy_3 2012-07-31
  • 打赏
  • 举报
回复
dlg = new CAaaDlg();

HINSTANCE hOldResHandle=AfxGetResourceHandle();
HINSTANCE hInst = ::GetModuleHandle(NULL);
AfxSetResourceHandle(hInst);

dlg->Create(IDD_AAA_DLG,NULL);
dlg->ShowWindow(SW_SHOW);

AfxSetResourceHandle(hOldResHandle);
yanjing_mail 2012-07-31
  • 打赏
  • 举报
回复
http://download.csdn.net/detail/yanjing_mail/4465841
Eleven 2012-07-31
  • 打赏
  • 举报
回复
http://www.vckbase.com/index.php/wv/1443
参考一下,希望有帮助
赵4老师 2012-07-31
  • 打赏
  • 举报
回复
用调试器(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.
mfcdeclare 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

http://www.vckbase.com/index.php/wv/1443
参考一下,希望有帮助
[/Quote]

你好,我感觉你提供的程序还是有些用处,但是我按照他这个程序在我的机器上做测试,进程可以运行,但界面显示不出来,按照你的文章中的说明,原因是:“我们还需要在使用CreateService函数时(Install()中),加上一个参数,这样才能允许程序与桌面交互,也就是可以显示界面。这个参数是SERVICE_INTERACTIVE_PROCESS”。

但问题是,我已经加上这个参数了,但界面还是显示不出来。我不知道是什么原因?
我的系统是WIN7 64位。不知道和这个有没有关系?

mfcdeclare 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

http://www.vckbase.com/index.php/wv/1443
参考一下,希望有帮助
[/Quote]

你好,我感觉你提供的程序还是有些用处,但是我按照他这个程序在我的机器上做测试,进程可以运行,但界面显示不出来,按照你的文章中的说明,原因是:“我们还需要在使用CreateService函数时(Install()中),加上一个参数,这样才能允许程序与桌面交互,也就是可以显示界面。这个参数是SERVICE_INTERACTIVE_PROCESS”。

但问题是,我已经加上这个参数了,但界面还是显示不出来。我不知道是什么原因?
我的系统是WIN7 64位。不知道和这个有没有关系?

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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