Link to home
Start Free TrialLog in
Avatar of obg
obg

asked on

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?
Avatar of NickRepin
NickRepin

Create a program with a hidden main window (which is not have a WS_VISIBLE style).
Use Sleep(60000) to sleep 1 min.
Avatar of jkr
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()'...
For win9x you can make it even more hidden (that is: from the Ctrl+Alt+Del- tasklist) by calling the "RegisterServiceProcess" API...

Regards, Madshi.
Avatar of obg

ASKER

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...
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...
Avatar of obg

ASKER

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...?
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;        

    }

// ...

}
Avatar of obg

ASKER

Yes, that much I've figured out myself, but I meant my Sleep() and ProcessSomething() stuff... A thread, or...?
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();
  }
}
Hmm, is your problem solved then?
Avatar of obg

ASKER

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...)
Avatar of obg

ASKER

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...)
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of obg

ASKER

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!