int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	hInst = hInstance;

	if (!CheckSingleInstance()) return 1;
	
	HANDLE hSplashBitmap = static_cast<HBITMAP>(LoadImage(hInst, MAKEINTRESOURCE(IDB_SPLASH), IMAGE_BITMAP, 0, 0, 0));
	if (hSplashBitmap)
	{
		CreateThread(NULL, 0, SplashScreenThread, hSplashBitmap, 0, NULL);
	}

	if (!LocateJVM()) return 1;
	if (!LoadVMOptions()) return 1;
	if (!LoadJVMLibrary()) return 1;
	if (!CreateJVM()) return 1;
	if (!RunMainClass()) return 1;

	jvm->DestroyJavaVM();

	terminating = true;
	SetEvent(hEvent);
	WaitForSingleObject(hSingleInstanceWatcherThread, INFINITE);
	CloseHandle(hEvent);
	CloseHandle(hFileMapping);

	return 0;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);

    hInst = hInstance;

    if (__argc == 2 && _wcsicmp(__wargv[0], _T("SPLASH")) == 0)
    {
        HBITMAP hSplashBitmap = static_cast<HBITMAP>(LoadImage(hInst, MAKEINTRESOURCE(IDB_SPLASH), IMAGE_BITMAP, 0, 0, 0));
        if (hSplashBitmap)
        {
            parentProcId = _wtoi(__wargv[1]);
            parentProcHandle = OpenProcess(SYNCHRONIZE, FALSE, parentProcId);
            if (IsParentProcessRunning(parentProcHandle)) SplashScreen(hSplashBitmap);
        }
        CloseHandle(parentProcHandle);
        return 0;
    }

    //it's OK to return 0 here, because the control is transferred to the first instance
    if (!CheckSingleInstance()) return 0;

    if (nativesplash = wcsstr(lpCmdLine, _T("/nativesplash")) != NULL) StartSplashProcess();

    if (!LocateJVM()) return 1;
    if (!LoadVMOptions()) return 1;
    if (!LoadJVMLibrary()) return 1;
    if (!CreateJVM()) return 1;

    hSingleInstanceWatcherThread = CreateThread(NULL, 0, SingleInstanceThread, NULL, 0, NULL);

    if (!RunMainClass()) return 1;

    jvm->DestroyJavaVM();

    terminating = true;
    SetEvent(hEvent);
    WaitForSingleObject(hSingleInstanceWatcherThread, INFINITE);
    CloseHandle(hEvent);
    CloseHandle(hFileMapping);

    return 0;
}