public int RunMain(string[] args) { Application.ThreadException += HandleThreadException; using (SingleInstance singleInstance = new SingleInstance()) { if (singleInstance.IsFirstInstance()) { try { Run(); } catch (Exception e) { LocalStaticLogger.WriteLine(e.ToString()); } } else { singleInstance.BringFirstInstanceToFront(); } } LocalStaticLogger.Stop(); return(0); }
protected virtual IntPtr InternalHookProc(int code, IntPtr wParam, IntPtr lParam) { IntPtr result = UNHANDLED; if (pfnClientHookProc != null) { try { // try to ensure CallNextHookEx is always called, even if client // hook proc code or MarhsalToStructure except result = pfnClientHookProc(code, wParam, lParam); } catch (System.Exception ex) { LocalStaticLogger.WriteLine(ex.ToString()); } } try { // only call CallNextHook if client hook proc did not run or did run but didn't return handled if (result == UNHANDLED) { result = Win32HookAPI.CallNextHookEx(hHook, code, wParam, lParam); } } catch (System.Exception ex) { LocalStaticLogger.WriteLine(ex.ToString()); } return(result); }
protected bool Invoke(CallbackArguments arguments) { if (ThreadPool.QueueUserWorkItem(new WaitCallback(CallbackInvoker), arguments)) { activeCallbackCount++; LocalStaticLogger.WriteLine("CallbackInvocation().Invoke(): activeCallbackCount= " + activeCallbackCount); return(true); } return(false); }
protected void Save(Uri path) { try { Uri uri = new Uri(path.ToFileSystemPath() + "." + Extension); storage.Save(this as T, uri); } catch (Exception e) { LocalStaticLogger.WriteLine(e.ToString()); } }
protected override IntPtr InternalHookProc(int code, IntPtr wParam, IntPtr lParam) { try { bool processed = ThreadPool.QueueUserWorkItem(new WaitCallback(ClientHookProcInvoker), new WindowHookProcArgs(code, wParam, lParam)); } catch (NotSupportedException nse) { LocalStaticLogger.WriteLine(nse.ToString()); } return(Win32HookAPI.CallNextHookEx(hHook, code, wParam, lParam)); }
public static bool UnhookWindowsHook(IntPtr hHook) { bool result = false; try { result = Win32HookAPI.UnhookWindowsHookEx(hHook); } catch (System.Exception ex) { LocalStaticLogger.WriteLine(ex.ToString()); } return(result); }
protected static T Load(Uri filePath) { T file = default(T); try { file = storage.Load(filePath); } catch (Exception e) { file = FormattedFile <T, S, R> .Empty; LocalStaticLogger.WriteLine(e.ToString()); } return(file); }
public void Save(T obj, Uri path) { try { using (var writeStream = GetWriteStream(path)) { if (writeStream != null) { serializer.WriteObject(obj, writeStream); } } } catch (Exception e) { LocalStaticLogger.WriteLine(e.ToString()); } }
public static Response Post(Uri url, NameValueCollection parameters) { using (var client = new WebClient()) { Response response = null; try { byte[] bytes = client.UploadValues(url, parameters); response = new Response(bytes); } catch (Exception e) { LocalStaticLogger.WriteLine("HttpRequest.Post(" + url + ") - " + e.ToString()); } return(response); } }
public static Image ScaleImage(Image image, int width, int height) { double aspectRatio = image.Width / image.Height; double boxRatio = width / height; double scaleFactor = 0.0; //Use height, since that is the most restrictive dimension of box. if (boxRatio > aspectRatio) { scaleFactor = (double)height / (double)image.Height; } else { scaleFactor = (double)width / (double)image.Width; } int newWidth = (int)(image.Width * scaleFactor); int newHeight = (int)(image.Height * scaleFactor); Image newImage = null; using (Bitmap buffer = new Bitmap((int)newWidth, newHeight)) { using (Graphics g = Graphics.FromImage(buffer)) { g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.DrawImage(image, 0, 0, newWidth, newHeight); } try { newImage = buffer.Clone() as Image; } catch (Exception e) { LocalStaticLogger.WriteLine(e.ToString()); } } return(newImage); }
public T CreateInstance(string typeName, object[] args) { foreach (var type in subTypes) { if (type.FullName == typeName) { try { return(Activator.CreateInstance(type, args) as T); } catch (Exception e) { LocalStaticLogger.WriteLine(e.ToString()); } } } return(null); }
protected virtual IntPtr LowLevelKeyboardHookProc(HookCode hc, KeyEvent keyEvent, KeyEventInfo info) { try { string line = "\r\nHC: [" + hc.ToString() + "]\r\nKEY_EVENT: [" + keyEvent.ToString() + "]\r\n" + info.ToString(); LocalStaticLogger.WriteLine(line); Console.Out.WriteLine(line); foreach (var remappedKey in mappings.Keys) { // we fudge equality here, comparing a KeyPress to a KeyEvent, technically // they are not so much equal as equivalent if (info == remappedKey) { var mappedKey = mappings[remappedKey]; if (mappedKey != null) { if (keyEvent == KeyEvent.WM_KEYDOWN) { if (KeyEventSimulator.SimulateKeyDownAsync(mappedKey.VkCode, mappedKey.ScanCode, mappedKey.Extended)) { return(LowLevelKeyboardHook.HANDLED); } } else if (keyEvent == KeyEvent.WM_KEYUP) { if (KeyEventSimulator.SimulateKeyUpAsync(mappedKey.VkCode, mappedKey.ScanCode, mappedKey.Extended)) { return(LowLevelKeyboardHook.HANDLED); } } } } } } catch (Exception e) { LocalStaticLogger.WriteLine(e.ToString()); } return(IntPtr.Zero); }
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms644985%28v=vs.85%29.aspx: // If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. // If nCode is greater than or equal to zero, and the hook procedure did not process the message, it is highly // recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that // have installed WH_KEYBOARD_LL hooks will not receive hook notifications and may behave incorrectly as a result. // If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing // the message to the rest of the hook chain or the target window procedure. protected override IntPtr InternalHookProc(int code, IntPtr wParam, IntPtr lParam) { IntPtr result = IntPtr.Zero; // protect this instance from ever calling InternalHookProc // at the same time lock (lockObj) { if (code >= 0) { if (pfnClientHookProc != null) { try { // protected ourselves from exceptions thrown in client hook procedures LowLevelKeyboardHookProcArgs args = new LowLevelKeyboardHookProcArgs(code, wParam, lParam); result = pfnClientHookProc(args.HookCode, args.KeyEvent, args.Info); } catch (Exception e) { LocalStaticLogger.WriteLine(e.ToString()); } } } try { // try to ensure CallNextHookEx is always called, even if client // hook proc code or MarhsalToStructure except if (result == IntPtr.Zero) { result = Win32HookAPI.CallNextHookEx(hHook, code, wParam, lParam); } } catch (Exception e) { LocalStaticLogger.WriteLine(e.ToString()); } } return(result); }
protected virtual void ClientHookProcInvoker(object data) { WindowHookProcArgs args = data as WindowHookProcArgs; if (args != null) { if (pfnClientHookProc != null) { try { // no way to pass return code back IntPtr result = pfnClientHookProc(args.Code, args.WParam, args.LParam); } catch (System.Exception ex) { LocalStaticLogger.WriteLine(ex.ToString()); } } } }
public static Response Get(Uri url, WebHeaderCollection headers) { using (var client = new WebClient()) { Response response = null; try { if (headers != null && headers.Count > 0) { client.Headers.Add(headers); } var bytes = client.DownloadData(url); response = new Response(bytes); } catch (Exception e) { LocalStaticLogger.WriteLine("HttpRequest.Get(" + url + ") - " + e.ToString()); } return(response); } }
public static IntPtr SetWindowsHook(HookType hookType, WindowHookProc lpfnHookProc) { try { using (var currentProcess = Process.GetCurrentProcess()) { using (var currentModule = currentProcess.MainModule) { IntPtr hModule = Win32HookAPI.GetModuleHandle(currentModule.ModuleName); if (hModule != IntPtr.Zero) { return(Win32HookAPI.SetWindowsHookEx(hookType, lpfnHookProc, hModule, 0)); } } } } catch (System.Exception ex) { LocalStaticLogger.WriteLine(ex.ToString()); } return(IntPtr.Zero); }
protected void UpdateUI() { try { listView.Items.Clear(); foreach (var file in Directory.GetFiles(selectedDirectory, string.Format("*.{0}", FileExtension))) { string name = Path.GetFileName(file).Replace("." + FileExtension, string.Empty); var item = new ListViewItem(name); item.Tag = Path.Combine(selectedDirectory, file); listView.Items.Add(item); } listView.Enabled = true; directoryTextBox.Text = selectedDirectory; } catch (Exception e) { listView.Enabled = false; LocalStaticLogger.WriteLine(e.ToString()); } }
public static Type[] GetInterfaceSubclassTypes(string assemblyFile, Type iface) { List <Type> types = new List <Type>(); try { Assembly assembly = Assembly.LoadFrom(assemblyFile); foreach (var type in assembly.GetTypes()) { foreach (var i in type.GetInterfaces()) { if (i == iface) { types.Add(type); } } } } catch (Exception e) { LocalStaticLogger.WriteLine(e.ToString()); } return(types.ToArray()); }
protected virtual void HandleThreadProcException(Exception e) { LocalStaticLogger.WriteLine(e.ToString()); }
static void HandleThreadException(object sender, ThreadExceptionEventArgs e) { LocalStaticLogger.WriteLine(e.ToString()); }
protected virtual void CallbackInvoker(object state) { Callback(); activeCallbackCount--; LocalStaticLogger.WriteLine("CallbackInvocation().CallbackInvoker(): activeCallbackCount= " + activeCallbackCount); }
protected override void CallbackInvoker(object state) { Callback(state as T); activeCallbackCount--; LocalStaticLogger.WriteLine("CallbackInvocation().CallbackInvoker(): activeCallbackCount= " + activeCallbackCount); }