描述:
我做了两个进程。其中一个为NT服务,它要向另外一个进程用SendMessage发送消息。
在Debug状况下(在IDE环境下运行),另一个进程可以接收到它发的消息。
当我把编译成Release版并注册到NT服务中去时,另一个进程就接收不到它发的消息了。
不知道是什么原因?望高人释疑!谢谢!!!
解决方案1:
支持的!
解决方案2: 或者在CreateService创建服务时
dwServiceType 设置 SERVICE_INTERACTIVE_PROCESS
服务运行在"Winlogon"桌面上
用户程序运行在"Default"桌面上
运行在一个桌面内的窗口不能收到运行在另一个桌面内线程发出的消息
当然支持罗
NT服务程序 跟普通的差别不是太大。。。
正解
解决方案6: IDE环境下运行可以成功,但是Release并注册了以后不成功,产生这个现象的原因很简单。这是因为你的Service并没有与桌面交互,现实环境中的服务不属于任何桌面,因此也无法指定向哪个具体的窗口句柄SendMessage,你可以这样让服务和桌面交互:
HDESK hdeskCurrent = NULL;
HDESK hdesk = NULL;
HWINSTA hwinstaCurrent = NULL;
HWINSTA hwinsta = NULL;
bool InteractCurDesktop()
{
//
// Set the process interact with current desktop
//
Output("Begin InteractCurDesktop.");
// Restore old handles
hwinstaCurrent = GetProcessWindowStation();
if (NULL == hwinstaCurrent)
{
Outerr("GetProcessWindowStation failed!");
return false;
}
hdeskCurrent = GetThreadDesktop(GetCurrentThreadId());
if (NULL == hdeskCurrent)
{
Outerr("GetThreadDesktop failed!");
return false;
}
// Open current work station
hwinsta = OpenWindowStation(
"winsta0", // open "Winsta0" workstation
TRUE, // can be inherited
WINSTA_ACCESSCLIPBOARD | // enable using clipboard
WINSTA_ACCESSGLOBALATOMS | // enable to manipulate global atoms
WINSTA_CREATEDESKTOP | // enable to create new desktop objects
WINSTA_ENUMDESKTOPS | // enable to enumerate existing desktop objects
WINSTA_ENUMERATE | // enable window station to be enumerated
WINSTA_EXITWINDOWS | // enable to successfully call the ExitWindows
WINSTA_READATTRIBUTES | // enable to read the attributes of a window station
WINSTA_READSCREEN | // enable to access screen contents
WINSTA_WRITEATTRIBUTES); // enable to modify the attributes of a window station
if (NULL == hwinsta)
{
Outerr("OpenWindowStation failed.");
return false;
}
// Set current work station
if (!SetProcessWindowStation(hwinsta))
{
Outerr("SetProcessWindowStation failed.");
CloseWindowStation(hwinsta);
return false;
}
// Open current desktop
hdesk = OpenDesktop(
"default", // open "default" desktop
0, // no flags
FALSE, // can be inherited
DESKTOP_CREATEMENU | // enable to create a menu on the desktop
DESKTOP_CREATEWINDOW | // enable to create a window on the desktop
DESKTOP_ENUMERATE | // enable desktop to be enumerated
DESKTOP_HOOKCONTROL | // enable to establish any of the window hooks
DESKTOP_JOURNALPLAYBACK | // enable to perform journal playback
DESKTOP_JOURNALRECORD | // enable to perform journal recording
DESKTOP_READOBJECTS | // enable to read objects on the desktop
DESKTOP_SWITCHDESKTOP | // enable to activate the desktop using SwitchDesktop
DESKTOP_WRITEOBJECTS); // enable to write objects on the desktop
if (NULL == hdesk)
{
Outerr("OpenDesktop failed.");
CloseWindowStation(hwinsta);
return false;
}
// Interact with current desktop
if (!SetThreadDesktop(hdesk))
{
Outerr("SetThreadDesktop failed.");
CloseWindowStation(hwinsta);
CloseDesktop(hdesk);
return false;
}
// Complete
Output("InteractCurDesktop successful!");
return true;
}
您可能想查找下面的文章:
- SendMessage引起的问题!!!!!
- VistaCOMServer中的COleMessageFilter问题
- SendMessage和std::list问题
- 怎樣利用Sendmessage向toolbar加入button?
- 关于消息函数GetMessage和sendMessage的问题急!!!!!
- SendMessage的问题
- SendMessageTimeout得到lRes为空getlasterror返回值为零
- VC通过sendmessage传送消息给webbrowser无效
- 关于SendMessageHWND_BROADCAST,WM_SETTINGCHANGE,0,"Environment"
- 用sendmessage获取listview总记录