-
Notifications
You must be signed in to change notification settings - Fork 5
/
XBMC_PC.cpp
203 lines (178 loc) · 6.93 KB
/
XBMC_PC.cpp
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
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.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, 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 XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "settings/AdvancedSettings.h"
#include "settings/AppParamParser.h"
#include "utils/CharsetConverter.h"
#include "utils/log.h"
#include "WIN32Util.h"
#include "shellapi.h"
#include "dbghelp.h"
#include "XBDateTime.h"
#include "threads/Thread.h"
#include "Application.h"
typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType,
CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
// Minidump creation function
LONG WINAPI CreateMiniDump( EXCEPTION_POINTERS* pEp )
{
// Create the dump file where the xbmc.exe resides
CStdString errorMsg;
CStdString dumpFile;
CDateTime now(CDateTime::GetCurrentDateTime());
dumpFile.Format("%s\\xbmc_crashlog-%04i%02i%02i-%02i%02i%02i.dmp", CWIN32Util::GetProfilePath().c_str(), now.GetYear(), now.GetMonth(), now.GetDay(), now.GetHour(), now.GetMinute(), now.GetSecond());
HANDLE hFile = CreateFile(dumpFile.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
// Call MiniDumpWriteDump api with the dump file
if ( hFile && ( hFile != INVALID_HANDLE_VALUE ) )
{
// Load the DBGHELP DLL
HMODULE hDbgHelpDll = ::LoadLibrary("DBGHELP.DLL");
if (hDbgHelpDll)
{
MINIDUMPWRITEDUMP pDump = (MINIDUMPWRITEDUMP)::GetProcAddress(hDbgHelpDll, "MiniDumpWriteDump");
if (pDump)
{
// Initialize minidump structure
MINIDUMP_EXCEPTION_INFORMATION mdei;
mdei.ThreadId = CThread::GetCurrentThreadId();
mdei.ExceptionPointers = pEp;
mdei.ClientPointers = FALSE;
// Call the minidump api with normal dumping
// We can get more detail information by using other minidump types but the dump file will be
// extermely large.
BOOL rv = pDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &mdei, 0, NULL);
if( !rv )
{
errorMsg.Format("MiniDumpWriteDump failed with error id %d", GetLastError());
MessageBox(NULL, errorMsg.c_str(), "XBMC: Error", MB_OK|MB_ICONERROR);
}
}
else
{
errorMsg.Format("MiniDumpWriteDump failed to load with error id %d", GetLastError());
MessageBox(NULL, errorMsg.c_str(), "XBMC: Error", MB_OK|MB_ICONERROR);
}
// Close the DLL
FreeLibrary(hDbgHelpDll);
}
else
{
errorMsg.Format("LoadLibrary 'DBGHELP.DLL' failed with error id %d", GetLastError());
MessageBox(NULL, errorMsg.c_str(), "XBMC: Error", MB_OK|MB_ICONERROR);
}
// Close the file
CloseHandle( hFile );
}
else
{
errorMsg.Format("CreateFile '%s' failed with error id %d", dumpFile.c_str(), GetLastError());
MessageBox(NULL, errorMsg.c_str(), "XBMC: Error", MB_OK|MB_ICONERROR);
}
return pEp->ExceptionRecord->ExceptionCode;;
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR commandLine, INT )
{
//this can't be set from CAdvancedSettings::Initialize() because it will overwrite
//the loglevel set with the --debug flag
#ifdef _DEBUG
g_advancedSettings.m_logLevel = LOG_LEVEL_DEBUG;
g_advancedSettings.m_logLevelHint = LOG_LEVEL_DEBUG;
#else
g_advancedSettings.m_logLevel = LOG_LEVEL_NORMAL;
g_advancedSettings.m_logLevelHint = LOG_LEVEL_NORMAL;
#endif
CLog::SetLogLevel(g_advancedSettings.m_logLevel);
// Initializes CreateMiniDump to handle exceptions.
SetUnhandledExceptionFilter( CreateMiniDump );
// check if XBMC is already running
CreateMutex(NULL, FALSE, "XBMC Media Center");
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
HWND m_hwnd = FindWindow("XBMC","XBMC");
if(m_hwnd != NULL)
{
// switch to the running instance
ShowWindow(m_hwnd,SW_RESTORE);
SetForegroundWindow(m_hwnd);
}
return 0;
}
#ifndef HAS_DX
if(CWIN32Util::GetDesktopColorDepth() < 32)
{
//FIXME: replace it by a SDL window for all ports
MessageBox(NULL, "Desktop Color Depth isn't 32Bit", "XBMC: Fatal Error", MB_OK|MB_ICONERROR);
return 0;
}
#endif
//Initialize COM
CoInitializeEx(NULL, COINIT_MULTITHREADED);
// Handle numeric values using the default/POSIX standard
setlocale(LC_NUMERIC, "C");
// If the command line passed to WinMain, commandLine, is not "" we need
// to process the command line arguments.
// Note that commandLine does not include the program name and can be
// equal to "" if no arguments were supplied. By contrast GetCommandLineW()
// does include the program name and is never equal to "".
g_advancedSettings.Initialize();
if (strlen(commandLine) != 0)
{
int argc;
LPWSTR* argvW = CommandLineToArgvW(GetCommandLineW(), &argc);
CStdString* strargvA = new CStdString[argc];
const char** argv = (const char**) LocalAlloc(LMEM_FIXED, argc*sizeof(char*));
for (int i = 0; i < argc; i++)
{
g_charsetConverter.wToUTF8(argvW[i], strargvA[i]);
argv[i] = strargvA[i].c_str();
}
// Parse the arguments
CAppParamParser appParamParser;
appParamParser.Parse(argv, argc);
// Clean up the storage we've used
LocalFree(argvW);
LocalFree(argv);
delete [] strargvA;
}
// Initialise Winsock
WSADATA wd;
WSAStartup(MAKEWORD(2,2), &wd);
// Create and run the app
if(!g_application.Create())
{
CStdString errorMsg;
errorMsg.Format("CApplication::Create() failed - check log file and that it is writable");
MessageBox(NULL, errorMsg.c_str(), "XBMC: Error", MB_OK|MB_ICONERROR);
return 0;
}
#ifndef _DEBUG
// we don't want to see the "no disc in drive" windows message box
SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);
#endif
g_application.Run();
// put everything in CApplication::Cleanup() since this point is never reached
return 0;
}