Skip to content

Commit

Permalink
stop taskbar default hiding behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliviaophia committed Apr 13, 2022
1 parent e07f5ea commit 78ab275
Show file tree
Hide file tree
Showing 24 changed files with 360 additions and 3 deletions.
Binary file added Sources/SmartTaskbar.Win10.Hook/EasyHook32.dll
Binary file not shown.
Binary file not shown.
Binary file added Sources/SmartTaskbar.Win10.Hook/EasyHook64.dll
Binary file not shown.
Binary file not shown.
Binary file added Sources/SmartTaskbar.Win10.Hook/EasyLoad32.dll
Binary file not shown.
Binary file added Sources/SmartTaskbar.Win10.Hook/EasyLoad64.dll
Binary file not shown.
99 changes: 99 additions & 0 deletions Sources/SmartTaskbar.Win10.Hook/InjectionEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;
using EasyHook;

namespace SmartTaskbar.Hook
{
public class InjectionEntryPoint : IEntryPoint
{
private readonly ServerInterface _server;

public InjectionEntryPoint(RemoteHooking.IContext context,
string channelName)
{
_server = RemoteHooking.IpcConnectClient<ServerInterface>(channelName);
_server.Ping();
}

public void Run(RemoteHooking.IContext context,
string channelName)
{
_server.Ping();
var postMessageHook = LocalHook.Create(
LocalHook.GetProcAddress("user32.dll", "PostMessageW"),
new PostMessageDelegate(PostMessageHook),
this);
postMessageHook.ThreadACL.SetExclusiveACL(new[] {0});
RemoteHooking.WakeUpProcess();
try
{
while (true)
{
Thread.Sleep(1000);

if (!Mutex.TryOpenExisting("{959d3545-aa5c-42a8-a327-6e2c079daa94}", out _))
break;

_server.Ping();
}
}
finally
{
postMessageHook?.Dispose();
LocalHook.Release();
}
}

#region GetProcessId

/// <summary>
/// Retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the
/// process that created the window.
/// </summary>
/// <param name="hWnd">A handle to the window.</param>
/// <param name="lpdwProcessId">
/// A pointer to a variable that receives the process identifier. If this parameter is not
/// NULL, GetWindowThreadProcessId copies the identifier of the process to the variable; otherwise, it does not.
/// </param>
/// <returns>The return value is the identifier of the thread that created the window.</returns>
[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

#endregion

#region PostMessage

/// Return Type: BOOL->int
/// hWnd: HWND->HWND__*
/// Msg: UINT->unsigned int
/// wParam: WPARAM->UINT_PTR->unsigned int
/// lParam: LPARAM->LONG_PTR->int
[DllImport("user32.dll", EntryPoint = "PostMessageW")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private delegate bool PostMessageDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

private static bool PostMessageHook(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
try
{
if (hWnd == FindWindow("Shell_TrayWnd", null)
&& msg == 0x05D1)
return false;

return PostMessage(hWnd, msg, wParam, lParam);
}
catch { return false; }
}


[DllImport("user32.dll", CharSet = CharSet.Unicode)]
internal static extern IntPtr FindWindow(string strClassName, string strWindowName);

#endregion
}
}
35 changes: 35 additions & 0 deletions Sources/SmartTaskbar.Win10.Hook/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;

// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("SmartTaskbar.Win10.Hook")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SmartTaskbar.Win10.Hook")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("537c876f-dd20-4a8a-83fa-d87f2f6f20cb")]

// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.4.3.0")]
[assembly: AssemblyFileVersion("1.4.3.0")]
9 changes: 9 additions & 0 deletions Sources/SmartTaskbar.Win10.Hook/ServerInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace SmartTaskbar.Hook
{
public class ServerInterface : MarshalByRefObject
{
public void Ping() { }
}
}
76 changes: 76 additions & 0 deletions Sources/SmartTaskbar.Win10.Hook/SmartTaskbar.Win10.Hook.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{537C876F-DD20-4A8A-83FA-D87F2F6F20CB}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SmartTaskbar.Hook</RootNamespace>
<AssemblyName>SmartTaskbar.Hook</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EasyHook, Version=2.7.7097.0, Culture=neutral, PublicKeyToken=4b580fca19d0b0c5, processorArchitecture=MSIL">
<HintPath>..\packages\EasyHook.2.7.7097\lib\net40\EasyHook.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="InjectionEntryPoint.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServerInterface.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="EasyHook32.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="EasyHook32Svc.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="EasyHook64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="EasyHook64Svc.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="EasyLoad32.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="EasyLoad64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
4 changes: 4 additions & 0 deletions Sources/SmartTaskbar.Win10.Hook/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EasyHook" version="2.7.7097" targetFramework="net48" />
</packages>
Binary file added Sources/SmartTaskbar.Win10/EasyHook32.dll
Binary file not shown.
Binary file added Sources/SmartTaskbar.Win10/EasyHook32Svc.exe
Binary file not shown.
Binary file added Sources/SmartTaskbar.Win10/EasyHook64.dll
Binary file not shown.
Binary file added Sources/SmartTaskbar.Win10/EasyHook64Svc.exe
Binary file not shown.
Binary file added Sources/SmartTaskbar.Win10/EasyLoad32.dll
Binary file not shown.
Binary file added Sources/SmartTaskbar.Win10/EasyLoad64.dll
Binary file not shown.
53 changes: 53 additions & 0 deletions Sources/SmartTaskbar.Win10/Helpers/HookHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Ipc;
using EasyHook;
using SmartTaskbar.Hook;

namespace SmartTaskbar
{
public static class HookHelper
{
private static int _targetPid;
private static string _channelName;

private static readonly string InjectionLibrary =
Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
?? throw new InvalidOperationException(),
"SmartTaskbar.Hook.dll");

private static IpcServerChannel _channel;

public static void ReleaseHook()
{
if (_channel is null) return;

_channel.StopListening(null);

_channel = null;
_channelName = null;
}

public static void SetHook()
{
if (_channel != null)
return;

_targetPid = TaskbarHelper.GetExplorerId();

if (_targetPid == 0)
return;

_channel = RemoteHooking.IpcCreateServer<ServerInterface>(ref _channelName, WellKnownObjectMode.Singleton);

RemoteHooking.Inject(
_targetPid,
InjectionLibrary,
InjectionLibrary,
_channelName);
}
}
}
29 changes: 28 additions & 1 deletion Sources/SmartTaskbar.Win10/SmartTaskbar.Win10.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<ApplicationIcon>Resources\logo_blue_128.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="EasyHook, Version=2.7.7097.0, Culture=neutral, PublicKeyToken=4b580fca19d0b0c5, processorArchitecture=MSIL">
<HintPath>..\packages\EasyHook.2.7.7097\lib\net40\EasyHook.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Remoting" />
Expand All @@ -59,6 +62,7 @@
<ItemGroup>
<Compile Include="Helpers\AnimationHelper.cs" />
<Compile Include="Helpers\AutoHideHelper.cs" />
<Compile Include="Helpers\HookHelper.cs" />
<Compile Include="Helpers\NativeMethods.cs" />
<Compile Include="Helpers\UISettingsHelper.cs" />
<Compile Include="Helpers\WindowVisible.cs" />
Expand Down Expand Up @@ -112,10 +116,33 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Content Include="EasyHook32.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="EasyHook32Svc.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="EasyHook64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="EasyHook64Svc.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="EasyLoad32.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="EasyLoad64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Resources\Logo-Black.ico" />
<Content Include="Resources\Logo-White.ico" />
<None Include="Resources\logo_blue_128.ico" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<ProjectReference Include="..\SmartTaskbar.Win10.Hook\SmartTaskbar.Win10.Hook.csproj">
<Project>{537c876f-dd20-4a8a-83fa-d87f2f6f20cb}</Project>
<Name>SmartTaskbar.Win10.Hook</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
3 changes: 3 additions & 0 deletions Sources/SmartTaskbar.Win10/Views/SystemTray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private void NotifyIconOnMouseDoubleClick(object s, MouseEventArgs e)
UserSettings.AutoModeType = AutoModeType.None;
Fun.ChangeAutoHide();
HideBar();
HookHelper.ReleaseHook();
}

private static void HideBar()
Expand Down Expand Up @@ -197,6 +198,7 @@ private void ExitOnClick(object s, EventArgs e)
else
HideBar();
_container?.Dispose();
HookHelper.ReleaseHook();
Application.Exit();
}

Expand All @@ -206,6 +208,7 @@ private void AutoModeOnClick(object s, EventArgs e)
{
UserSettings.AutoModeType = AutoModeType.None;
HideBar();
HookHelper.ReleaseHook();
}
else { UserSettings.AutoModeType = AutoModeType.Auto; }
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/SmartTaskbar.Win10/Worker/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ private static void AutoModeWorker()
// Some users will kill the explorer.exe under certain situation.
// In this case, the taskbar cannot be found, just return and wait for the user to reopen the file explorer.
if (taskbar.Handle == IntPtr.Zero)
{
HookHelper.ReleaseHook();
return;
}

HookHelper.SetHook();

switch (taskbar.CheckIfMouseOver())
{
Expand Down
Loading

0 comments on commit 78ab275

Please sign in to comment.