Window Process opening from Windows Service

Source: Internet
Author: User

The IVR local business has a dba_bjldnw tool to run on the server. Once the business is disconnected, It is aborted. You need to write a daemon to monitor it,

Restart once it is found to be disabled.

To prevent the daemon from being disabled by users, I plan to run a Windows service in the background and list the current system process every five minutes. If

If dba_bjldnw.exe is not found, restart.

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

Later, I found the method to open the window program in Windows service. it is to open the user winsta0 and desktop, get the permission to interact with the user, and then open the window process, it can really open the window process in the Windows Service:

 

Hinstance hprocesssnap = NULL;
Processentry32 pe32 = {0 };

 

// Obtain the memory image of the process
Hprocesssnap = (hinstance): createconlhelp32snapshot (th32cs_snapprocess, 0 );
If (hprocesssnap = (handle)-1) return;
Pe32.dwsize = sizeof (processentry32 );

DWORD flag = 0;
Cstring theprocssname = "dba_bjldnw.exe ";

// List processes

If (: process32first (hprocesssnap, & pe32 )){
Do {
If (! Theprocssname. comparenocase (pe32.szexefile )){
Flag = 1;
Break;
}
}
While (process32next (hprocesssnap, & pe32 ));
}
: Closehandle (hprocesssnap );

If (! Flag) {// if not found

Hdesk hsf-current;
Hdesk;
Hwinsta hwinstacurrent;
Hwinsta;

Hwinstacurrent = getprocesswindowstation ();

If (hwinstacurrent = NULL ){
Logevent (_ T ("Get window station Err "));
Return;
}

Hsf-current = getthreaddesktop (getcurrentthreadid ());
If (hsf-current = NULL ){
Logevent (_ T ("Get window desktop Err "));
Return;
}

 

// Open the user's winsta0
Hwinsta = openwindowstation ("winsta0", false,
Winsta_accessclipboard |
Winsta_accessglobalatoms |
Winsta_createdesktop |
Winsta_enum1_tops |
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 the Desktop
Hdesk = opendesktop ("default", 0, false,
Optional top_createmenu |
Optional top_createwindow |
Optional top_enumerate |
Optional top_hookcontrol |
Optional top_journalplayback |
Optional top_journalrecord |
Optional top_readobjects |
Optional top_switchdesktop |
Optional top_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 is restarted ";
Logevent (MSG );

Setprocesswindowstation (hwinstacurrent );
Setthreaddesktop (hsf-current );
Closewindowstation (hwinsta );
Closedesktop (hdesk );
}

 

After the service is installed, be sure to right-click and choose Properties> log on, and select allow service and desktop interaction under the local system account to check.

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.