To open a window process from a Windows service (turn)

Source: Internet
Author: User

Http://dev.csdn.net/author/axman/420f84d550804bfbaf534819c757a2b9.html

In the AVR local business there is a DBA_BJLDNW tool to run on the server. Once disconnected business is terminated. You need to write a daemon to monitor it,

Reboot once the discovery is turned off.

To prevent the daemon itself from being shut down by the user, I'm going to do a Windows service running in the background. Enumerate a current system process every 5 minutes, if

The dba_bjldnw.exe is restarted without discovery.

The problem is that Windows service is not a window process, and the application that calls ShellExecute open in this process is also running in the background and cannot be displayed in the current window.

Later, you searched for a way to open a window program in a Windows service. Just open the user WinSta0 and desktop, get permission to interact with the user, and then open the window process, and you can open the window process in the Windows service:

HInstance Hprocesssnap = NULL;
PROCESSENTRY32 pe32 = {0};

Get the memory mirror of the process
Hprocesssnap = (hinstance):: CreateToolhelp32Snapshot (th32cs_snapprocess,0);
if (Hprocesssnap = = (HANDLE)-1) return;
pe32.dwsize = sizeof (PROCESSENTRY32);

DWORD flag = 0;
CString theprocssname = "Dba_bjldnw.exe";

Enumerating processes

if (::P Rocess32first (hprocesssnap,&pe32)) {
do{
if (!theprocssname.comparenocase (Pe32.szexefile)) {
flag = 1;
Break
}
}
while (Process32Next (HPROCESSSNAP,&PE32));
}
:: CloseHandle (HPROCESSSNAP);

if (!flag) {//If not found

Hdesk hdeskcurrent;
Hdesk Hdesk;
Hwinsta hwinstacurrent;
Hwinsta Hwinsta;

Hwinstacurrent = Getprocesswindowstation ();

if (hwinstacurrent = = NULL) {
LogEvent (_t ("Get window Station err"));
return;
}

Hdeskcurrent = Getthreaddesktop (GetCurrentThreadID ());
if (hdeskcurrent = = NULL) {
LogEvent (_t ("Get window Desktop Err"));
return;
}

 //Open user's winsta0
  hwinsta = openwindowstation ("winsta0", false,                           
   WINSTA_ACCESSCLIPBOARD   |
   winsta_accessglobalatoms |
   WINSTA_CREATEDESKTOP     |
   WINSTA_ENUMDESKTOPS      |
   WINSTA_ENUMERATE         |
   WINSTA_EXITWINDOWS       |
   WINSTA_READATTRIBUTES    |
   WINSTA_READSCREEN        |
   winsta_writeattributes);
  if (Hwinsta = = NULL) {
   logevent (_t ("open window station err");
   return;
  }

if (! SetProcessWindowStation (Hwinsta)) {
LogEvent (_t ("Set window station err"));
return;
}

Open Desktop
Hdesk = OpenDesktop ("Default", 0, FALSE,
Desktop_createmenu |
Desktop_createwindow |
Desktop_enumerate |
Desktop_hookcontrol |
Desktop_journalplayback |
Desktop_journalrecord |
desktop_readobjects |
Desktop_switchdesktop |
Desktop_writeobjects);
if (hdesk = = NULL) {
LogEvent (_t ("Open Desktop Err"));
Return
}

SetThreadDesktop (hdesk);

:: ShellExecute (0,null, "F://ivrtj//dba//dba_bjldnw.exe", null,null,sw_maximize);
CString msg = "Dba_bjldnw.exe to be restarted";
LogEvent (msg);

SetProcessWindowStation (hwinstacurrent);
SetThreadDesktop (hdeskcurrent);
Closewindowstation (Hwinsta);
Closedesktop (hdesk);
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.