Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
RatinCN committed Dec 14, 2024
1 parent 3f1bef6 commit 9919c58
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 60 deletions.
6 changes: 4 additions & 2 deletions Source/KNSoft.MakeLifeEasier/KNSoft.MakeLifeEasier.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
<ClCompile Include="Process\Sync.c" />
<ClCompile Include="Process\Token.c" />
<ClCompile Include="Resource.Embedded.rc.g.c" />
<ClCompile Include="Shell\Desktop.c" />
<ClCompile Include="Shell\Shell.c" />
<ClCompile Include="String\Convert.cpp" />
<ClCompile Include="String\Encoding.c" />
Expand Down Expand Up @@ -240,6 +241,7 @@
<ClInclude Include="Resource.Embedded.h" />
<ClInclude Include="Resource.Embedded.rc.g.h" />
<ClInclude Include="Resource.h" />
<ClInclude Include="Shell\Desktop.h" />
<ClInclude Include="Shell\Shell.h" />
<ClInclude Include="String\Core.h" />
<ClInclude Include="String\Convert.h" />
Expand Down Expand Up @@ -278,14 +280,14 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\KNSoft.Precomp4C.1.0.15-alpha\build\KNSoft.Precomp4C.targets" Condition="Exists('..\packages\KNSoft.Precomp4C.1.0.15-alpha\build\KNSoft.Precomp4C.targets')" />
<Import Project="..\packages\KNSoft.NDK.1.2.15-beta\build\KNSoft.NDK.targets" Condition="Exists('..\packages\KNSoft.NDK.1.2.15-beta\build\KNSoft.NDK.targets')" />
<Import Project="..\packages\KNSoft.NDK.1.2.17-beta\build\KNSoft.NDK.targets" Condition="Exists('..\packages\KNSoft.NDK.1.2.17-beta\build\KNSoft.NDK.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\KNSoft.Precomp4C.1.0.15-alpha\build\KNSoft.Precomp4C.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.Precomp4C.1.0.15-alpha\build\KNSoft.Precomp4C.props'))" />
<Error Condition="!Exists('..\packages\KNSoft.Precomp4C.1.0.15-alpha\build\KNSoft.Precomp4C.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.Precomp4C.1.0.15-alpha\build\KNSoft.Precomp4C.targets'))" />
<Error Condition="!Exists('..\packages\KNSoft.NDK.1.2.15-beta\build\KNSoft.NDK.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.NDK.1.2.15-beta\build\KNSoft.NDK.targets'))" />
<Error Condition="!Exists('..\packages\KNSoft.NDK.1.2.17-beta\build\KNSoft.NDK.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.NDK.1.2.17-beta\build\KNSoft.NDK.targets'))" />
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@
<ClCompile Include="PE\Symbol.c">
<Filter>PE</Filter>
</ClCompile>
<ClCompile Include="Shell\Desktop.c">
<Filter>Shell</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="MakeLifeEasier.inl" />
Expand Down Expand Up @@ -326,6 +329,9 @@
<ClInclude Include="PE\Symbol.h">
<Filter>PE</Filter>
</ClInclude>
<ClInclude Include="Shell\Desktop.h">
<Filter>Shell</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Resource.Embedded.rc" />
Expand Down
3 changes: 2 additions & 1 deletion Source/KNSoft.MakeLifeEasier/MakeLifeEasier.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define MLE_API DECLSPEC_IMPORT
#endif

typedef _Return_type_success_(return == 0) ULONG W32ERROR;
typedef _Return_type_success_(return == ERROR_SUCCESS) ULONG W32ERROR;

/*
* Core Header
Expand Down Expand Up @@ -60,6 +60,7 @@ typedef _Return_type_success_(return == 0) ULONG W32ERROR;
#include "Process/Environment.h"
#include "Process/Loader.h"
#include "Process/Token.h"
#include "Shell/Desktop.h"
#include "Shell/Shell.h"
#include "String/Convert.h"
#include "String/Encoding.h"
Expand Down
158 changes: 158 additions & 0 deletions Source/KNSoft.MakeLifeEasier/Shell/Desktop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include "../MakeLifeEasier.inl"

/* Reversed from Explorer.exe!CTray::_DoExitExplorer */
W32ERROR
NTAPI
Shell_QuitShellWindow(
_In_ HWND ShellWindow)
{
W32ERROR Ret;
HWND ShellTray;

Ret = PostMessageW(ShellWindow, WM_QUIT, 0, 0) ? ERROR_SUCCESS : NtGetLastError();
ShellTray = Shell_GetTrayWindow();
if (ShellTray != NULL && PostMessageW(ShellTray, WM_QUIT, 0, 0))
{
return ERROR_SUCCESS;
}
if (!Ret)
{
Ret = EndTask(ShellWindow, FALSE, TRUE) ? ERROR_SUCCESS : NtGetLastError();
}
return Ret;
}

W32ERROR
NTAPI
Shell_ExitExplorerEx(
_In_opt_ ULONG Timeout,
_Out_opt_ PULONG OldProcessId)
{
W32ERROR Ret;
HWND ShellWindow;
ULONG ProcessId;
HANDLE ProcessHandle;
NTSTATUS Status;

/* Get shell window and process id */
ShellWindow = GetShellWindow();
if (ShellWindow == NULL)
{
ProcessId = 0;
goto _Done;
}
Ret = UI_GetWindowThreadProcessId(ShellWindow, NULL, &ProcessId);
if (Ret != ERROR_SUCCESS)
{
return Ret;
}

/* Open process handle if need wait it */
if (Timeout == 0)
{
ProcessHandle = NULL;
} else if (!NT_SUCCESS(PS_OpenProcess(&ProcessHandle, SYNCHRONIZE, ProcessId)))
{
goto _Force_Kill;
}

/* Quit shell */
Ret = Shell_QuitShellWindow(ShellWindow);
if (Ret == ERROR_SUCCESS)
{
if (ProcessHandle == NULL)
{
goto _Done;
}
} else
{
if (ProcessHandle != NULL)
{
NtClose(ProcessHandle);
}
goto _Force_Kill;
}

/* Wait shell exit */
Status = PS_WaitForObject(ProcessHandle, Timeout);
NtClose(ProcessHandle);
if (Status == STATUS_WAIT_0)
{
goto _Done;
}

_Force_Kill:
Status = PS_OpenProcess(&ProcessHandle, PROCESS_TERMINATE, ProcessId);
if (!NT_SUCCESS(Status))
{
return RtlNtStatusToDosError(Status);
}
Status = NtTerminateProcess(ProcessHandle, STATUS_SUCCESS);
NtClose(ProcessHandle);
if (!NT_SUCCESS(Status))
{
return RtlNtStatusToDosError(Status);
}

_Done:
if (OldProcessId != NULL)
{
*OldProcessId = ProcessId;
}
return ERROR_SUCCESS;
}

#define SHELL_WAIT_EXPLORER_START_UNIT 300

W32ERROR
NTAPI
Shell_StartExplorerEx(
_In_opt_ ULONG Timeout,
_In_opt_ ULONG OldProcessId)
{
W32ERROR Ret;
HWND ShellWindow;
WCHAR Path[MAX_PATH];
ULONG u;

/* Check if shell window already exists */
ShellWindow = GetShellWindow();
if (ShellWindow != NULL)
{
Ret = UI_GetWindowThreadProcessId(ShellWindow, NULL, &u);
if (Ret != ERROR_SUCCESS)
{
return Ret;
} else
{
return u != OldProcessId ? ERROR_SUCCESS : ERROR_ALREADY_EXISTS;
}
}

/* Run explorer.exe */
u = Str_CopyW(Path, SharedUserData->NtSystemRoot);
if (u + 1 >= ARRAYSIZE(Path))
{
return ERROR_INSUFFICIENT_BUFFER;
}
Path[u++] = L'\\';
Str_CopyExW(Path + u, ARRAYSIZE(Path) - u, L"explorer.exe");
Ret = Shell_Exec(Path, NULL, L"open", SW_SHOWNORMAL, NULL);
if (Timeout == 0 || Ret != ERROR_SUCCESS)
{
return Ret;
}

/* Wait shell windows start */
u = 0;
do
{
PS_DelayExec(SHELL_WAIT_EXPLORER_START_UNIT);
if (GetShellWindow() != NULL && GetTaskmanWindow() != NULL)
{
return ERROR_SUCCESS;
}
u += SHELL_WAIT_EXPLORER_START_UNIT;
} while (u < Timeout);
return ERROR_TIMEOUT;
}
58 changes: 58 additions & 0 deletions Source/KNSoft.MakeLifeEasier/Shell/Desktop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once

#include "../MakeLifeEasier.h"

/*
* Reversed from the latest Win11 Explorer.exe.
*
* v_hwndDesktop = GetShellWindow();
* CTrayStatic c_ctrayStatic;
* c_ctrayStatic + 8 = Shell_TrayWnd = GetAncestor(GetTaskmanWindow(), GA_ROOT);
*/

EXTERN_C_START

FORCEINLINE
W32ERROR
Shell_GetExplorerProcessId(
_Out_ PULONG ProcessId)
{
HWND ShellWindow;

ShellWindow = GetShellWindow();
if (ShellWindow == NULL)
{
return ERROR_NOT_FOUND;
}
return GetWindowThreadProcessId(ShellWindow, ProcessId) != 0 ? ERROR_SUCCESS : NtGetLastError();
}

FORCEINLINE
HWND
Shell_GetTrayWindow(VOID)
{
HWND Window = GetTaskmanWindow();
return Window != NULL ? GetAncestor(Window, GA_ROOT) : NULL;
}

MLE_API
W32ERROR
NTAPI
Shell_QuitShellWindow(
_In_ HWND ShellWindow);

MLE_API
W32ERROR
NTAPI
Shell_ExitExplorerEx(
_In_opt_ ULONG Timeout,
_Out_opt_ PULONG OldProcessId);

MLE_API
W32ERROR
NTAPI
Shell_StartExplorerEx(
_In_opt_ ULONG Timeout,
_In_opt_ ULONG OldProcessId);

EXTERN_C_END
18 changes: 9 additions & 9 deletions Source/KNSoft.MakeLifeEasier/String/Convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ Str_ToIntExA(

template <typename TChar>
static
_Success_(return != FALSE)
LOGICAL
_Success_(return > 0)
ULONG
NTAPI
Str_FromIntEx_Impl(
_In_ INT64 Value,
Expand Down Expand Up @@ -277,12 +277,12 @@ Str_FromIntEx_Impl(
uPowerFlag = 4;
} else
{
return FALSE;
return 0;
}

if (DestCchSize <= 1)
{
return FALSE;
return 0;
}

// Find max divisor
Expand Down Expand Up @@ -329,11 +329,11 @@ Str_FromIntEx_Impl(
};
*psz = (TChar)'\0';

return lRet;
return PtrOffset(StrValue, psz) / sizeof(TChar);
}

_Success_(return != FALSE)
LOGICAL
_Success_(return > 0)
ULONG
NTAPI
Str_FromIntExW(
_In_ INT64 Value,
Expand All @@ -345,8 +345,8 @@ Str_FromIntExW(
return Str_FromIntEx_Impl<WCHAR>(Value, Unsigned, Base, StrValue, DestCchSize);
}

_Success_(return != FALSE)
LOGICAL
_Success_(return > 0)
ULONG
NTAPI
Str_FromIntExA(
_In_ INT64 Value,
Expand Down
8 changes: 4 additions & 4 deletions Source/KNSoft.MakeLifeEasier/String/Convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ Str_ToIntExA(
#pragma region String From Integer

MLE_API
_Success_(return != FALSE)
LOGICAL
_Success_(return > 0)
ULONG
NTAPI
Str_FromIntExW(
_In_ INT64 Value,
Expand All @@ -66,8 +66,8 @@ Str_FromIntExW(
_In_ ULONG DestCchSize);

MLE_API
_Success_(return != FALSE)
LOGICAL
_Success_(return > 0)
ULONG
NTAPI
Str_FromIntExA(
_In_ INT64 Value,
Expand Down
Loading

0 comments on commit 9919c58

Please sign in to comment.