Creating hidden process in Win32

I want to be able to execute a home made C/C++ Win32 program, that lives "forever" as invisibly as possible. It would be nice if I could let it sleep for a minute, then wake up, perform some simple stuff, and then sleep for a minute again.

I mean sleep, like unix's sleep. It should consume as little CPU as possible.

How is this done?
LVL 2
obgAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NickRepinCommented:
Create a program with a hidden main window (which is not have a WS_VISIBLE style).
Use Sleep(60000) to sleep 1 min.
0
jkrCommented:
That's no problem at all. There's a Win32 'Sleep()' that performs exactly what you want. To make your app invisible, simply don't show it's window (or better: Show it hidden), e.g.:

int PASCAL  wWinMain    (   HANDLE  hInstance,
                            HANDLE  hPrevInstance,
                            LPCTSTR lpszCmdLine,
                            int     nCmdShow
                        )
{
    WNDCLASS    wndclass;
    MSG         uMsg;
    HWND        hWnd;

    //  Do the usual initialization stuff ...
    ZeroMemory  (   &wndclass,  sizeof  (   WNDCLASS));

    wndclass.lpszClassName  =   L"MyApp";
    wndclass.hInstance      =   ( HINSTANCE) hInstance;
    wndclass.lpfnWndProc    =   WndProc;
    wndclass.hCursor        =   LoadCursor  (   NULL,   IDI_APPLICATION);
    wndclass.hIcon          =   LoadIcon    (   0,  IDI_EXCLAMATION);
    wndclass.lpszMenuName   =   NULL;
    wndclass.hbrBackground  =   ( HINSTANCE) ( COLOR_BACKGROUND +   1);
    wndclass.style          =   NULL;
    wndclass.cbClsExtra     =   0;
    wndclass.cbWndExtra     =   0;

    RegisterClass   (   &wndclass);

    hWnd    =   CreateWindowEx  (   0,
                                    L"MyApp",
                                    L"MyApp",
                                    WS_OVERLAPPEDWINDOW,
                                    CW_USEDEFAULT,
                                    0,
                                    CW_USEDEFAULT,
                                    0,
                                    NULL,
                                    NULL,
                                    ( HINSTANCE) hInstance,
                                    NULL
                                );

  ShowWindow  (   hWnd,   SW_HIDE); // <-- !!!

    while   (   GetMessage  (   &uMsg,  0,  0,  0))
            {
                TranslateMessage    (   &uMsg);
                DispatchMessage     (   &uMsg);
            };

    return  (   0);
};

For a GUI app. If you prefer a console app, simply call 'FreeConsole()'...
0
MadshiCommented:
For win9x you can make it even more hidden (that is: from the Ctrl+Alt+Del- tasklist) by calling the "RegisterServiceProcess" API...

Regards, Madshi.
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

obgAuthor Commented:
Sorry to reject the answer, NickRepin. I have to admit I am not very experienced as a Win32 programmer. For instance, where would I (not) involve the WS_VISIBLE stuff?

jkr, I understand your solution better, but could you please explain why I would need a WndProc when there is no visible window? I have tried to create a program with no wndclass, no wndproc and no window. It seems to work... - Comments...?

Madshi, I appreciate your comment! I might use it if I get through my beginners problems...

And for my next slightly embarrasing question; Where do I put my code? I guess it's no good to mix it with in the message loop. A Sleep() there would not do much good I guess... :-) I guess I could create a thread...?

Once again, sorry for being such a beginner, I really ought to know this stuff by now...
0
jkrCommented:
Well, it's not absolutely necessary to use a window, but without one, you're not able to perform any message processing (so I included one in the example). This comes in handy when e.g. having to shut the app down without killing it or if you want to somehow display a message. Of course, a 'naked'

int PASCAL  wWinMain    (   HANDLE  hInstance,
                            HANDLE  hPrevInstance,
                            LPCTSTR lpszCmdLine,
                            int     nCmdShow
                        )
{

  for ( ;;)
  {
    Sleep ( MY_INTERVAL);
    ProcessSomething();
  }

 return ( 0);
}

would also do it...
0
obgAuthor Commented:
You're right. I might want to do some stuff at both WM_CREATE and WM_DESTROY. Thanks. But then where would I put my stuff...?
0
jkrCommented:
Put it in the window proc, e.g.

LONG FAR PASCAL WndProc(HWND     hWnd,
                        unsigned uMsg,
                        WPARAM   wParam,
                        LPARAM   lParam)
{
  //Process messages  
   switch(uMsg)
   {
    case WM_CREATE:
                    CreateActions():
                    break;

    case WM_DESTROY:
                    DestroyActions():
                    break;        

    }

// ...

}
0
obgAuthor Commented:
Yes, that much I've figured out myself, but I meant my Sleep() and ProcessSomething() stuff... A thread, or...?
0
jkrCommented:
Yes, a thread would be an idea. You could e.g.

// global
HANDLE g_hThread = NULL;

void CreateActions()
{
  g_hThread = CreateThread ( NULL, 0, WorkerThread, NULL, 0, &g_dwTID);

// error checking omitted
}

void WINAPI WorkerThread( LPVOID)
{
  for (;;)
  {
    Sleep ( MY_INTERVAL);
    ProcessSomething();
  }
}
0
jkrCommented:
Hmm, is your problem solved then?
0
obgAuthor Commented:
I think so, yes. Thanks very much for your help! If you post an answer, I'll give you the score (if I don't run into something wierd along the way...)
0
obgAuthor Commented:
I think so, yes. Thanks very much for your help! If you post an answer, I'll give you the score (if I don't run into something wierd along the way...)
0
jkrCommented:
Thanx! Feel free to ask if you need more information!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
obgAuthor Commented:
Ok. I've used the _beginthread and _endthread functions before. Do you know any disadvantages with them? (Don't know if you can comment now, but anyway...)

Thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.