static HRESULT CreateTempEvent(const wchar_t *name, NSynchronization::CManualResetEvent &event, UString &eventName) { CRandom random; random.Init(GetTickCount()); for (;;) { int number = random.Generate(); wchar_t temp[32]; ConvertUInt64ToString((UInt32)number, temp); eventName = name; eventName += temp; RINOK(event.CreateWithName(false, GetSystemString(eventName))); if (::GetLastError() != ERROR_ALREADY_EXISTS) return S_OK; event.Close(); } }
static HRESULT CreateMap(const UStringVector &names, CFileMapping &fileMapping, NSynchronization::CManualResetEvent &event, UString ¶ms) { size_t totalSize = 1; { FOR_VECTOR (i, names) totalSize += (names[i].Len() + 1); } totalSize *= sizeof(wchar_t); CRandNameGenerator random; UString mappingName; for (;;) { random.GenerateName(mappingName, "7zMap"); WRes res = fileMapping.Create(PAGE_READWRITE, totalSize, GetSystemString(mappingName)); if (fileMapping.IsCreated() && res == 0) break; if (res != ERROR_ALREADY_EXISTS) return res; fileMapping.Close(); } UString eventName; for (;;) { random.GenerateName(eventName, "7zEvent"); WRes res = event.CreateWithName(false, GetSystemString(eventName)); if (event.IsCreated() && res == 0) break; if (res != ERROR_ALREADY_EXISTS) return res; event.Close(); } params += L'#'; params += mappingName; params += L':'; char temp[32]; ConvertUInt64ToString(totalSize, temp); params.AddAscii(temp); params += L':'; params += eventName; LPVOID data = fileMapping.Map(FILE_MAP_WRITE, 0, totalSize); if (!data) return E_FAIL; CFileUnmapper unmapper(data); { wchar_t *cur = (wchar_t *)data; *cur++ = 0; // it means wchar_t strings (UTF-16 in WIN32) FOR_VECTOR (i, names) { const UString &s = names[i]; unsigned len = s.Len() + 1; wmemcpy(cur, (const wchar_t *)s, len); cur += len; } } return S_OK; }