Example #1
0
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	if (_tcsstr(lpCmdLine, _T("--restart")))
	{
		TCHAR *stop, *spc = _tcsrchr(lpCmdLine, _T(' '));
		DWORD pid = _tcstoul(spc + 1, &stop, 10);

		HANDLE hproc = OpenProcess(SYNCHRONIZE, FALSE, pid);
		WaitForSingleObject(hproc, INFINITE);
		CloseHandle(hproc);
	}
	else
	{
		CLimitSingleInstance limit(_T("__enso_portable__"));

		if (limit.IsAnotherInstanceRunning())
		{
			return 0;
		}
	}


	TCHAR *point = NULL;
	TCHAR module_name[MAX_PATH];
	GetModuleFileName(hInstance, module_name, MAX_PATH);

	size_t module_name_len = lstrlen(module_name);

	// Python path
	TCHAR python_path[MAX_PATH];
	TCHAR exec_dir[MAX_PATH];
	lstrcpyn(python_path, module_name, module_name_len);

	point = _tcsrchr(python_path, _T('\\'));
	*(++point) = NULL;

	_tcscpy(exec_dir, python_path);

	SetEnvironmentVariable(_T("PYTHONPATH"), python_path);

	_tcscpy(point, _T("python\\pythonw.exe"));

	// Enso startup script path
	TCHAR enso_executable_path[MAX_PATH];
	lstrcpyn(enso_executable_path, module_name, module_name_len);

	point = _tcsrchr(enso_executable_path, _T('\\'));
	_tcscpy(point + 1, _T("scripts\\run_enso.py"));

#ifndef PORTABLE
	if (_tcsstr(lpCmdLine, _T("--portable")))
	{
#endif
		point = _tcsrchr(exec_dir, _T('\\'));
		*point = NULL;

		SetEnvironmentVariable(_T("HOME"), exec_dir);
#ifndef PORTABLE
	}
#endif


    return LaunchTarget(python_path, enso_executable_path, exec_dir);
}
Example #2
0
int __cdecl wmain(
    __in int argc,
    __in WCHAR * argv[]
    )
{
    HRESULT hr = S_OK;
    LPWSTR pwzCommandLine = NULL;

    LPWSTR wzAppId = NULL;
    GUID guidApp;

    DWORD64 dw64Version;
    LPWSTR pwzFeedUri = NULL;
    LPWSTR pwzApplicationPath = NULL;
    LPWSTR pwzApplicationDirectory = NULL;

    DWORD64 dw64NextUpdateTime = 0;
    BOOL fUpdateReady = FALSE;
    DWORD64 dw64UpdateVersion = 0;
    LPWSTR pwzFeedPath = NULL;
    LPWSTR pwzSetupPath = NULL;

    DWORD dwTimeToLive = 0;
    LPWSTR pwzApplicationId = NULL;
    LPWSTR pwzApplicationSource = NULL;

    BOOL bDeleteUpdateInfoPath = FALSE;
    BOOL bDeleteUpdateBinaryPath = FALSE;
    HANDLE hProcess = INVALID_HANDLE_VALUE;
    HANDLE hUpdateMutex = INVALID_HANDLE_VALUE;

    //
    // Process command-line arguments.
    //
    for (int i=1; i<argc; i++)
    {
        if (argv[i][0] == L'-' || argv[i][0] == L'/')
        {
            if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, &argv[i][1], -1, L"ac", -1))
            {
                if (wzAppId)
                {
                    ExitOnFailure(hr = E_INVALIDARG, "May only specify one -ac switch.");
                }

                wzAppId = argv[++i];
                hr = ::CLSIDFromString(wzAppId, &guidApp);
                ExitOnFailure(hr, "Failed to parse the -ac argument.");
            }
        }
        else
        {
            ExitOnFailure1(hr = E_INVALIDARG, "Bad commandline argument: %S", argv[i]);
        }
    }
    ExitOnNull(wzAppId, hr, E_INVALIDARG, "Must specify a -ac switch.");

    hr = GetUpdateMutex(&guidApp, &hUpdateMutex);
    if (FAILED(hr))
    {
        TraceError(hr, "Failed to query the update mutex.  Proceeding as if this process didn't acquire the mutex.");
    }

    hr = RssUpdateGetAppInfo(wzAppId, &dw64Version, &pwzFeedUri, &pwzApplicationPath);
    ExitOnFailure(hr, "Failed to get app info.");

    // If we acquired the update lock and there is already an update downloaded, install that now.
    if (INVALID_HANDLE_VALUE != hUpdateMutex)
    {
        Trace(REPORT_DEBUG, "Got the update mutex.  Will check for updates on local machine before launching app.");

        // If an update is available and higher version that the application currently on the local 
        // machine, launch the install and bail.
        hr = RssUpdateTryLaunchUpdate(wzAppId, dw64Version, &hProcess, &dw64NextUpdateTime);
        if (SUCCEEDED(hr))
        {
            if (hProcess)
            {
                ::CloseHandle(hProcess);
                ExitFunction(); // bail since we're doing an update
            }
        }
    }
    else
    {
        Trace(REPORT_DEBUG, "Didn't get the update mutex.  Won't check for updates.");
    }

    hr = PathExpand(&pwzCommandLine, pwzApplicationPath, PATH_EXPAND_FULLPATH);
    ExitOnFailure(hr, "Failed to expand application path.");

    if (pwzCommandLine && L'\"' != pwzCommandLine[0])
    {
        // Get the working directory.
        hr = PathGetDirectory(pwzCommandLine, &pwzApplicationDirectory);
        ExitOnFailure(hr, "Failed to get application directory from command-line.");

        // Put quotes around the command line.
        hr = StrAllocPrefix(&pwzCommandLine, L"\"", 0);
        ExitOnFailure(hr, "Failed to prefix command-line with quote.");

        hr = StrAllocConcat(&pwzCommandLine, L"\"", 0);
        ExitOnFailure(hr, "Failed to concat command-line with quote.");
    }

    Trace1(REPORT_DEBUG, "Launching the target app with commandline: %ls.", pwzCommandLine);
    hr = LaunchTarget(pwzCommandLine, pwzApplicationDirectory, &hProcess);
    ExitOnFailure1(hr, "Failed to launch %ls", pwzCommandLine);

    // If we acquired the update lock then check to see if enough time has passed such that we look for more updates.
    if (INVALID_HANDLE_VALUE != hUpdateMutex)
    {
        hr = RssUpdateCheckFeed(wzAppId, dw64Version, pwzFeedUri, dw64NextUpdateTime);

        hr = S_OK;
    }

LExit:
    if (INVALID_HANDLE_VALUE != hUpdateMutex)
    {
        ::CloseHandle(hUpdateMutex);
    }

    if (INVALID_HANDLE_VALUE != hProcess)
    {
        ::CloseHandle(hProcess);
    }

    if (bDeleteUpdateInfoPath)
    {
        ::DeleteFileW(pwzFeedPath);
    }

    if (bDeleteUpdateBinaryPath)
    {
        ::DeleteFileW(pwzSetupPath);
    }

    ReleaseStr(pwzApplicationSource);
    ReleaseStr(pwzApplicationId);
    ReleaseStr(pwzSetupPath);
    ReleaseStr(pwzFeedPath);
    ReleaseStr(pwzApplicationPath);
    ReleaseStr(pwzFeedUri);
    ReleaseStr(pwzCommandLine);
    ReleaseStr(pwzApplicationDirectory);

    return SCODE_CODE(hr);
}