Newer
Older
JumpListUtil / ShortcutUtil / TTLib.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace NativeHelpers
{
	public static class TTLib
	{

		// Errors returned by TTLib_Init
		public enum InitResult : UInt32
		{
			OK = 0,
			ERR_INIT_ALREADY_INITIALIZED,
			ERR_INIT_REGISTER_MESSAGE,
			ERR_INIT_FILE_MAPPING,
			ERR_INIT_VIEW_MAPPING,
		}

		// Errors returned by TTLib_LoadIntoExplorer
		public enum LoadIntoExplorerResult : UInt32
		{
			OK = 0,
			ERR_EXE_NOT_INITIALIZED,
			ERR_EXE_ALREADY_LOADED,
			ERR_EXE_FIND_TASKBAR,
			ERR_EXE_OPEN_PROCESS,
			ERR_EXE_VIRTUAL_ALLOC,
			ERR_EXE_WRITE_PROC_MEM,
			ERR_EXE_CREATE_REMOTE_THREAD,
			ERR_EXE_READ_PROC_MEM,

			ERR_INJ_BEFORE_RUN = 101,
			ERR_INJ_BEFORE_GETMODULEHANDLE,
			ERR_INJ_BEFORE_LOADLIBRARY,
			ERR_INJ_BEFORE_GETPROCADDR,
			ERR_INJ_BEFORE_LIBINIT,
			ERR_INJ_GETMODULEHANDLE,
			ERR_INJ_LOADLIBRARY,
			ERR_INJ_GETPROCADDR,

			ERR_LIB_INIT_ALREADY_CALLED = 201,
			ERR_LIB_LIB_VER_MISMATCH,
			ERR_LIB_WIN_VER_MISMATCH,
			ERR_LIB_VIEW_MAPPING,
			ERR_LIB_FIND_IMPORT,
			ERR_LIB_WND_TASKBAR,
			ERR_LIB_WND_TASKSW,
			ERR_LIB_WND_TASKLIST,
			ERR_LIB_WND_THUMB,
			ERR_LIB_MSG_DLL_INIT,
			ERR_LIB_WAITTHREAD,

			ERR_LIB_EXTHREAD_MINHOOK = 301,
			ERR_LIB_EXTHREAD_MINHOOK_PRELOADED,
			ERR_LIB_EXTHREAD_COMFUNCHOOK,
			ERR_LIB_EXTHREAD_REFRESHTASKBAR,
			ERR_LIB_EXTHREAD_MINHOOK_APPLY,
		}

		public enum GroupType : UInt32
		{
			UNKNOWN = 0,
			NORMAL,
			PINNED,
			COMBINED,
			TEMPORARY,
		}

		public enum List : UInt32
		{
			LABEL = 0,
			GROUP,
			GROUPPINNED,
			COMBINE,
		}

		public enum ListValue : UInt32
		{
			LABEL_NEVER = 0,
			LABEL_ALWAYS,

			GROUP_NEVER = 0,
			GROUP_ALWAYS,

			GROUPPINNED_NEVER = 0,
			GROUPPINNED_ALWAYS,

			COMBINE_NEVER = 0,
			COMBINE_ALWAYS,
		}

		[StructLayout(LayoutKind.Sequential)]
		public struct RECT
		{
			public int Left, Top, Right, Bottom;

			public RECT(int left, int top, int right, int bottom)
			{
				Left = left;
				Top = top;
				Right = right;
				Bottom = bottom;
			}

			public RECT(System.Drawing.Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom) { }

			public int X
			{
				get { return Left; }
				set { Right -= (Left - value); Left = value; }
			}

			public int Y
			{
				get { return Top; }
				set { Bottom -= (Top - value); Top = value; }
			}

			public int Height
			{
				get { return Bottom - Top; }
				set { Bottom = value + Top; }
			}

			public int Width
			{
				get { return Right - Left; }
				set { Right = value + Left; }
			}

			public System.Drawing.Point Location
			{
				get { return new System.Drawing.Point(Left, Top); }
				set { X = value.X; Y = value.Y; }
			}

			public System.Drawing.Size Size
			{
				get { return new System.Drawing.Size(Width, Height); }
				set { Width = value.Width; Height = value.Height; }
			}

			public static implicit operator System.Drawing.Rectangle(RECT r)
			{
				return new System.Drawing.Rectangle(r.Left, r.Top, r.Width, r.Height);
			}

			public static implicit operator RECT(System.Drawing.Rectangle r)
			{
				return new RECT(r);
			}

			public static bool operator ==(RECT r1, RECT r2)
			{
				return r1.Equals(r2);
			}

			public static bool operator !=(RECT r1, RECT r2)
			{
				return !r1.Equals(r2);
			}

			public bool Equals(RECT r)
			{
				return r.Left == Left && r.Top == Top && r.Right == Right && r.Bottom == Bottom;
			}

			public override bool Equals(object obj)
			{
				if (obj is RECT)
					return Equals((RECT)obj);
				else if (obj is System.Drawing.Rectangle)
					return Equals(new RECT((System.Drawing.Rectangle)obj));
				return false;
			}

			public override int GetHashCode()
			{
				return ((System.Drawing.Rectangle)this).GetHashCode();
			}

			public override string ToString()
			{
				return string.Format(System.Globalization.CultureInfo.CurrentCulture, "{{Left={0},Top={1},Right={2},Bottom={3}}}", Left, Top, Right, Bottom);
			}
		}

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_Init", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern InitResult Init();

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_Uninit", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool Uninit();

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_LoadIntoExplorer", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern LoadIntoExplorerResult LoadIntoExplorer();

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_IsLoadedIntoExplorer", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool IsLoadedIntoExplorer();

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_UnloadFromExplorer", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool UnloadFromExplorer();

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_ManipulationStart", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool ManipulationStart();

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_ManipulationEnd", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool ManipulationEnd();

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetMainTaskbar", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern IntPtr GetMainTaskbar();

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetSecondaryTaskbarCount", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool GetSecondaryTaskbarCount(out Int32 pnCount);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetSecondaryTaskbar", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern IntPtr GetSecondaryTaskbar(Int32 nIndex);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetTaskListWindow", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern IntPtr GetTaskListWindow(IntPtr hTaskbar);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetTaskbarWindow", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern IntPtr GetTaskbarWindow(IntPtr hTaskbar);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetTaskbarMonitor", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern IntPtr GetTaskbarMonitor(IntPtr hTaskbar);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetButtonGroupCount", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool GetButtonGroupCount(IntPtr hTaskbar, out Int32 pnCount);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetButtonGroup", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern IntPtr GetButtonGroup(IntPtr hTaskbar, Int32 nIndex);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetActiveButtonGroup", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern IntPtr GetActiveButtonGroup(IntPtr hTaskbar);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetTrackedButtonGroup", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern IntPtr GetTrackedButtonGroup(IntPtr hTaskbar);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_ButtonGroupMove", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool ButtonGroupMove(IntPtr hTaskbar, Int32 nIndexFrom, Int32 nIndexTo);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetButtonGroupTaskbar", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool GetButtonGroupTaskbar(IntPtr hButtonGroup, out IntPtr phTaskbar);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetButtonGroupRect", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool GetButtonGroupRect(IntPtr hButtonGroup, out RECT pRect);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetButtonGroupType", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool GetButtonGroupType(IntPtr hButtonGroup, out GroupType pnType);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetButtonGroupAppId", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
		public static extern UInt32 GetButtonGroupAppId(IntPtr hButtonGroup, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszAppId, UInt32 nMaxSize);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetButtonCount", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool GetButtonCount(IntPtr hButtonGroup, out Int32 pnCount);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetButton", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern IntPtr GetButton(IntPtr hButtonGroup, Int32 nIndex);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetActiveButton", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern IntPtr GetActiveButton(IntPtr hTaskbar);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetTrackedButton", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern IntPtr GetTrackedButton(IntPtr hTaskbar);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_ButtonMoveInButtonGroup", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool ButtonMoveInButtonGroup(IntPtr hButtonGroup, Int32 nIndexFrom, Int32 nIndexTo);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetButtonWindow", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		public static extern IntPtr GetButtonWindow(IntPtr hButton);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_AddAppIdToList", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool AddAppIdToList(List nList, [MarshalAs(UnmanagedType.LPWStr)] string pszAppId, ListValue nListValue);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_RemoveAppIdFromList", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool RemoveAppIdFromList(List nList, [MarshalAs(UnmanagedType.LPWStr)] string pszAppId);

		[DllImport("ttlib32.dll", EntryPoint = "TTLib_GetAppIdListValue", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
		[return: MarshalAs(UnmanagedType.U1)]
		public static extern bool GetAppIdListValue(List nList, [MarshalAs(UnmanagedType.LPWStr)] string pszAppId, out ListValue pnListValue);
	}
}