ByteArray &ByteArray::loadFromResource(int resId, const TCHAR *typeName) {
  DEFINEMETHODNAME;
  HRSRC hSource = FindResource(NULL, MAKEINTRESOURCE(resId), typeName);

  if(hSource == NULL) {
    throwException(_T("%s(%d,%s) failed:%s"), method, resId, typeNameToString(typeName).cstr(), getLastErrorText().cstr());
  }

  HGLOBAL hGlobal = LoadResource(NULL, hSource);
  if(hGlobal == NULL) {
    throwException(_T("%s(%d,%s) failed:%s"), method, resId, typeNameToString(typeName).cstr(), getLastErrorText().cstr());
  }

  try {
    void *r = LockResource(hGlobal);
    if(r == NULL) {
      throwException(_T("%s(%d,%s):LockResource failed"), method, resId, typeNameToString(typeName).cstr());
    }

    const int size = (UINT)SizeofResource(NULL, hSource);

    clear(size);
    append((BYTE*)r, size);

    UnlockResource(hGlobal); // 16Bit Windows Needs This
    FreeResource(hGlobal);   // 16Bit Windows Needs This (32Bit - Automatic Release)
    hGlobal = NULL;
    return *this;
  } catch(...) {
    UnlockResource(hGlobal); // 16Bit Windows Needs This
    FreeResource(hGlobal);   // 16Bit Windows Needs This (32Bit - Automatic Release)
    throw;
  }
}
Exemple #2
0
	json Serializer::serializeFileType(const string& aPath) noexcept {
		auto ext = Format::formatFileType(aPath);
		auto typeName = SearchManager::getInstance()->getNameByExtension(ext, true);

		return{
			{ "id", "file" },
			{ "content_type", typeNameToString(typeName) },
			{ "str", ext }
		};
	}