我正在尝试创建一个Win32程序,该程序可以删除特定路径中的临时文件。

但是我实际上在代码中的安装/卸载功能上遇到了麻烦。

我正在使用Visual Studio 2012,当我启动调试过程以测试代码时,调试窗口将立即关闭。

也许问题不是来自VS 2012,而是我的代码?

这里是:

/*
** INCLUDES
*/
#include <Windows.h>
#include <stdio.h>

/*
** MACROS
*/
# define MY_SERVICE_NAME "DiskCleaner"
# define MY_SERVICE_DISPLAY_NAME "Nettoyeur de disque"
SERVICE_STATUS my_ServiceStatus;
SERVICE_STATUS_HANDLE my_ServiceStatusHandle;
BOOL bRunning;
BOOL InstallMyService();
BOOL DeleteMyService();
void WINAPI ServiceCtrlHandler(DWORD Opcode);
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);

/* INSTALL THE SERVICE */
BOOL InstallMyService()
{
    char strDir[MAX_PATH];
    SC_HANDLE hSCManager;
    SC_HANDLE hService;
    LPCTSTR lpBinaryPathName;

    GetCurrentDirectory(MAX_PATH, strDir);
    strcat(strDir, "\\AutoClean.exe");

    hSCManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);

    if (hSCManager == NULL)
        return (FALSE);

    lpBinaryPathName=strDir;

    hService = CreateService(
        hSCManager,                             MY_SERVICE_NAME,
        MY_SERVICE_DISPLAY_NAME,                SERVICE_ALL_ACCESS,
        SERVICE_WIN32_OWN_PROCESS,
        SERVICE_AUTO_START,
        SERVICE_ERROR_NORMAL,
        lpBinaryPathName,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL);

    if (hService == NULL)
        return (FALSE);

    CloseServiceHandle(hService);
    return (TRUE);
}

/* DELETE THE SERVICE */
BOOL DeleteMyService()
{
    SC_HANDLE hSCManager;
    SC_HANDLE hService;

    hSCManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);

    if (hSCManager == NULL)
        return (FALSE);

    hService = OpenService (hSCManager, MY_SERVICE_NAME, SERVICE_ALL_ACCESS);

    if (hService == NULL)
        return (FALSE);

    if (DeleteService(hService) == NULL)
        return (FALSE);

    if (CloseServiceHandle(hService) == NULL)
        return (FALSE);

    return (TRUE);
}

/* SERVICE EVENT HANDLER */
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
    switch (Opcode)
    {
    case SERVICE_CONTROL_PAUSE:
        my_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
        break;
    case SERVICE_CONTROL_CONTINUE:
        my_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
        break;
    case SERVICE_CONTROL_STOP:
        my_ServiceStatus.dwWin32ExitCode = 0;
        my_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
        my_ServiceStatus.dwCheckPoint = 0;
        my_ServiceStatus.dwWaitHint = 0;
        SetServiceStatus (my_ServiceStatusHandle, &my_ServiceStatus);
        bRunning = FALSE;
        break;
    case SERVICE_CONTROL_INTERROGATE:
        break;
    default:
        break;
    }
    return;
}


/* SERVICE ENTRY POINT */
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
    DWORD Status;
    DWORD SpecificError;

    my_ServiceStatus.dwServiceType = SERVICE_WIN32;
    my_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
    my_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
    my_ServiceStatus.dwWin32ExitCode = 0;
    my_ServiceStatus.dwServiceSpecificExitCode = 0;
    my_ServiceStatus.dwCheckPoint = 0;
    my_ServiceStatus.dwWaitHint = 0;
    my_ServiceStatusHandle = RegisterServiceCtrlHandler(MY_SERVICE_NAME, ServiceCtrlHandler);

    if (my_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
    {
        return;
    }

    my_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
    my_ServiceStatus.dwCheckPoint = 0;
    my_ServiceStatus.dwWaitHint = 0;
    SetServiceStatus(my_ServiceStatusHandle, &my_ServiceStatus);
    bRunning = TRUE;

    while (bRunning)
    {
        printf("\nOK just to test if this part works well before implementing my cleaning part\n");
    }
    return;
}

/* ENTRY POINT */
int main(int argc, char* argv[])
{
    if (argc > 1)
    {
        if (strcmp(argv[1], "-i"))
        {
            if (InstallMyService())
                printf("\nService successfully installed\n");
            else
                printf("\nService installtion unsuccessfull\n");
        }
        else if (strcmp(argv[1], "-d"))
        {
            if (DeleteMyService())
                printf("\nService successfully deleted\n");
            else
                printf("\nService delete unsuccesfull\n");
        }
        else
            printf("\nUsage incorrect\n");
    }
    else
    {
        SERVICE_TABLE_ENTRY DispatchTable[]={{MY_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
        StartServiceCtrlDispatcher(DispatchTable);
    }

    return (EXIT_SUCCESS);
}

最佳答案

在主要功能中,您会遇到以下错误:

if (strcmp(argv[1], "-i"))

if (strcmp(argv[1], "-d"))


此条件为FALSE,因此不会执行您的InstallMyService()/ DeleteMyService()。

同样,出于复杂性考虑,您忘记了关闭服务管理器句柄。

关于c - 如何在Win32中安装/卸载Windows服务?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15691150/

10-11 03:35
查看更多