如何用C创建一个后台没有窗口的win32应用程序?
该应用程序不需要任何窗口或控制台。如果可以删除控制台窗口或可以将其置于后台,则它可以是控制台应用程序。应用程序将执行一些简单的任务(例如清理垃圾文件)并退出。
如果可能的话,我希望应用程序不应该是 Windows 服务。
我希望应用程序可以在资源管理器窗口中双击启动并静默运行。
当应用程序运行时,我希望鼠标光标不要变成沙漏形状。也就是说,应用程序运行时不要打扰用户。只需运行并默默退出即可。
不需要在 Windows 启动或用户登录时运行它。
如果可能的话,我希望这可以用 C 语言完成。我应该怎么办?
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);
}
}
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.
I hope that the application should not be a windows service if possible.
I hope that the application can be started with double-click in explorer window and run silently.
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.
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IIRC,您只需编写一个不创建或显示窗口或控制台的 WinMain 函数即可做到这一点。如果您希望您的程序能够从其他程序发送或接收消息,兰伯特关于使用消息循环的回答将为您提供帮助,但如果您只是进行简单的后台处理,您需要的只是一个不生成窗户。
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.
您所需要的只是一个消息循环 - 只需使用此代码(修改自 此处):
编辑:
对于 I/O 或其他任务,您可以 (1) 创建另一个线程,并从那里执行所有操作,或 (2) 使用 MsgWaitForMultipleObjects 在循环内(在下一个
GetMessage() 调用)同时等待 I/O 和消息,以便在有消息或 I/O 完成时通知您。
警告:
我还没有弄清楚如何使应用程序在启动时不显示沙漏形状。如果您需要该功能,请考虑创建 Windows 服务。您还可以编译为控制台应用程序,然后使用 FreeConsole 隐藏窗口。
All you need is a message loop -- just use this code (modified from here):
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.