Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
RatinCN committed Nov 6, 2024
1 parent bf1be68 commit 40869c8
Show file tree
Hide file tree
Showing 19 changed files with 309 additions and 91 deletions.
84 changes: 84 additions & 0 deletions Source/KNSoft.MakeLifeEasier/Error/Code.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "../MakeLifeEasier.inl"

NTSTATUS
NTAPI
Err_HrToNtStatus(
_In_ HRESULT Hr)
{
if (Hr == S_OK)
{
return STATUS_SUCCESS;
} else if (Hr == E_INVALIDARG)
{
return STATUS_INVALID_PARAMETER;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_INTERNAL_ERROR))
{
return STATUS_INTERNAL_ERROR;
} else if (Hr == E_OUTOFMEMORY)
{
return STATUS_NO_MEMORY;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW))
{
return STATUS_INTEGER_OVERFLOW;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND))
{
return STATUS_OBJECT_PATH_NOT_FOUND;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
return STATUS_OBJECT_NAME_NOT_FOUND;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION))
{
return STATUS_NOT_IMPLEMENTED;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_MORE_DATA))
{
return STATUS_BUFFER_OVERFLOW;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_IMPLEMENTATION_LIMIT))
{
return STATUS_IMPLEMENTATION_LIMIT;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_NO_MORE_MATCHES))
{
return STATUS_NO_MORE_MATCHES;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_ILLEGAL_CHARACTER))
{
return STATUS_ILLEGAL_CHARACTER;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_UNDEFINED_CHARACTER))
{
return STATUS_UNDEFINED_CHARACTER;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
{
return STATUS_BUFFER_TOO_SMALL;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_DISK_FULL))
{
return STATUS_DISK_FULL;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME))
{
return STATUS_OBJECT_NAME_INVALID;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND))
{
return STATUS_DLL_NOT_FOUND;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_OLD_WIN_VERSION))
{
return STATUS_REVISION_MISMATCH;
} else if (Hr == E_FAIL)
{
return STATUS_UNSUCCESSFUL;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_XML_PARSE_ERROR))
{
return STATUS_XML_PARSE_ERROR;
} else if (Hr == HRESULT_FROM_WIN32(ERROR_UNHANDLED_EXCEPTION))
{
return STATUS_NONCONTINUABLE_EXCEPTION;
}

if (Hr & FACILITY_NT_BIT)
{
return Hr & ~FACILITY_NT_BIT;
} else if (HRESULT_FACILITY(Hr) == FACILITY_WIN32)
{
return NTSTATUS_FROM_WIN32(HRESULT_CODE(Hr));
} else if (HRESULT_FACILITY(Hr) == FACILITY_SSPI)
{
return Hr <= 0 ? (NTSTATUS)Hr : MAKE_NTSTATUS(STATUS_SEVERITY_ERROR, FACILITY_SSPI, HRESULT_CODE(Hr));
}
return STATUS_INTERNAL_ERROR;
}
46 changes: 46 additions & 0 deletions Source/KNSoft.MakeLifeEasier/Error/Code.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include "../MakeLifeEasier.h"

EXTERN_C_START

/* See also RtlNtStatusToDosErrorNoTeb */
FORCEINLINE
ULONG
Err_NtStatusToWin32Error(
_In_ NTSTATUS Status)
{
return NT_FACILITY(Status) == FACILITY_NTWIN32 ? NT_CODE(Status) : RtlNtStatusToDosErrorNoTeb(Status);
}

/* See also HRESULT_FROM_NT and wil::NtStatusToHr */
FORCEINLINE
HRESULT
Err_NtStatusToHr(
_In_ NTSTATUS Status)
{
ULONG Win32Error;

if (NT_SUCCESS(Status))
{
return S_OK;
} else if (Status == STATUS_NO_MEMORY)
{
return E_OUTOFMEMORY;
}
Win32Error = Err_NtStatusToWin32Error(Status);
if (Win32Error != 0 && Win32Error != ERROR_MR_MID_NOT_FOUND)
{
return HRESULT_FROM_WIN32(Win32Error);
}
return HRESULT_FROM_NT(Status);
}

/* See also wil::HrToNtStatus */
MLE_API
NTSTATUS
NTAPI
Err_HrToNtStatus(
_In_ HRESULT Hr);

EXTERN_C_END
30 changes: 11 additions & 19 deletions Source/KNSoft.MakeLifeEasier/Error/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@

#include "../MakeLifeEasier.h"

#include "../PE/PE.h"
#include "../PE/Resource.h"
#include "../System/Library.h"
#include "../UI/Dialog/Dialog.h"

EXTERN_C_START

FORCEINLINE
ULONG
Err_NtStatusToWin32Error(
_In_ NTSTATUS Status)
{
return NT_FACILITY(Status) == FACILITY_NTWIN32 ? NT_CODE(Status) : RtlNtStatusToDosErrorNoTeb(Status);
}

#pragma region Error Message

FORCEINLINE
Expand Down Expand Up @@ -48,20 +40,20 @@ Err_GetNtStatusInfo(
FORCEINLINE
_Success_(return != NULL)
PCWSTR
Err_GetHResultInfo(
_In_ HRESULT HResult)
Err_GetHrInfo(
_In_ HRESULT Hr)
{
ULONG Facility, Code;

Facility = HRESULT_FACILITY(HResult);
Code = HRESULT_CODE(HResult);
Facility = HRESULT_FACILITY(Hr);
Code = HRESULT_CODE(Hr);

if (Facility == FACILITY_WIN32)
{
return Err_GetWin32ErrorInfo(Code);
} else if ((ULONG)HResult & FACILITY_NT_BIT)
} else if ((ULONG)Hr & FACILITY_NT_BIT)
{
return Err_GetNtStatusInfo(HResult & ~FACILITY_NT_BIT);
return Err_GetNtStatusInfo(Hr & ~FACILITY_NT_BIT);
}

return NULL;
Expand Down Expand Up @@ -108,15 +100,15 @@ Error_NtStatusMessageBox(

FORCEINLINE
VOID
Error_HResultMessageBox(
Error_HrMessageBox(
_In_opt_ HWND Owner,
_In_opt_ PCWSTR Title,
_In_ HRESULT HResult)
_In_ HRESULT Hr)
{
UI_MsgBox(Owner,
Err_GetHResultInfo(HResult),
Err_GetHrInfo(Hr),
Title,
(HRESULT_SEVERITY(HResult) == SEVERITY_ERROR ? MB_ICONERROR : MB_ICONINFORMATION) | MB_OK);
(HRESULT_SEVERITY(Hr) == SEVERITY_ERROR ? MB_ICONERROR : MB_ICONINFORMATION) | MB_OK);
}

#pragma endregion
Expand Down
16 changes: 10 additions & 6 deletions Source/KNSoft.MakeLifeEasier/KNSoft.MakeLifeEasier.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Crypt\Cert.c" />
<ClCompile Include="Error\Code.c" />
<ClCompile Include="Error\Message.c" />
<ClCompile Include="I18N.xml.g.c" />
<ClCompile Include="IO\Hardware.c" />
Expand All @@ -169,15 +170,16 @@
<ClCompile Include="Memory\Memory.c" />
<ClCompile Include="NT\Object.c" />
<ClCompile Include="NT\Security.c" />
<ClCompile Include="PE\PE.c" />
<ClCompile Include="PE\Resolve.c" />
<ClCompile Include="PE\Resource.c" />
<ClCompile Include="Process\Loader.c" />
<ClCompile Include="Process\Sync.c" />
<ClCompile Include="Process\Token.c" />
<ClCompile Include="Resource.Embedded.rc.g.c" />
<ClCompile Include="Shell\Shell.c" />
<ClCompile Include="String\Convert.cpp" />
<ClCompile Include="String\Hash.cpp" />
<ClCompile Include="System\Config.c" />
<ClCompile Include="System\Info.c" />
<ClCompile Include="System\Library.c" />
<ClCompile Include="System\Registry.cpp" />
<ClCompile Include="Time\Time.c" />
Expand All @@ -196,6 +198,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="Crypt\Cert.h" />
<ClInclude Include="Error\Code.h" />
<ClInclude Include="Error\Message.h" />
<ClInclude Include="I18N.xml.g.h" />
<ClInclude Include="IO\Hardware.h" />
Expand All @@ -207,7 +210,8 @@
<ClInclude Include="Memory\Memory.h" />
<ClInclude Include="NT\Object.h" />
<ClInclude Include="NT\Security.h" />
<ClInclude Include="PE\PE.h" />
<ClInclude Include="PE\Resolve.h" />
<ClInclude Include="PE\Resource.h" />
<ClInclude Include="Process\Loader.h" />
<ClInclude Include="Process\Sync.h" />
<ClInclude Include="Process\Token.h" />
Expand All @@ -218,7 +222,7 @@
<ClInclude Include="String\Core.h" />
<ClInclude Include="String\Convert.h" />
<ClInclude Include="String\Hash.h" />
<ClInclude Include="System\Config.h" />
<ClInclude Include="System\Info.h" />
<ClInclude Include="System\Library.h" />
<ClInclude Include="System\Registry.h" />
<ClInclude Include="Time\Time.h" />
Expand Down Expand Up @@ -246,15 +250,15 @@
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\KNSoft.NDK.1.2.4-beta\build\KNSoft.NDK.targets" Condition="Exists('..\packages\KNSoft.NDK.1.2.4-beta\build\KNSoft.NDK.targets')" />
<Import Project="..\packages\KNSoft.Precomp4C.1.0.13-alpha\build\KNSoft.Precomp4C.targets" Condition="Exists('..\packages\KNSoft.Precomp4C.1.0.13-alpha\build\KNSoft.Precomp4C.targets')" />
<Import Project="..\packages\KNSoft.NDK.1.2.7-beta\build\KNSoft.NDK.targets" Condition="Exists('..\packages\KNSoft.NDK.1.2.7-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.NDK.1.2.4-beta\build\KNSoft.NDK.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.NDK.1.2.4-beta\build\KNSoft.NDK.targets'))" />
<Error Condition="!Exists('..\packages\KNSoft.Precomp4C.1.0.13-alpha\build\KNSoft.Precomp4C.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.Precomp4C.1.0.13-alpha\build\KNSoft.Precomp4C.props'))" />
<Error Condition="!Exists('..\packages\KNSoft.Precomp4C.1.0.13-alpha\build\KNSoft.Precomp4C.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.Precomp4C.1.0.13-alpha\build\KNSoft.Precomp4C.targets'))" />
<Error Condition="!Exists('..\packages\KNSoft.NDK.1.2.7-beta\build\KNSoft.NDK.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\KNSoft.NDK.1.2.7-beta\build\KNSoft.NDK.targets'))" />
</Target>
</Project>
20 changes: 16 additions & 4 deletions Source/KNSoft.MakeLifeEasier/KNSoft.MakeLifeEasier.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<ClCompile Include="System\Library.c">
<Filter>System</Filter>
</ClCompile>
<ClCompile Include="PE\PE.c">
<ClCompile Include="PE\Resolve.c">
<Filter>PE</Filter>
</ClCompile>
<ClCompile Include="Main.c" />
Expand All @@ -92,7 +92,7 @@
<ClCompile Include="UI\GDI.c">
<Filter>UI</Filter>
</ClCompile>
<ClCompile Include="System\Config.c">
<ClCompile Include="System\Info.c">
<Filter>System</Filter>
</ClCompile>
<ClCompile Include="UI\DPI.c">
Expand Down Expand Up @@ -129,6 +129,12 @@
<ClCompile Include="NT\Security.c">
<Filter>NT</Filter>
</ClCompile>
<ClCompile Include="Error\Code.c">
<Filter>Error</Filter>
</ClCompile>
<ClCompile Include="PE\Resource.c">
<Filter>PE</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="MakeLifeEasier.inl" />
Expand Down Expand Up @@ -178,7 +184,7 @@
<ClInclude Include="System\Library.h">
<Filter>System</Filter>
</ClInclude>
<ClInclude Include="PE\PE.h">
<ClInclude Include="PE\Resolve.h">
<Filter>PE</Filter>
</ClInclude>
<ClInclude Include="Resource.Embedded.h" />
Expand All @@ -189,7 +195,7 @@
<ClInclude Include="UI\GDI.h">
<Filter>UI</Filter>
</ClInclude>
<ClInclude Include="System\Config.h">
<ClInclude Include="System\Info.h">
<Filter>System</Filter>
</ClInclude>
<ClInclude Include="UI\DPI.h">
Expand Down Expand Up @@ -227,6 +233,12 @@
<ClInclude Include="Memory\Core.h">
<Filter>Memory</Filter>
</ClInclude>
<ClInclude Include="Error\Code.h">
<Filter>Error</Filter>
</ClInclude>
<ClInclude Include="PE\Resource.h">
<Filter>PE</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Resource.Embedded.rc" />
Expand Down
10 changes: 6 additions & 4 deletions Source/KNSoft.MakeLifeEasier/MakeLifeEasier.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
typedef _Return_type_success_(return == 0) ULONG W32ERROR;

/*
* L0 Header
* Core Header
* - Provides core functions
* - Depends on NDK only
* - Header only
Expand All @@ -31,27 +31,29 @@ typedef _Return_type_success_(return == 0) ULONG W32ERROR;
* - Provides basic and low-level utilities
* - Depends on NDK and L0 header only
*/
#include "Error/Code.h"
#include "Math/Math.h"
#include "Math/Rand.h"
#include "Memory/Memory.h"
#include "NT/Object.h"
#include "PE/PE.h"
#include "Process/Sync.h"
#include "Time/Time.h"
#include "String/Convert.h"
#include "String/Hash.h"
#include "System/Library.h"
#include "System/Registry.h"
#include "UI/GDI.h"

/* L2 Header: No dependencies yet */
#include "Crypt/Cert.h"
#include "IO/Hardware.h"
#include "NT/Security.h"
#include "PE/Resolve.h"
#include "PE/Resource.h"
#include "Process/Loader.h"
#include "Process/Token.h"
#include "Shell/Shell.h"
#include "System/Config.h"
#include "System/Registry.h"
#include "System/Info.h"
#include "UI/Control.h"
#include "UI/Dialog/Dialog.h"
#include "UI/DPI.h"
Expand Down
3 changes: 2 additions & 1 deletion Source/KNSoft.MakeLifeEasier/Memory/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include <KNSoft/NDK/NDK.h>

/*
* If failed, the caller could use STATUS_NO_MEMORY, ERROR_NOT_ENOUGH_MEMORY, ... as error code
* If failed, the caller could use
* STATUS_NO_MEMORY, ERROR_OUTOFMEMORY, ERROR_NOT_ENOUGH_MEMORY, E_OUTOFMEMORY, ... as error code
*/
FORCEINLINE
_Success_(return != NULL)
Expand Down
4 changes: 2 additions & 2 deletions Source/KNSoft.MakeLifeEasier/Memory/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ EXTERN_C_START

#pragma region Heap

#if !defined(__cplusplus) && _MSC_FULL_VER >= 193933428
#define Mem_AllocPtr(p) ((p = (__typeof__(p))Mem_Alloc(sizeof(*(p)))) != NULL)
#if defined(TYPE_OF)
#define Mem_AllocPtr(p) ((LOGICAL)((p = (TYPE_OF(p))Mem_Alloc(sizeof(*(p)))) != NULL))
#endif

#pragma endregion
Expand Down
Loading

0 comments on commit 40869c8

Please sign in to comment.