forked from margru/xbmc-on-imon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
98 lines (83 loc) · 4.05 KB
/
Program.cs
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
using System;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
namespace iMon.XBMC
{
class MyMessageFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
return false;
}
}
static class Program
{
static Mutex mutex = new Mutex(true, Application.ProductName);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
if (mutex.WaitOne(TimeSpan.Zero, true))
{
Logging.initialize(Application.StartupPath);
Logging.Log("Application path: " + Application.StartupPath);
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new XBMC());
}
catch (Exception ex)
{
Logging.Error("Unhandled exception", ex);
MessageBox.Show("An unhandled exception has occured." + Environment.NewLine +
"Please check the debug/error log for more details",
"Unhandled Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Logging.deinitialize();
mutex.ReleaseMutex();
}
else
{
// send our Win32 message to make the currently running instance
// jump on top of all the other windows
NativeMethods.PostMessage((IntPtr) NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
//MessageBox.Show("Another instance is running." + Environment.NewLine +
// "Current process: " + Process.GetCurrentProcess().ToString() + Environment.NewLine +
// "Process by name length: " + Process.GetProcessesByName("XbmcOniMon").Length + Environment.NewLine +
// "1st process: " + (Process.GetProcessesByName("XbmcOniMon")[0].ToString()) + Environment.NewLine +
// "2nd process: " + (Process.GetProcessesByName("XbmcOniMon")[1].ToString()));
//Process[] processes = Process.GetProcessesByName("XbmcOniMon");
//foreach (Process proc in processes)
// NativeMethods.PostMessage((IntPtr) proc.MainWindowHandle, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
//NativeMethods.PostMessage((IntPtr) Process.GetProcessesByName("XbmcOniMon")[1].MainWindowHandle, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
}
}
catch (Exception ex)
{
//Logging.Error("Unhandled exception", ex);
MessageBox.Show("An unhandled exception has occured." + Environment.NewLine +
ex.ToString(),
//"Please check the debug/error log for more details",
"Unhandled Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
internal class NativeMethods
{
public const int HWND_BROADCAST = 0xffff;
public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME");
//public static readonly int WM_SHOWME = 0xc201;
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
[DllImport("user32")]
public static extern int RegisterWindowMessage(string message);
}
}