// Decompiled with JetBrains decompiler
// Type: CheckRunedCopy.SingleInstance
// Assembly: FileCash_v3.0, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 1DECC99D-34C5-4E02-A85D-DC744B5C555A
// Assembly location: C:\Users\Administrateur\Downloads\Virusshare-00001-msil\Constructor.MSIL.ArchSMS.a-33cc8dceb24ed9df287e1f92d02773477af42b8d2b6de53a45bb9b5a3ead070b.exe

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace CheckRunedCopy
{
  internal static class SingleInstance
  {
    private const int WM_COPYDATA = 74;
    private static readonly bool isNew;
    private static readonly string guid;
    private static Mutex _mutex;

    static SingleInstance()
    {
      using (Process currentProcess = Process.GetCurrentProcess())
        SingleInstance.guid = string.Format("[{0}]", (object) currentProcess.ProcessName);
      if (SingleInstance._mutex != null)
        return;
      SingleInstance._mutex = new Mutex(true, SingleInstance.guid, out SingleInstance.isNew);
    }

    public static bool IsFirstRun => SingleInstance.isNew;

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool ShowWindow(IntPtr hWnd, SingleInstance.ShowWindowCommand nCmdShow);

    [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

    public static void ShowWindow(string windowName) => SingleInstance.ShowWindow(SingleInstance.FindWindowByCaption(IntPtr.Zero, windowName));

    public static void ShowWindow(IntPtr handleWindow)
    {
      SingleInstance.ShowWindow(handleWindow, SingleInstance.ShowWindowCommand.Restore);
      SingleInstance.SetForegroundWindow(handleWindow);
    }

    public static string MessageToString(Message m) => ((SingleInstance.COPYDATASTRUCT) Marshal.PtrToStructure(m.LParam, typeof (SingleInstance.COPYDATASTRUCT))).lpData;

    public static string[] MessageToArray(Message m) => ((SingleInstance.COPYDATASTRUCT) Marshal.PtrToStructure(m.LParam, typeof (SingleInstance.COPYDATASTRUCT))).lpData.Split(new char[1]
    {
      ' '
    }, StringSplitOptions.RemoveEmptyEntries);

    public static void SendArgs(string windowName, string[] args) => SingleInstance.SendArgs(SingleInstance.FindWindowByCaption(IntPtr.Zero, windowName), args);

    public static void SendArgs(IntPtr handle, string[] args)
    {
      if (handle == IntPtr.Zero || args == null || args.Length <= 0)
        return;
      string str = "";
      for (int index = 0; index < args.Length; ++index)
        str = str + " " + args[index];
      SingleInstance.COPYDATASTRUCT structure = new SingleInstance.COPYDATASTRUCT()
      {
        lpData = str,
        cbData = str.Length + 1
      };
      IntPtr num = Marshal.AllocHGlobal(Marshal.SizeOf((object) structure));
      Marshal.StructureToPtr((object) structure, num, false);
      SingleInstance.SendMessage(handle, 74, IntPtr.Zero, num);
      Marshal.FreeHGlobal(num);
    }

    private struct COPYDATASTRUCT
    {
      public int dwData;
      public int cbData;
      [MarshalAs(UnmanagedType.LPStr)]
      public string lpData;
    }

    private enum ShowWindowCommand
    {
      Hide = 0,
      Normal = 1,
      ShowMinimized = 2,
      Maximize = 3,
      ShowMaximized = 3,
      ShowNoActivate = 4,
      Show = 5,
      Minimize = 6,
      ShowMinNoActive = 7,
      ShowNA = 8,
      Restore = 9,
      ShowDefault = 10, // 0x0000000A
      ForceMinimize = 11, // 0x0000000B
    }
  }
}
