2

The application do not need any window or console. It can be a console application if the console window can be removed or can be put into background. The application will do some simple tasks(such as cleaning rubbish files) and exit.

  1. I hope that the application should not be a windows service if possible.

  2. I hope that the application can be started with double-click in explorer window and run silently.

  3. When the application is running, I hope that the mouse cursor should not be changed to hourglass shape. That is to say, do not disturb the user when the application is running. Just run and exit silently.

  4. DO NOT NEED to run it when windows starts or user logins.

I hope this can be done in C if possible. What should I do?

task_finished = 0;
CreateThread(NULL, 65535, task_thread, para, 0, NULL);

MSG msg;
while(!task_finished){
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
        if(task_finished)
            break;
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}
0

2 Answers 2

2

IIRC, you can do this by just writing a WinMain function that doesn't create or display a window or console. Lambert's answer about using a message loop will help you if you want your program to be able to send or receive messages from other programs, but if you're just doing simple background processing all you should need is a WinMain that doesn't make a window.

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried to replace "int main(int argc, char **argv)" with "int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)". and compile the code with gcc -mwindows. But I can still see the hourglass mouse cursor after I double-click and run the application in explorer.
On the other hand, I found that the hourglass will disappear after a few or more seconds either main or WinMain is used. The program will continue to run silently until it exit. I hope the hourglass will disappear at once, just as when the application with GetMessage.
PeekMessage does not work at all. PeekMessage return at once and the hourglass appear a few or more seconds just as without PeekMessage. But a single GetMessage seemed can let the hourglass disappear at one. I am not sure whether the hourglass will appear again after the GetMesssage return because GetMessage will block forever.
1

All you need is a message loop -- just use this code (modified from here):

MSG msg;
BOOL bRet; 
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    } 
} 

Edit:

For I/O or other tasks, you can either (1) create another thread, and do everything from there, or (2) Use MsgWaitForMultipleObjects inside the loop (before the next GetMessage() call) to simultaneously wait on your I/O and on messages, so that you can be notified when there's either a message, or when your I/O is finished.

Warning:

I haven't figured out how to make the application not bring up the hourglass shape when starting. If you need that functionality, consider creating a Windows service instead. You can also compile as a console application, then use FreeConsole to hide the window.

13 Comments

Where Should I put the IO code? It seems that the GetMessage will block here.
Good question; see my edit. :) Huh, a question: If all your application does is I/O, then why don't you just do that? You might not even need a message loop, if it's a one-time thing and it doesn't process messages.
I also come to consider the thread. But it is a little heavy if there are are methods: such as (if possible) send some type of message to notify the system that this program will be runned in background without interacting with the user? Thanks. When the GetMessage block, the system hourglass will disappear. FreeConsole, interesting. Now trying.
I do not need to interactive with the user. The application only do I/O tasks. Yes, I hope it can do not use message loop if possible. I hope it can be easy to use and as simple as possible, so service was not considered.
Thanks. FreeConsole works. But there will be a flicker: the console window will appear and hide at once. Is it possible to hide the console window before it appear?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.