-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 に答える