如何用C创建一个后台没有窗口的win32应用程序?

发布于 2024-10-10 06:07:42 字数 664 浏览 2 评论 0原文

该应用程序不需要任何窗口或控制台。如果可以删除控制台窗口或可以将其置于后台,则它可以是控制台应用程序。应用程序将执行一些简单的任务(例如清理垃圾文件)并退出。

  1. 如果可能的话,我希望应用程序不应该是 Windows 服务。

  2. 我希望应用程序可以在资源管理器窗口中双击启动并静默运行。

  3. 当应用程序运行时,我希望鼠标光标不要变成沙漏形状。也就是说,应用程序运行时不要打扰用户。只需运行并默默退出即可。

  4. 不需要在 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.

  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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的十字路口 2024-10-17 06:07:42

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.

浅暮の光 2024-10-17 06:07:42

您所需要的只是一个消息循环 - 只需使用此代码(修改自 此处):

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); 
    } 
} 

编辑:

对于 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):

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文