00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include <string>
00008
00009 HANDLE StartProcess (LPCSTR pszName, LPCSTR pszArgs)
00010 {
00011 STARTUPINFO si;
00012 PROCESS_INFORMATION pi;
00013
00014 ZeroMemory (&si, sizeof (si));
00015 si.cb = sizeof (si);
00016 ZeroMemory (&pi, sizeof (pi));
00017
00018 char sz [MAX_PATH] = "\"";
00019 lstrcat (sz, pszName);
00020 lstrcat (sz, "\"");
00021
00022 if (pszArgs && *pszArgs)
00023 {
00024 lstrcat (sz, " ");
00025 lstrcat (sz, pszArgs);
00026 }
00027
00028 if (FALSE == CreateProcess (NULL, sz, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
00029 return NULL;
00030
00031 return pi.hProcess;
00032 }
00033
00034 int APIENTRY WinMain(HINSTANCE hInstance,
00035 HINSTANCE hPrevInstance,
00036 LPSTR lpCmdLine,
00037 int nCmdShow)
00038 {
00039 HANDLE hMutex;
00040 DWORD dwLastError;
00041
00042
00043
00044 if (*lpCmdLine++ != '"')
00045 return 0;
00046
00047 std::string strProcessExe, strAppMutex, strUpgradeExe, strUpgradeExeArgs,
00048 strDeleteDistribFlags;
00049
00050 while (*lpCmdLine && *lpCmdLine != '"')
00051 strProcessExe += *lpCmdLine++;
00052 if (*lpCmdLine == 0)
00053 return 0;
00054 lpCmdLine += 2;
00055
00056 if (*lpCmdLine++ != '"')
00057 return 0;
00058 while (*lpCmdLine && *lpCmdLine != '"')
00059 strAppMutex += *lpCmdLine++;
00060 if (*lpCmdLine == 0)
00061 return 0;
00062 lpCmdLine += 2;
00063
00064 if (*lpCmdLine++ != '"')
00065 return 0;
00066 while (*lpCmdLine && *lpCmdLine != '"')
00067 strUpgradeExe += *lpCmdLine++;
00068 if (*lpCmdLine == 0)
00069 return 0;
00070 lpCmdLine += 2;
00071
00072 if (*lpCmdLine++ != '"')
00073 return 0;
00074 while (*lpCmdLine && *lpCmdLine != '"')
00075 strUpgradeExeArgs += *lpCmdLine++;
00076 if (*lpCmdLine == 0)
00077 return 0;
00078 lpCmdLine += 2;
00079
00080 if (*lpCmdLine++ != '"')
00081 return 0;
00082 while (*lpCmdLine && *lpCmdLine != '"')
00083 strDeleteDistribFlags += *lpCmdLine++;
00084
00085 if (strProcessExe == "" || strAppMutex == "" || strUpgradeExe == "")
00086 return 0;
00087
00088
00089 do
00090 {
00091 hMutex = CreateMutex (NULL, TRUE, strAppMutex.c_str ());
00092
00093 dwLastError = GetLastError ();
00094 CloseHandle (hMutex);
00095
00096 Sleep (300);
00097 }
00098 while (dwLastError == ERROR_ALREADY_EXISTS);
00099
00100
00101
00102 HANDLE hUpdate = StartProcess (strUpgradeExe.c_str (), strUpgradeExeArgs.c_str ());
00103
00104 if (hUpdate == NULL)
00105 MessageBox (NULL, "Can't find upgrade distributive file.", "Update error", MB_ICONERROR);
00106 else
00107 WaitForSingleObject (hUpdate, INFINITE);
00108
00109 if (strDeleteDistribFlags == "1")
00110 DeleteFile (strUpgradeExe.c_str ());
00111
00112
00113 StartProcess (strProcessExe.c_str (), NULL);
00114
00115 return 0;
00116 }
00117