-1

我正在尝试创建一个可以删除特定路径中的临时文件的 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);
}
4

2 回答 2

1

在您的主要功能中,您有错误:

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

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

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

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

于 2013-03-29T15:12:37.813 回答
-1

我已经更正了我的代码,它现在可以工作了。这是整个解决方案:

#include <Windows.h>
#include <stdio.h>

/*
** MACROS
*/
# define MY_SERVICE_NAME "DiskCleaner"
# define MY_SERVICE_DESCRIPTOR "Nettoyeur de fichiers temporaires"
# define MY_SERVICE_BIN_NAME "DiskCleaner.exe"

/*
** GLOBALS
*/
SERVICE_STATUS g_ServiceStatus;
SERVICE_STATUS_HANDLE g_ServiceStatusHandle;

/*
** INSTALL THE SERVICE
*/
BOOL InstallMyService()
{
  char strDir[MAX_PATH + 1];
  SC_HANDLE schSCManager;
  SC_HANDLE schService;
  LPCTSTR lpszBinaryPathName;

  GetCurrentDirectory(MAX_PATH, strDir);
  strcat(strDir, "\\"MY_SERVICE_BIN_NAME);
  if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
    return (TRUE);
  lpszBinaryPathName = strDir;
  schService = CreateService(schSCManager, MY_SERVICE_NAME, MY_SERVICE_DESCRIPTOR,
                             SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,     SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
                             lpszBinaryPathName, NULL, NULL, NULL, NULL, NULL);
  if (schService == NULL)
    return (FALSE);
  CloseServiceHandle(schService);
  return (TRUE);
}

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

  if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
return (FALSE);
  if ((hService = OpenService(schSCManager, MY_SERVICE_NAME, SERVICE_ALL_ACCESS)) == NULL)
    return (FALSE);
  if (!DeleteService(hService))
    return (FALSE);
  if (!CloseServiceHandle(hService))
    return (FALSE);
  return (TRUE);
}

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

/*
** SERVICE MAIN ENTRY
*/
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
  g_ServiceStatus.dwServiceType = SERVICE_WIN32;
  g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
  g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  g_ServiceStatus.dwWin32ExitCode = 0;
  g_ServiceStatus.dwServiceSpecificExitCode = 0;
  g_ServiceStatus.dwCheckPoint = 0;
  g_ServiceStatus.dwWaitHint = 0;
  g_ServiceStatusHandle = RegisterServiceCtrlHandler(MY_SERVICE_NAME, ServiceCtrlHandler);
  if (g_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
    return;
  g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
  g_ServiceStatus.dwCheckPoint = 0;
  g_ServiceStatus.dwWaitHint = 0;
  SetServiceStatus(g_ServiceStatusHandle, &g_ServiceStatus);

}

/*
** MAIN ENTRY
*/
int main(int argc, char* argv[])
{
  if (argc > 1)
  {
    if (!strcmp(argv[1], "-i"))
       if(InstallMyService())
           printf("\nService correctly installed\n");
   else
       printf("\nService uncorrectly installed\n");

    else if (!strcmp(argv[1], "-d"))
       if(DeleteMyService())
       printf("\nService correctly deleted\n");
   else
       printf("\nService uncorrectly deleted\n");
  }
  else
  {
     SERVICE_TABLE_ENTRY DispatchTable[]={{MY_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
 StartServiceCtrlDispatcher(DispatchTable);
  }
  return (EXIT_SUCCESS);
}
于 2013-04-05T19:39:58.043 回答