-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUpdateCheck.cpp
More file actions
239 lines (190 loc) · 6.37 KB
/
UpdateCheck.cpp
File metadata and controls
239 lines (190 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// UpdateCheck.cpp
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "UpdateCheck.h"
#include "resource.h"
//#ifdef _DEBUG
//#undef THIS_FILE
//static char THIS_FILE[]=__FILE__;
//#define new DEBUG_NEW
//#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CUpdateCheck::CUpdateCheck()
{
}
CUpdateCheck::~CUpdateCheck()
{
}
BOOL CUpdateCheck::GetFileVersion(DWORD &dwMS, DWORD &dwLS)
{
char szModuleFileName[MAX_PATH];
LPBYTE lpVersionData;
if (GetModuleFileName(AfxGetInstanceHandle(), szModuleFileName, sizeof(szModuleFileName)) == 0) return FALSE;
DWORD dwHandle;
DWORD dwDataSize = ::GetFileVersionInfoSize(szModuleFileName, &dwHandle);
if ( dwDataSize == 0 )
return FALSE;
lpVersionData = new BYTE[dwDataSize];
if (!::GetFileVersionInfo(szModuleFileName, dwHandle, dwDataSize, (void**)lpVersionData) )
{
delete [] lpVersionData;
return FALSE;
}
ASSERT(lpVersionData != NULL);
UINT nQuerySize;
VS_FIXEDFILEINFO* pVsffi;
if ( ::VerQueryValue((void **)lpVersionData, _T("\\"),
(void**)&pVsffi, &nQuerySize) )
{
dwMS = pVsffi->dwFileVersionMS;
dwLS = pVsffi->dwFileVersionLS;
delete [] lpVersionData;
return TRUE;
}
delete [] lpVersionData;
return FALSE;
}
void CUpdateCheck::Check(UINT uiURL)
{
CString strURL(MAKEINTRESOURCE(uiURL));
Check(strURL);
}
void CUpdateCheck::Check(const CString& strURL)
{
DWORD dwMS, dwLS;
if (!GetFileVersion(dwMS, dwLS))
{
ASSERT(FALSE);
return;
}
CWaitCursor wait;
//check for a connection
BOOL connected = FALSE;
DWORD flags;
connected = InternetGetConnectedState(&flags, NULL);
if(!connected)
{
if(m_Quiet) return;
}
HINTERNET hInet = InternetOpen(UPDATECHECK_BROWSER_STRING, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL);
HINTERNET hUrl = InternetOpenUrl(hInet, strURL, NULL, -1L,
INTERNET_FLAG_RELOAD | INTERNET_FLAG_PRAGMA_NOCACHE |
INTERNET_FLAG_NO_CACHE_WRITE |WININET_API_FLAG_ASYNC, NULL);
if (hUrl)
{
char szBuffer[512];
DWORD dwRead;
if (InternetReadFile(hUrl, szBuffer, sizeof(szBuffer), &dwRead))
{
if (dwRead > 500) dwRead = 0; //abort if 404 or any other page retrived
if (dwRead > 0)
{
szBuffer[dwRead] = 0;
CString strSubMS1;
CString strSubMS2;
CString strSub;
DWORD dwMSWeb;
//Contents of file updatecnaad.txt are 1|0|https://github.com/oormicreations/naad/releases
AfxExtractSubString(strSubMS1, szBuffer, 0, '|'); //major ver num
AfxExtractSubString(strSubMS2, szBuffer, 1, '|'); //minor ver num
dwMSWeb = MAKELONG((WORD) atol(strSubMS2), (WORD) atol(strSubMS1));
if (dwMSWeb > dwMS)
{
AfxExtractSubString(strSub, szBuffer, 2, '|'); //download url
MsgUpdateAvailable(dwMS, dwLS, dwMSWeb, 0, strSub);
}
else
MsgUpdateNotAvailable(dwMS, dwLS);
}
else
MsgUpdateNoCheck(dwMS, dwLS);
}
InternetCloseHandle(hUrl);
}
else
MsgUpdateNoCheck(dwMS, dwLS);
InternetCloseHandle(hInet);
}
HINSTANCE CUpdateCheck::GotoURL(LPCTSTR url, int showcmd)
{
TCHAR key[MAX_PATH + MAX_PATH];
// First try ShellExecute()
HINSTANCE result = ShellExecute(NULL, _T("open"), url, NULL,NULL, showcmd);
// If it failed, get the .htm regkey and lookup the program
if ((UINT)result <= HINSTANCE_ERROR)
{
if (GetRegKey(HKEY_CLASSES_ROOT, _T(".htm"), key) == ERROR_SUCCESS)
{
lstrcat(key, _T("\\shell\\open\\command"));
if (GetRegKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS)
{
TCHAR *pos;
pos = _tcsstr(key, _T("\"%1\""));
if (pos == NULL) { // No quotes found
pos = _tcsstr(key, _T("%1")); // Check for %1, without quotes
if (pos == NULL) // No parameter at all...
pos = key+lstrlen(key)-1;
else
*pos = '\0'; // Remove the parameter
}
else
*pos = '\0'; // Remove the parameter
lstrcat(pos, _T(" "));
lstrcat(pos, url);
result = (HINSTANCE) WinExec(key,showcmd);
}
}
}
return result;
}
LONG CUpdateCheck::GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata)
{
HKEY hkey;
LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);
if (retval == ERROR_SUCCESS)
{
long datasize = MAX_PATH;
TCHAR data[MAX_PATH];
RegQueryValue(hkey, NULL, data, &datasize);
lstrcpy(retdata,data);
RegCloseKey(hkey);
}
return retval;
}
void CUpdateCheck::MsgUpdateAvailable(DWORD dwMSlocal, DWORD dwLSlocal, DWORD dwMSWeb, DWORD dwLSWeb, const CString& strURL)
{
CString strMessage;
strMessage.Format(IDS_UPDATE_AVAILABLE, HIWORD(dwMSlocal), LOWORD(dwMSlocal), HIWORD(dwMSWeb), LOWORD(dwMSWeb));
if (AfxMessageBox(strMessage, MB_YESNO|MB_ICONINFORMATION) == IDYES)
GotoURL(strURL, SW_SHOW);
}
void CUpdateCheck::MsgUpdateNotAvailable(DWORD dwMSlocal, DWORD dwLSlocal)
{
if(!m_Quiet) AfxMessageBox(IDS_UPDATE_NO, MB_OK|MB_ICONINFORMATION);
}
void CUpdateCheck::MsgUpdateNoCheck(DWORD dwMSlocal, DWORD dwLSlocal)
{
if(!m_Quiet) AfxMessageBox(IDS_UPDATE_NOCHECK, MB_OK|MB_ICONINFORMATION);
}
void CUpdateCheck::SendUsageData()
{
#ifndef _DEBUG
static TCHAR frmdata[] = "appinstall=naad_install&ver=1.2.0";
#else
static TCHAR frmdata[] = "appinstall=naad_install_D&ver=1.2.0";
#endif
static TCHAR hdrs[] = ("Content-Type: application/x-www-form-urlencoded");
static LPCSTR accept[2] = { "*/*", NULL };
HINTERNET hInternet = InternetOpen("Naad Usage", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hSession = InternetConnect(hInternet, "oormi.in", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hReq = HttpOpenRequest(hSession, "POST", "software/selftalkmsg/selftalkstat01.php", NULL, NULL, accept, INTERNET_FLAG_SECURE, 1);
//DWORD x = strlen(hdrs);
//not working in release ver
BOOL res = HttpSendRequest(hReq, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
InternetCloseHandle(hReq);
InternetCloseHandle(hSession);
InternetCloseHandle(hInternet);
}