Ejemplo n.º 1
0
int WINAPI WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow)
{
	mainInstance = hInstance;
	mainIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));

	int argc = __argc;
	char** argv = __argv;
	vector<string> jobs;
	string jobsFile;
	bool quiet = false;

	for (int i = 1; i < argc; i++)
	{
		string arg = argv[i];
		if (arg == "-appPath" && argc > i+1)
		{
			i++;
			appPath = argv[i];
		}
		else if (arg == "-exePath" && argc > i+1)
		{
			i++;
			exePath = argv[i];
		}
		else if (arg == "-updateFile" && argc > i+1)
		{
			i++;
			updateFile = argv[i];
			jobs.push_back("update");
		}
		else if (arg == "-quiet")
		{
			quiet = true;
		}
		else
		{
			jobsFile = arg;
		}
	}

	if (appPath.empty() || exePath.empty())
	{
		ShowError("The installer was not given enough information to continue.");
		return __LINE__;
	}

	if (updateFile.empty())
	{
		app = Application::NewApplication(appPath);
	}
	else
	{
		app = Application::NewApplication(updateFile, appPath);
	}

	if (app.isNull())
	{
		ShowError("The installer could not read the application manifest.");
		return __LINE__;
	}

	if (!updateFile.empty())
	{
		appInstallPath = app->path;
	}
	else
	{
		appInstallPath = GetDefaultInstallationDirectory();
	}

	componentInstallPath = FileUtils::GetSystemRuntimeHomeDirectory();

	// Read all jobs from the jobs file
	if (!jobsFile.empty() && FileUtils::IsFile(jobsFile))
	{
		std::ifstream file(jobsFile.c_str());
		if (!file.bad() && !file.fail() && !file.eof())
		{
			string line;
			while(!std::getline(file, line).eof())
			{
				jobs.push_back(line);
			}
		}
	}

	// Major WTF here, Redmond.
	LoadLibrary(TEXT("Riched20.dll"));
	CoInitialize(NULL);

	if (!quiet)
	{
		HWND introDialog = CreateDialog(
			hInstance,
			MAKEINTRESOURCE(IDD_INTRODIALOG),
			0,
			DialogProc);

		if (!introDialog)
		{
			int i = GetLastError();
			ShowError("The installer could not create the introductory dialog.");
			return __LINE__;
		}

		MSG msg;
		int status;
		while ((status = GetMessage(&msg, 0, 0, 0)) != 0)
		{
			if (status == -1)
			{
				char buf[2000];
				sprintf(buf, "Error: %i", GetLastError());
				ShowError(buf);
				return -1;
			}
			if (!IsDialogMessage(introDialog, &msg))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
	}
	else
	{
		doInstall = true;
	}

	if (doInstall)
	{
		Progress *p = new Progress;
		p->SetLineText(1, app->name, false);
		p->Show();
		bool success = 
			InstallApplication(p) &&
			HandleAllJobs(jobs, p) &&
			FinishInstallation();
		CoUninitialize();
		return success ? 0 : 1;
	}
	else
	{
		CoUninitialize();
		return 1;
	}
}