Skip to content

Commit 7ef2f43

Browse files
committed
OrcLib: EmbeddedResource: add wrappers for resource handling api
1 parent c3d89e3 commit 7ef2f43

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

src/OrcLib/EmbeddedResource_Embed.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,51 @@ void CheckYaraRules(const std::filesystem::path& peFile, const std::vector<XmlSt
10461046
}
10471047
}
10481048

1049+
HANDLE TryBeginUpdateResource(
1050+
LPCWSTR pFileName,
1051+
BOOL bDeleteExistingResources,
1052+
uint8_t maxAttempt = kDefaultAttemptLimit,
1053+
uint32_t delayms = kDefaultAttemptDelay)
1054+
{
1055+
for (size_t i = 1; i <= maxAttempt; ++i)
1056+
{
1057+
HANDLE hResource = BeginUpdateResourceW(pFileName, bDeleteExistingResources);
1058+
if (hResource != NULL)
1059+
{
1060+
return hResource;
1061+
}
1062+
1063+
Log::Debug(L"Failed BeginUpdateResourceW (path: {}, attempt: #{}) [{}]", pFileName, i, LastWin32Error());
1064+
Sleep(delayms);
1065+
}
1066+
1067+
return NULL;
1068+
}
1069+
1070+
BOOL TryUpdateResource(
1071+
HANDLE hUpdate,
1072+
LPCWSTR lpType,
1073+
LPCWSTR lpName,
1074+
WORD wLanguage,
1075+
LPVOID lpData,
1076+
DWORD cb,
1077+
uint8_t maxAttempt = kDefaultAttemptLimit,
1078+
uint32_t delayms = kDefaultAttemptDelay)
1079+
{
1080+
for (size_t i = 1; i <= maxAttempt; ++i)
1081+
{
1082+
if (UpdateResourceW(hUpdate, lpType, lpName, wLanguage, lpData, cb))
1083+
{
1084+
return TRUE;
1085+
}
1086+
1087+
Log::Debug(L"Failed EndUpdateResource (handle: {}, attempt: {}) [{}]", hUpdate, i, LastWin32Error());
1088+
Sleep(delayms);
1089+
}
1090+
1091+
return FALSE;
1092+
}
1093+
10491094
// There is sometimes a race condition with Windows after modifying the module. I guess it is only EndUpdateResource
10501095
// which has some issue but I wrapped other api aswell.
10511096
BOOL TryEndUpdateResource(
@@ -1061,7 +1106,7 @@ BOOL TryEndUpdateResource(
10611106
return TRUE;
10621107
}
10631108

1064-
Log::Debug(L"Failed EndUpdateResource (handle: {:#x}, attempt: {}) [{}]", hUpdate, i, LastWin32Error());
1109+
Log::Debug(L"Failed EndUpdateResource (handle: {}, attempt: {}) [{}]", hUpdate, i, LastWin32Error());
10651110
Sleep(delayms);
10661111
}
10671112

0 commit comments

Comments
 (0)