@@ -1133,69 +1133,38 @@ HRESULT EmbeddedResource::_UpdateResource(
11331133
11341134 if (hOutput == INVALID_HANDLE_VALUE)
11351135 {
1136- hOut = BeginUpdateResource (szModule, FALSE );
1136+ hOut = TryBeginUpdateResource (szModule, FALSE );
11371137 if (hOut == NULL )
11381138 {
11391139 hr = HRESULT_FROM_WIN32 (GetLastError ());
1140- Log::Debug (L" Failed to update resource in '{}' (BeginUpdateResource ) [{}]" , szModule, SystemError (hr));
1140+ Log::Debug (L" Failed TryBeginUpdateResource (module: {} ) [{}]" , szModule, SystemError (hr));
11411141 return hr;
11421142 }
11431143 }
11441144
1145- if (!UpdateResource (hOut, szType, szName, MAKELANGID (LANG_NEUTRAL, SUBLANG_NEUTRAL), pData, cbSize))
1145+ if (!TryUpdateResource (hOut, szType, szName, MAKELANGID (LANG_NEUTRAL, SUBLANG_NEUTRAL), pData, cbSize))
11461146 {
11471147 hr = HRESULT_FROM_WIN32 (GetLastError ());
1148- Log::Debug (L" Failed to add resource '{}' (UpdateResource ) [{}]" , szName, SystemError (hr));
1148+ Log::Debug (L" Failed TryUpdateResource (module: {}, name: {} ) [{}]" , szModule , szName, SystemError (hr));
11491149 return hr;
11501150 }
11511151
11521152 if (hOutput == INVALID_HANDLE_VALUE)
11531153 {
1154- if (!EndUpdateResource (hOut, FALSE ))
1154+ if (!TryEndUpdateResource (hOut, FALSE ))
11551155 {
11561156 hr = HRESULT_FROM_WIN32 (GetLastError ());
1157- Log::Debug (L" Failed to update resource in '{}' (EndUpdateResource ) [{}]" , szModule, SystemError (hr));
1157+ Log::Debug (L" Failed TryEndUpdateResource (module: {} ) [{}]" , szModule, SystemError (hr));
11581158 return hr;
11591159 }
11601160 }
11611161
11621162 return S_OK;
11631163}
11641164
1165- // There is sometimes a race condition with Windows after modifying the module.
1166- // This function will retry multiple time to access a file for updating its resource.
1167- HRESULT EmbeddedResource::TryUpdateResource (
1168- HANDLE hOutput,
1169- const WCHAR* szModule,
1170- const WCHAR* szType,
1171- const WCHAR* szName,
1172- LPVOID pData,
1173- DWORD cbSize,
1174- uint8_t maxAttempt)
1175- {
1176-
1177- HRESULT hr = E_FAIL;
1178-
1179- for (size_t i = 1 ; i <= maxAttempt; ++i)
1180- {
1181- hr = EmbeddedResource::_UpdateResource (hOutput, szModule, szType, szName, pData, cbSize);
1182- if (SUCCEEDED (hr))
1183- {
1184- return S_OK;
1185- }
1186-
1187- Log::Debug (L" Failed to update resource '{}' (attempt: {}) [{}]" , szName, i, SystemError (hr));
1188- Sleep (1000 );
1189- }
1190-
1191- Log::Error (L" Failed to update resource '{}' [{}]" , szName, SystemError (hr));
1192- return hr;
1193- }
1194-
11951165HRESULT EmbeddedResource::UpdateResources (const std::wstring& strPEToUpdate, const std::vector<EmbedSpec>& ToEmbed)
11961166{
11971167 HRESULT hr = S_OK;
1198- const auto kMaxAttempt = 8 ;
11991168
12001169 DWORD dwMajor = 0L , dwMinor = 0L ;
12011170 SystemDetails::GetOSVersion (dwMajor, dwMinor);
@@ -1214,12 +1183,11 @@ HRESULT EmbeddedResource::UpdateResources(const std::wstring& strPEToUpdate, con
12141183
12151184 if (!bAtomicUpdate)
12161185 {
1217- hOutput = BeginUpdateResource (strPEToUpdate.c_str (), FALSE );
1186+ hOutput = TryBeginUpdateResource (strPEToUpdate.c_str (), FALSE );
12181187 if (hOutput == NULL )
12191188 {
12201189 hr = HRESULT_FROM_WIN32 (GetLastError ());
1221- Log::Error (
1222- L" Failed to update resources in '{}' (BeginUpdateResource) [{}]" , strPEToUpdate, SystemError (hr));
1190+ Log::Error (L" Failed TryBeginUpdateResource (path: {}) [{}]" , strPEToUpdate, SystemError (hr));
12231191 return hr;
12241192 }
12251193 }
@@ -1232,14 +1200,13 @@ HRESULT EmbeddedResource::UpdateResources(const std::wstring& strPEToUpdate, con
12321200 {
12331201 case EmbeddedResource::EmbedSpec::EmbedType::NameValuePair: {
12341202 if (SUCCEEDED (
1235- hr = TryUpdateResource (
1203+ hr = _UpdateResource (
12361204 hOutput,
12371205 strPEToUpdate.c_str (),
12381206 EmbeddedResource::VALUES (),
12391207 item.Name .c_str (),
12401208 (LPVOID)item.Value .c_str (),
1241- (DWORD)item.Value .size () * sizeof (WCHAR),
1242- kMaxAttempt )))
1209+ (DWORD)item.Value .size () * sizeof (WCHAR))))
12431210 {
12441211 Log::Info (L" Successfully added resource link {} -> {}" , item.Name , item.Value );
12451212 }
@@ -1252,14 +1219,8 @@ HRESULT EmbeddedResource::UpdateResources(const std::wstring& strPEToUpdate, con
12521219 }
12531220 case EmbeddedResource::EmbedSpec::EmbedType::ValuesDeletion: {
12541221 if (SUCCEEDED (
1255- hr = TryUpdateResource (
1256- hOutput,
1257- strPEToUpdate.c_str (),
1258- EmbeddedResource::VALUES (),
1259- item.Name .c_str (),
1260- NULL ,
1261- 0L ,
1262- kMaxAttempt )))
1222+ hr = _UpdateResource (
1223+ hOutput, strPEToUpdate.c_str (), EmbeddedResource::VALUES (), item.Name .c_str (), NULL , 0L )))
12631224 {
12641225 Log::Info (L" Successfully delete resource '{}'" , item.Name );
12651226 }
@@ -1272,14 +1233,8 @@ HRESULT EmbeddedResource::UpdateResources(const std::wstring& strPEToUpdate, con
12721233 }
12731234 case EmbeddedResource::EmbedSpec::EmbedType::BinaryDeletion: {
12741235 if (SUCCEEDED (
1275- hr = TryUpdateResource (
1276- hOutput,
1277- strPEToUpdate.c_str (),
1278- EmbeddedResource::BINARY (),
1279- item.Name .c_str (),
1280- NULL ,
1281- 0L ,
1282- kMaxAttempt )))
1236+ hr = _UpdateResource (
1237+ hOutput, strPEToUpdate.c_str (), EmbeddedResource::BINARY (), item.Name .c_str (), NULL , 0L )))
12831238 {
12841239 Log::Info (L" Successfully delete resource '{}'" , item.Name );
12851240 }
@@ -1301,15 +1256,6 @@ HRESULT EmbeddedResource::UpdateResources(const std::wstring& strPEToUpdate, con
13011256 return hr;
13021257 }
13031258
1304- if (!bAtomicUpdate)
1305- {
1306- if (!TryEndUpdateResource (hOutput, FALSE ))
1307- {
1308- Log::Error (L" Failed to update resources in '{}'" , strPEToUpdate);
1309- return E_FAIL;
1310- }
1311- }
1312-
13131259 for (auto iter = ToEmbed.begin (); iter != ToEmbed.end (); ++iter)
13141260 {
13151261 const EmbeddedResource::EmbedSpec& item = *iter;
@@ -1351,14 +1297,13 @@ HRESULT EmbeddedResource::UpdateResources(const std::wstring& strPEToUpdate, con
13511297 memstream->GrabBuffer (data);
13521298
13531299 if (SUCCEEDED (
1354- hr = TryUpdateResource (
1355- INVALID_HANDLE_VALUE ,
1300+ hr = _UpdateResource (
1301+ hOutput ,
13561302 strPEToUpdate.c_str (),
13571303 EmbeddedResource::BINARY (),
13581304 item.Name .c_str (),
13591305 data.GetData (),
1360- (DWORD)data.GetCount (),
1361- kMaxAttempt )))
1306+ (DWORD)data.GetCount ())))
13621307 {
13631308 Log::Info (L" Successfully added '{}' as resource '{}'" , item.Value , item.Name );
13641309 resourceRegistry.MarkAsEmbedded (item.Name , L" res" );
@@ -1435,14 +1380,13 @@ HRESULT EmbeddedResource::UpdateResources(const std::wstring& strPEToUpdate, con
14351380 memstream->GrabBuffer (data);
14361381
14371382 if (SUCCEEDED (
1438- hr = TryUpdateResource (
1439- INVALID_HANDLE_VALUE ,
1383+ hr = _UpdateResource (
1384+ hOutput ,
14401385 strPEToUpdate.c_str (),
14411386 EmbeddedResource::BINARY (),
14421387 item.Name .c_str (),
14431388 data.GetData (),
1444- (DWORD)data.GetCount (),
1445- kMaxAttempt )))
1389+ (DWORD)data.GetCount ())))
14461390 {
14471391 Log::Info (L" Successfully added archive '{}'" , item.Name );
14481392 }
@@ -1458,6 +1402,15 @@ HRESULT EmbeddedResource::UpdateResources(const std::wstring& strPEToUpdate, con
14581402 }
14591403 }
14601404
1405+ if (!bAtomicUpdate)
1406+ {
1407+ if (!TryEndUpdateResource (hOutput, FALSE ))
1408+ {
1409+ Log::Error (L" Failed to update resources in '{}'" , strPEToUpdate);
1410+ return E_FAIL;
1411+ }
1412+ }
1413+
14611414 PrintMissingResources (resourceRegistry);
14621415
14631416 Sleep (3000 ); // Wait for the UpdateResource delay
0 commit comments