making a program a background process

Started by
8 comments, last by FreeTutorialNewbie 18 years, 7 months ago
In Windows, how do you make a program a background process (c++)? Also, how do you make a program have a tray icon?
I hate signatures.
Advertisement
For the tray icon, search MSDN for "Shell_NotifyIcon"... that should point you in the right direction. Just a quick example to get you started (not tested, sorry):

   NOTIFYICONDATA IconData;   IconData.cbSize = sizeof(NOTIFYICONDATA);   IconData.hWnd = hWnd;   IconData.uID = 0;   IconData.uCallbackMessage = SYSTRAY_CALLBACK_ID;   IconData.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;   strcpy(IconData.szTip, "Text Here");   IconData.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_ICON);   Shell_NotifyIcon(NIM_ADD, &IconData);
To make it a background process, I presume you mean make it only use up 'spare' CPU time. To do that, you need to use SetPriorityClass like so:

SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);

If you're targetting Windows 2000 or newer (XP, 2003, Vista, etc), you can also specify BELOW_NORMAL_PRIORITY_CLASS instead of idle to still get a little bit of CPU time when things are tight, but you won't get it nearly as often as other processess.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
You could use the services API in Windows. Check out the Microsoft knowledge base but here is a quick and dirty example to give you a head start:

http://www.mailbag.com/users/pengel/servicemain.html


Mr. Creamy
By background, I also meant that it wouldn't show up on the taskbar as a running program.
I hate signatures.
I could be wrong, but I think that if you don't create a window (and it isn't a console program) it will not show up on the taskbar.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Quote:Original post by SirLuthor
I could be wrong, but I think that if you don't create a window (and it isn't a console program) it will not show up on the taskbar.
Correct. There are also some (extended) styles which prevent a window from showing up on the taskbar, such as not giving it a title area.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
just go about making your program without opening a window
windows are after all, optional, and background processes do not open a window unless like you click on the taskbar icon.

also use the Sleep(DWORD milliseconds) command, using like 50 milliseconds should keep cpu usage down to 0% and if it does nothing most of the time go ahead and put it about 200

just put a loop in winmain that has the ability to open a window and close it over and over so you can make the background process open a window when the user clicks on the taskbar icon, as long as you have no window you will not have a button on the task bar area, and you must create the taskbar icon
but you have the ability to run in complete stealth with the exception of your executable being listed as a process in window's taskmanager
You can make your WinMain do all kind of sneaky things.
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
here is an example of making a windowless process that has the ability to open a window

this is a naked barebone code, you will have to make the event that sets DoRunWindow=1



#include <windows.h>

int DoRunEngine, DoRunWindow;

LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam){

switch(uMsg){

case WM_CLOSE:
DoRunWindow=0;
return 0;
default:
break;}
return DefWindowProc(hWnd,uMsg,wParam,lParam);}


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
DoRunEngine=1;
DoRunWindow=0;
while(DoRunEngine){
if(DoRunWindow){
wc.style=CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc=(WNDPROC) WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor=LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground=NULL;
wc.lpszMenuName=NULL;
wc.lpszClassName="MainEngineWind";
RegisterClass(&wc);
dwExstyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwstyle=WS_OVERLAPPEDWINDOW;
hWnd=CreateWindowEx(dwExstyle,"MainEngineWind",WindTitle,dwstyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,0,0,200,200,NULL,NULL,hInstance,NULL);
while(DoRunWindow){ //as long as this = 1 window we run
Sleep(30);
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
if(msg.message==WM_QUIT){ //if quited then stop window and program
DoRunWindow=0;
DoRunEngine=0;
}else{
TranslateMessage(&msg);//translate and dispatch our messages
DispatchMessage(&msg);}
}else{
DestroyWindow(hWnd);
UnregisterClass("MainEngineWind",hInstance);}}}}
return (msg.wParam);}



this will run a loop in winmain until the WM_QUIT message is received or DoRunEngine and DoRunWindow are both set to 0

i took this from an opengl engine i made that would close out the window and rebuild it to change resolutions during program execution

the window does not have to be open or opened at any time for this program to run

remember windows are just a way for your program to communicate to the end user, the window is not your program, but a part of the shell you reserve to draw output or take input from
as far as maing a console app..would still have a taskbar title unless it was ran as a service

you setup the tray icon as an early step in winmain and you must make a message pump dedicated to it as if you made a window i beleive

[Edited by - FreeTutorialNewbie on August 31, 2005 9:40:07 AM]

This topic is closed to new replies.

Advertisement