Example #1
0
//	PURPOSE:  This function is called by the SCM whenever ControlService() is called on this service.  The
//		SCM does not start the service through this function.
void WINAPI CNTService::service_ctrl(DWORD dwCtrlCode) // static
{
	if ( dwCtrlCode == SERVICE_CONTROL_STOP )
		g_Service.ServiceStop();
	g_Service.SetServiceStatus(g_Service.m_sStatus.dwCurrentState, NO_ERROR, 0);
}
Example #2
0
/////////////////////////////////////////////////////////////////////////////////////
//
//	FUNCTION: main()
//
/////////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);

	TCHAR	*argv[32];
	argv[0] = NULL;
	int argc = Str_ParseCmds(lpCmdLine, &argv[1], COUNTOF(argv)-1, " \t") + 1;

	if ( GRAY_GetOSInfo()->dwPlatformId != VER_PLATFORM_WIN32_NT )
	{
		// We are running Win9x - So we are not an NT service.
do_not_nt_service:
		NTWindow_Init(hInstance, lpCmdLine, nCmdShow);
		int iRet = Sphere_MainEntryPoint(argc, argv);
		NTWindow_Exit();
		TerminateProcess(GetCurrentProcess(), iRet);
		return iRet;
	}

	// We need to find out what the server name is....look it up in the .ini file
	if ( !g_Cfg.LoadIni(true) )
	{
		// Try to determine the name and path of this application.
		char szPath[_MAX_PATH];

		GetModuleFileName(NULL, szPath, sizeof(szPath));

		if ( !szPath[0] )
			return -2;

		ExtractPath(szPath);
		g_Cfg.LoadIni(false);
	}

	if ( !g_Cfg.m_fUseNTService )	// since there is no way to detect how did we start, use config for that
		goto do_not_nt_service;

	g_Service.SetServiceStatus(SERVICE_START_PENDING, NO_ERROR, 5000);

	// process the command line arguments...
	if (( argc > 1 ) && _IS_SWITCH(*argv[1]) )
	{
		if ( argv[1][1] == 'k' )		// service control
		{
			if ( argc < 3 )
			{
				printf("Use \"-k command\" with operation to proceed (install/remove)\n");
			}
			else if ( !strcmp(argv[2], "install") )
			{
				g_Service.CmdInstallService();
			}
			else if ( !strcmp(argv[2], "remove") )
			{
				g_Service.CmdRemoveService();
			}
			return 0;
		}
	}

	// If the argument does not match any of the above parameters, the Service Control Manager (SCM) may
	// be attempting to start the service, so we must call StartServiceCtrlDispatcher.
	g_Service.ReportEvent(EVENTLOG_INFORMATION_TYPE, 0, "Starting Service.");

	g_Service.CmdMainStart();
	g_Service.SetServiceStatus(SERVICE_STOPPED, NO_ERROR, 0);
	return -1;
}