Exemplo n.º 1
0
HRESULT	InitializeLoggingQueue()
{
    HRESULT hr = S_OK;

    if(WZCLOG_UNINITIALIZED == g_LogMsgQueue.dwState)
    {
        InitializeCriticalSection(&g_LogMsgQueue.Lock);
        g_LogMsgQueue.dwNewMsgIdx 	= -1;
        g_LogMsgQueue.dwOldMsgIdx 	= -1;

        EnterCriticalSection(&g_LogMsgQueue.Lock);

        LoadStateFromRegistry();
        g_LogMsgQueue.dwState		= WZCLOG_INACTIVE;

        LeaveCriticalSection(&g_LogMsgQueue.Lock);
    }

    return hr;

} // InitializeLoggingQueue
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	g_hInstance = hInstance;


	LogString( "vmpi_service_ui startup.\n" );

	// Don't run multiple instances.	
	HANDLE hMutex = CreateMutex( NULL, FALSE, "vmpi_service_ui_mutex" );
	if ( hMutex && GetLastError() == ERROR_ALREADY_EXISTS )
		return 1;


	// Hook spew output.
	SpewOutputFunc( MySpewOutputFunc );
	InitConsoleWindow();
	LogString( "Setup console window.\n" );

	LoadStateFromRegistry();

	// Setup the popup menu.
	if( !LoadPopupMenu() )
	{
		return false;
	}
	UpdatePopupMenuState();


	// Setup the tray icon.
	Msg( "Waiting for jobs...\n" );
	if ( !g_ShellIconMgr.Init( &g_ShellIconMgrHelper, g_pIconTooltip, MYWM_NOTIFYICON, IDI_WAITING_ICON ) )
	{
		return false;
	}


	// Connect to the VMPI service.
	g_ConnMgr.InitClient();

	LogString( "Entering main loop.\n" );
	
	while ( 1 )
	{
		MSG msg;
		msg.message = !WM_QUIT;	// So it doesn't accidentally exit.
		while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
		{
			if ( msg.message == WM_QUIT )
				break;
			
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		if ( msg.message == WM_QUIT )
			break;
	
		g_ConnMgr.Update();
		if ( !g_ConnMgr.IsConnected() )
		{
			g_ShellIconMgr.ChangeIcon( IDI_UNCONNECTED );
		}

		Sleep( 30 );
	}

	// Important that we call this instead of letting the destructor do it because it deletes its 
	// socket and it needs to cleanup some threads.
	g_ConnMgr.Term();
	
	g_ShellIconMgr.Term();
	
	return 0;
}
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	// Hook spew output.
	SpewOutputFunc( MySpewOutputFunc );

	// Get access to the registry..
	RegCreateKey( HKEY_LOCAL_MACHINE, VMPI_SERVICE_KEY, &g_hVMPIServiceKey );

	// Setup our version string.
	LoadString( hInstance, VMPI_SERVICE_IDS_VERSION_STRING, g_VersionString, sizeof( g_VersionString ) );

	// Setup the base app path.
	if ( !GetModuleFileName( GetModuleHandle( NULL ), g_BaseAppPath, sizeof( g_BaseAppPath ) ) )
	{
		Warning( "GetModuleFileName failed.\n" );
		return false;
	}
	V_StripLastDir( g_BaseAppPath, sizeof( g_BaseAppPath ) );

	// Setup the cache path.
	V_ComposeFileName( g_BaseAppPath, "vmpi_service_cache", g_FileCachePath, sizeof( g_FileCachePath ) );


	const char *pArg = FindArg( __argc, __argv, "-mpi_pw", NULL );
	SetPassword( pArg );
	
	if ( FindArg( __argc, __argv, "-console" ) )
	{					
		g_RunMode = RUNMODE_CONSOLE;
	}
	else
	{
		g_RunMode = RUNMODE_SERVICE;
	}

	if ( FindArg( __argc, __argv, "-superdebug" ) )
		g_bSuperDebugMode = true;

	g_AppStartTime = GetTickCount();
	g_bMinimized = FindArg( __argc, __argv, "-minimized" ) != NULL;

	ServiceHelpers_Init(); 	
	g_hInstance = hInstance;

	LoadStateFromRegistry();

	// Install the service?
	if ( g_RunMode == RUNMODE_CONSOLE )
	{					
		RunAsNonServiceApp();
	}
	else
	{
		RunService();
	}

	return 0;
}