forked from JFC-Group/JFC-microsip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicrosip.cpp
More file actions
335 lines (295 loc) · 9.55 KB
/
microsip.cpp
File metadata and controls
335 lines (295 loc) · 9.55 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Copyright (C) 2011-2024 MicroSIP (http://www.microsip.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// microsip.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "microsip.h"
#include "mainDlg.h"
#include "const.h"
#include "settings.h"
#include "langpack.h"
#include "Strsafe.h"
#include <afxinet.h>
#include <Psapi.h>
#include <Dbghelp.h>
#pragma comment(lib, "Psapi")
#pragma comment(lib, "Dbghelp")
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CmicrosipApp
BEGIN_MESSAGE_MAP(CmicrosipApp, CWinAppEx)
ON_COMMAND(ID_HELP, &CWinAppEx::OnHelp)
END_MESSAGE_MAP()
void RecursiveDelete(CString path)
{
if (!path.IsEmpty()) {
CFileFind ff;
if (path.Right(1) == _T("\\")) {
path = path.Mid(0, path.GetLength() - 1);
}
BOOL res = ff.FindFile(path);
while (res) {
res = ff.FindNextFile();
if (!ff.IsDots()) {
if (ff.IsDirectory()) {
path = ff.GetFilePath();
CString path1 = path;
path1.Append(_T("\\*.*"));
RecursiveDelete(path1);
RemoveDirectory(path);
}
else {
DeleteFile(ff.GetFilePath());
}
}
}
}
}
// CmicrosipApp construction
CmicrosipApp::CmicrosipApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CmicrosipApp object
CmicrosipApp theApp;
// CmicrosipApp initialization
CStringA wineVersion() {
static const char * (CDECL *pwine_get_version)(void);
HMODULE hntdll = GetModuleHandle(_T("ntdll.dll"));
if (hntdll) {
pwine_get_version = (const char* (*)())(void *)GetProcAddress(hntdll, "wine_get_version");
if (pwine_get_version) {
return pwine_get_version();
}
}
return "n/a";
}
LONG WINAPI ExceptionFilter(EXCEPTION_POINTERS *ExceptionInfo)
{
CTime tm = CTime::GetCurrentTime();
bool sent = false;
CString blockFileName = accountSettings.pathLocal;
blockFileName.AppendFormat(_T("block_%d.%d.%d.%d.dat"), _GLOBAL_VERSION_COMMA);
bool blockDump = PathFileExists(blockFileName);
if (!blockDump) {
CString filename;
CFile file;
// mini dump
filename.Format(_T("%sdump%s.dmp"), accountSettings.pathLocal, tm.Format(_T("_%Y-%m-%d_%H-%M-%S")));
if (file.Open(filename, CFile::modeCreate | CFile::modeReadWrite)) {
MINIDUMP_EXCEPTION_INFORMATION MinidumpExceptionInfo;
MinidumpExceptionInfo.ThreadId = GetCurrentThreadId();
MinidumpExceptionInfo.ExceptionPointers = ExceptionInfo;
MinidumpExceptionInfo.ClientPointers = FALSE;
if (MiniDumpWriteDump(
GetCurrentProcess(),
GetCurrentProcessId(),
file.m_hFile,
MiniDumpNormal,
//MINIDUMP_TYPE(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory | MiniDumpWithFullMemory),
&MinidumpExceptionInfo,
NULL,
NULL
)) {
}
file.Close();
}
}
if (pj_ready && pjsua_var.state == PJSUA_STATE_RUNNING && tm.GetTime() - startTime.GetTime() > 10) {
// automatic restart after sip crash
ShellExecute(NULL, NULL, accountSettings.exeFile, NULL, NULL, SW_SHOWDEFAULT);
return EXCEPTION_EXECUTE_HANDLER;
}
else {
CString message;
if (blockDump) {
CString caption;
caption.Format(_T("%s %s"), _T(_GLOBAL_NAME_NICE), Translate(_T("Update Available")));
message.Format(_T("%s %s"),
Translate(_T("The current version is unstable on your system. You should update to the latest version.")),
Translate(_T("Do you want to update now?"))
);
if (::MessageBox(NULL, message, caption, MB_YESNO | MB_ICONQUESTION) == IDYES) {
MSIP::OpenURL(_T("https://www.microsip.org/downloads"));
}
}
else {
#ifdef _GLOBAL_VIDEO
message.Format(_T("A crash happened. Check your video card drivers or update DirectX. Try to install the LITE version (without video). Tracking info: %s%s"), tm.Format(_T("%Y%m%d%H%M%S")), sent ? _T("Y") : _T("N"));
#else
message.Format(_T("A crash happened. Make sure your system is healthy and you have enough free memory and hard disk space. You can try to uninstall Microsip \"with configuration\", and install it again. If this is a softphone bug, you can contact us to help fix it. Tracking info: %s%s"), tm.Format(_T("%Y%m%d%H%M%S")), sent ? _T("Y") : _T("N"));
#endif
AfxMessageBox(message, MB_ICONERROR);
}
return EXCEPTION_EXECUTE_HANDLER;
}
return EXCEPTION_CONTINUE_SEARCH;
}
struct MsipEnumWindowsProcData {
HINSTANCE hInst;
HWND hWnd;
int count;
};
BOOL CALLBACK MsipEnumWindowsProc(HWND hWnd, LPARAM lParam)
{
MsipEnumWindowsProcData *data = (MsipEnumWindowsProcData *)lParam;
HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
if (hInstance && hInstance == data->hInst && GetWindow(hWnd, GW_OWNER) == (HWND)0) {
TCHAR className[256];
if (GetClassName(hWnd, className, 256)) {
if (StrCmp(className, _T(_GLOBAL_NAME)) == 0) {
//--
DWORD dwProcessID;
GetWindowThreadProcessId(hWnd, &dwProcessID);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ, FALSE, dwProcessID);
if (hProcess) {
TCHAR exeFilePath[MAX_PATH];
if (GetModuleFileNameEx(hProcess, NULL, exeFilePath, MAX_PATH)) {
if (StrCmpI (exeFilePath, accountSettings.exeFile) == 0) {
data->hWnd = hWnd;
data->count++;
return FALSE;
}
}
CloseHandle(hProcess);
}
//--
}
}
}
return TRUE;
}
BOOL CmicrosipApp::InitInstance()
{
accountSettings.Init();
SetUnhandledExceptionFilter(ExceptionFilter);
MsipEnumWindowsProcData data;
data.hInst = AfxGetInstanceHandle();
data.count = 0;
HWND hWndRunning = NULL;
EnumWindows(MsipEnumWindowsProc, (LPARAM)&data);
if (data.count) {
hWndRunning = data.hWnd;
}
//*((char*)NULL) = 0; //produce a crash!!
bool cmdReset = lstrcmp(theApp.m_lpCmdLine, _T("/reset")) == 0;
if (cmdReset) {
if (hWndRunning) {
::SendMessage(hWndRunning, WM_CLOSE, NULL, NULL);
}
if (AfxMessageBox(Translate(_T("Do you want to delete user data and program settings? This action cannot be undone.")), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2 | MB_SYSTEMMODAL) == IDYES) {
if (AfxMessageBox(Translate(_T("Are you sure you want to delete?")), MB_YESNO | MB_ICONQUESTION | MB_SYSTEMMODAL) == IDYES) {
RecursiveDelete(accountSettings.appDataRoaming);
if (!::PathFileExists(accountSettings.appDataLocal + _T("Uninstall.exe"))) {
RecursiveDelete(accountSettings.appDataLocal);
}
}
}
return FALSE;
}
bool cmdExit = lstrcmp(theApp.m_lpCmdLine, _T("/exit")) == 0;
if (cmdExit) {
if (hWndRunning) {
::SendMessage(hWndRunning, WM_CLOSE, NULL, NULL);
}
return FALSE;
}
if (hWndRunning) {
if ( lstrcmp(theApp.m_lpCmdLine, _T("/minimized"))==0) {
} else if ( lstrcmp(theApp.m_lpCmdLine, _T("/hidden"))==0) {
} else {
bool activate = true;
if (lstrlen(theApp.m_lpCmdLine)) {
COPYDATASTRUCT cd;
cd.dwData = 1;
cd.lpData = theApp.m_lpCmdLine;
cd.cbData = sizeof(TCHAR) * (lstrlen(theApp.m_lpCmdLine) + 1);
activate = ::SendMessage(hWndRunning, WM_COPYDATA, NULL, (LPARAM)&cd);
}
if (activate && !MACRO_SILENT) {
::ShowWindow(hWndRunning, SW_SHOW);
::SetForegroundWindow(hWndRunning);
}
}
return FALSE;
} else {
if (lstrcmp(theApp.m_lpCmdLine, _T("/answer")) == 0
|| lstrcmp(theApp.m_lpCmdLine, _T("/hangupall")) == 0
) {
return FALSE;
}
}
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
// Set this to include all the common control classes you want to use
// in your application.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
InitCtrls.dwICC = ICC_LISTVIEW_CLASSES |
ICC_LINK_CLASS |
ICC_BAR_CLASSES |
ICC_LINK_CLASS |
ICC_STANDARD_CLASSES |
ICC_TAB_CLASSES |
ICC_UPDOWN_CLASS;
InitCommonControlsEx(&InitCtrls);
// Initialize OLE libraries (need this for Virtual Display)
if (!AfxOleInit())
{
AfxMessageBox(_T("OLE initialization failed. Make sure that the OLE libraries are the correct version."));
return FALSE;
}
AfxEnableControlContainer();
// disabled, conflict with AfxOleInit
//if (CoInitializeEx(NULL, COINIT_MULTITHREADED) != S_OK)
//{
// AfxMessageBox(_T("COM initialization failed."));
// return FALSE;
//}
AfxInitRichEdit2();
InitShellManager();
WNDCLASS wc;
// Get the info for this class.
// #32770 is the default class name for dialogs boxes.
::GetClassInfo(AfxGetInstanceHandle(), L"#32770", &wc);
wc.lpszClassName = _T(_GLOBAL_NAME);
// Register this class so that MFC can use it.
if (!::AfxRegisterClass(&wc)) return FALSE;
CmainDlg *mainDlg = new CmainDlg;
m_pMainWnd = mainDlg;
if (!m_pMainWnd) {
// return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
if (!mainDlg->GetDlgItem(IDC_MAIN_TAB)) {
DWORD error = GetLastError();
CString message;
message.Format(_T("Window creation failed. Error %d"), error);
AfxMessageBox(message, MB_ICONERROR);
return FALSE;
}
//--
mainDlg->OnCreated();
//--
return TRUE;
}