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