VC implements system hotkeys to activate background service programs

Source: Internet
Author: User
Abstract: This article provides a method to hide the running service programs in the system background by setting the system hotkey, you can set the interaction between background service programs and users when necessary.

Introduction

Generally, services used for background monitoring do not have interfaces or even taskbar icons. Therefore, in most cases, Service programs cannot interact with users. However, in actual applications, although these service programs run in the background for most of the time, user intervention is required and some necessary interactive operations are performed with the user in some cases. However, since the service program does not provide any interface for interactive operations, how to activate it from the background (that is, calling out) becomes a key to solving this problem. The following describes how to activate a service program running on the background by setting the system hotkey.

Design Concept

Although theoretically there are many ways to activate a service program running in the background, for example, you can find the window name of the service program and get its window pointer, then, send a message to the window to bring it to the foreground. You can also use the System Snapshot to enumerate the current system process and then activate it to the foreground. However, all of the above methods need to write additional applications. Activation of background service programs is actually performed in these applications. This processing method is obviously inconvenient, the best way is to activate and hide programs within the service program. Therefore, you can consider receiving messages from the system. If you intercept and capture the set events by setting a global hook, it is obviously quite cumbersome. The system hotkey is used to activate the background service program. The implementation process is very simple. You only need to add a global Atom to the operating system and then register a hotkey with the operating system, when the program is running in the background, once this hot key is pressed, the operating system will throw the system message WM_HOTKEY, therefore, the service program only needs to add the corresponding code in the WM_HOTKEY message response function to activate the background of the service program.

Registration of system hotkeys

According to the previous introduction, it is not difficult to write the code that adds the system hotkey response to the background service program. First, use the globalfindatom () function to check whether the global atom corresponding to the service program already exists in the global atomic table. If it is found, the service exists in the system and the program exits. If no data is found, use the globaladdatom () function to add a string to the global atomic table and obtain an atom that uniquely identifies the string. The two function prototypes are as follows:

The following is a reference clip:
Atom globalfindatom (lpctstr lpstring );
Atom globaladdatom (lpctstr lpstring );

The input parameter is a string describing atoms. If globalfindatom () finds the specified string from the global atomic table, the corresponding atom is returned. Otherwise, 0 is returned. If globaladdatom () is created successfully, a newly created atom is returned.

Next, in order to capture system hotkeys during the program running, you need to define a system-range hotkey through registerhotkey. The original function is as follows:

The following is a reference clip:
Bool registerhotkey (hwnd, // window handle for receiving hotkey response
Int ID, // ID of the hotkey
Uint fsmodifiers, // control key flag
Uint VK // virtual key value
);

The hotkey id must be a globally unique value ranging from 0xC000 to 0xFFFF. To avoid hot key conflicts, the atoms returned by GlobalAddAtom () are usually input as parameters, the returned value range of GlobalAddAtom () is the same as that of the id parameter. The fsModifiers parameter defines the control key combination that generates the system hotkey message WM_HOTKEY when the vk is pressed at the same time, such as MOD_ALT, MOD_CONTROL, MOD_SHIFT, and MOD_WIN. In this example, the system Hot Key to be set is Alt + Ctrl + R. Therefore, the fsModifiers and vk parameters are set to MOD_ALT | MOD_CONTROL and VK_R respectively. The following describes how to register system hotkeys:

The following is a reference clip:
// Obtain the current window handle
HWND handle = GetSafeHwnd ();
// Find whether the atom corresponding to the HotKey exists in the atom list
If (GlobalFindAtom ("Hotkey") = 0)
{
// Create an atom if it does not exist in the atom list
Id = GlobalAddAtom ("Hotkey ");
// Register the global hot key Ctrl + Alt + R
RegisterHotKey (handle, id, CONTROL + ALT, R );
}
Else // If the HotKey already exists in the atomic list, terminate the program.
Postquitmessage (0 );

Service Program hiding and Activation

In addition to interaction with users after the service program is activated, most of the time is hidden in the background, not only the interface is invisible, but also the task list should not appear. The hiding of the interface is relatively simple. You can set the sw_hide parameter to the showwindow () function. The general practice of hiding in the task list is to call the registerserviceprocess () of the system kernel kernel32.dll () the function sets it as a service process, which is invisible in the task list. However, the registerserviceprocess () function is not a standard API function. It is a bit cumbersome to use. First, you must use the getmodulehandle () function to obtain the handle of the kernel32.dll module, and then use the getprocaddress () function to obtain the entry address of the registerserviceprocess () function in kernel32.dll, and finally use the registerserviceprocess. The function prototype declaration is as follows:

The following is a reference clip:
DWORD registerserviceprocess (DWORD dwprocessid, DWORD dwtype );
The first parameter specifies the process ID to be registered as a service process. The dwtype parameter specifies to register a service process (when it is set to 1) or Uninstall a service process (when it is 0 ). The specific service registration process is as follows:
Typedef DWORD (winapi * RSP) (DWORD dwprocessid, DWORD dwtype );
// Obtain the kernel32.dll module handle
Hmodule m_hkernel =: getmodulehandle ("kernel32.dll ");
// Obtain the endpoint address of the registerserviceprocess () function.
RSP m_rsp = (RSP): getprocaddress (m_hkernel, "registerserviceprocess ");
// Register the current process as a service process
M_rsp (: getcurrentprocessid (), 1 );

During the running of the service program background, once the system Hot Key Alt + Ctrl + R is pressed, the system hot key message wm_hotkey will be sent. The message response function of the message cannot be added through classwizard, message ing can only be completed manually. In the message response function, the wparam parameter can be used to determine whether the system hotkey is set by the service program. If yes, the program interface is displayed through showwindow (sw_show, to interact with users:

The following is a reference clip:
Void cservicedlg: onhotkey (wparam, lparam)
{
// Determine whether it is the system hotkey set by the Service Program
If (wparam = ID)
{
......
// Send the wm_paint message here, which is passed in onpain ()
// Showwindow (sw_show) set the interface to visible
Postmessage (wm_paint, 0, 0 );
}
}

Unmount system hotkey

Because the system hotkeys and global atoms are previously registered to the system, you must uninstall them before the service program exits. Otherwise, the next registration will fail. The unregisterhotkey () function is used to release the system hotkey. globaldeleteatom () deletes the global atom from the global atom list.

Summary

By using this method, the system service program running in the background can be added with the hotkey outgoing call function, which can realize concealed operation and hotkey activation in the background of the program, which is very beneficial to the management and use of Administrators and Users. The program described in this article is compiled by Microsoft Visual C ++ 2000 under Windows 6.0 professional.

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.