Example #1
0
IconRsrc load_icon_rsrc(HMODULE h_module, LPCTSTR name, WORD lang_id) {
  IconRsrc icon_rsrc;
  icon_rsrc.id = name;
  icon_rsrc.lang_id = lang_id;
  HRSRC h_rsrc = FindResourceEx(h_module, RT_GROUP_ICON, name, lang_id);
  CHECK_SYS(h_rsrc);
  HGLOBAL h_global = LoadResource(h_module, h_rsrc);
  CHECK_SYS(h_global);
  unsigned char* res_data = static_cast<unsigned char*>(LockResource(h_global));
  CHECK_SYS(res_data);
  const IconGroupHeader* header = reinterpret_cast<const IconGroupHeader*>(res_data);
  for (unsigned i = 0; i < header->count; i++) {
    IconImageRsrc image_rsrc;
    const IconGroupEntry* entry = reinterpret_cast<const IconGroupEntry*>(res_data + sizeof(IconGroupHeader) + i * sizeof(IconGroupEntry));
    image_rsrc.id = entry->id;
    image_rsrc.lang_id = lang_id;
    image_rsrc.image.width = entry->width;
    image_rsrc.image.height = entry->height;
    image_rsrc.image.color_cnt = entry->color_cnt;
    image_rsrc.image.plane_cnt = entry->plane_cnt;
    image_rsrc.image.bit_cnt = entry->bit_cnt;
    HRSRC h_rsrc = FindResourceEx(h_module, RT_ICON, MAKEINTRESOURCE(entry->id), lang_id);
    CHECK_SYS(h_rsrc);
    HGLOBAL h_global = LoadResource(h_module, h_rsrc);
    CHECK_SYS(h_global);
    unsigned char* icon_data = static_cast<unsigned char*>(LockResource(h_global));
    CHECK_SYS(icon_data);
    image_rsrc.image.bitmap.assign(icon_data, icon_data + entry->size);
    icon_rsrc.images.push_back(image_rsrc);
  }
  return icon_rsrc;
}
Example #2
0
//---------------------------------------------------------------------------
static bool __fastcall GetResource(
  const UnicodeString ResName, void *& Content, unsigned long & Size)
{
  HRSRC Resource = FindResourceEx(HInstance, RT_RCDATA, ResName.c_str(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
  bool Result = (Resource != NULL);
  if (Result)
  {
    Size = SizeofResource(HInstance, Resource);
    if (!Size)
    {
      throw Exception(FORMAT(L"Cannot get size of resource %s", (ResName)));
    }

    Content = LoadResource(HInstance, Resource);
    if (!Content)
    {
      throw Exception(FORMAT(L"Cannot read resource %s", (ResName)));
    }

    Content = LockResource(Content);
    if (!Content)
    {
      throw Exception(FORMAT(L"Cannot lock resource %s", (ResName)));
    }
  }

  return Result;
}
Example #3
0
BOOL loadString(const int resID, char* buffer) {
	HRSRC hResource;
	HGLOBAL hResourceLoaded;
	LPBYTE lpBuffer;

	hResource = FindResourceEx(hModule, RT_RCDATA, MAKEINTRESOURCE(resID),
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT));
	if (NULL != hResource) {
		hResourceLoaded = LoadResource(hModule, hResource);
		if (NULL != hResourceLoaded) {
			lpBuffer = (LPBYTE) LockResource(hResourceLoaded);
			if (NULL != lpBuffer) {
				int x = 0;
				do {
					buffer[x] = (char) lpBuffer[x];
				} while (buffer[x++] != 0);
				// debug("Resource %d:\t%s\n", resID, buffer);
				return TRUE;
			}
		}
	} else {
		SetLastError(0);
	}
	return FALSE;
}
Example #4
0
/** Load RCDATA with id <b>lpName</b> and the <b>wLanguage</b> locale
 *  from the binary file specified by <b>hInstance</b>.
 *  If the resource doesn't exist, return 0 and don't touch <b>lpBuffer</b>.
 *
 *  If <b>lpBuffer</b> is NULL then return the size of the resource,
 *
 *  If <b>lpBuffer</b> is provided then write the resource data as is to
 *  <b>lpBuffer</b> and return the length of the size written data.
 *  If <b>nBufferMax</b> is less than the actual size of the resource,
 *  then fill the buffer till the end.
 *
 *  \code
 *  LPTSTR lpName = "resource-name";
 *  WORD langID = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
 *  int bufferLen = LoadResDataEx(NULL, lpName, NULL, 0, langID);
 *  LPTSTR buffer = (LPTSTR)malloc(bufferLen);
 *  int size = LoadResDataEx(NULL, lpName, buffer, bufferLen, langID);
 *  if (size > 0) {
 *    _tprintf(_T("Resource size: %d\n"), size);
 *  } else {
 *    _tprintf(_T("Resource '%d' does not exist.\n"), lpName);
 *  }
 *  free(buffer);
 *  \endcode
 */
int
LoadResDataEx(HINSTANCE hInstance,
              LPCTSTR lpName,
              LPVOID lpBuffer,
              int nBufferMax,
              WORD wLanguage)
{
  HRSRC hRes = FindResourceEx(hInstance, RT_RCDATA,  lpName, wLanguage);
  if (hRes == NULL) return 0;

  DWORD resSize = SizeofResource(hInstance, hRes);
  if (resSize == 0) return 0;

  /* If lpBuffer is NULL, then this is a request for
   * the required buffer length */
  if (lpBuffer == NULL) return resSize;

  HGLOBAL hGlobal = LoadResource(hInstance, hRes);
  if (hGlobal == NULL) return 0;

  LPVOID lpResData = (LPVOID)LockResource(hGlobal);
  if (lpResData == NULL) return 0;

  /* Limit the buffer length to the provided one. */
  int bufferLen = min((int)resSize, nBufferMax);
  /* Copy the data to the buffer as is. We won't interpret the data format */
  memcpy(lpBuffer, lpResData, bufferLen);

  return bufferLen;
}
Example #5
0
static HRSRC
FindResourceLang(PTSTR resType, PTSTR resId, LANGID langId)
{
    HRSRC res;

    /* try to find the resource in requested language */
    res = FindResourceEx(o.hInstance, resType, resId, langId);
    if (res)
        return res;

    /* try to find the resource in the default language */
    res = FindResourceEx(o.hInstance, resType, resId, fallbackLangId);
    if (res)
        return res;

    /* try to find the resource in any language */
    return FindResource(o.hInstance, resId, resType);
}
Example #6
0
BOOL CResModule::ReplaceDialog(UINT nID, WORD wLanguage)
{
	const WORD*	lpDlg;
	HRSRC		hrsrc;
	HGLOBAL		hGlblDlgTemplate;

	hrsrc = FindResourceEx(m_hResDll, RT_DIALOG, MAKEINTRESOURCE(nID), wLanguage);

	if (hrsrc == NULL)
		MYERROR;

	hGlblDlgTemplate = LoadResource(m_hResDll, hrsrc);

	if (hGlblDlgTemplate == NULL)
		MYERROR;

	lpDlg = (WORD *) LockResource(hGlblDlgTemplate);

	if (lpDlg == NULL)
		MYERROR;

	size_t nMem = 0;
	const WORD * p = lpDlg;
	if (!CountMemReplaceDialogResource(p, &nMem, NULL))
		goto DONE_ERROR;
	WORD * newDialog = new WORD[nMem + (nMem % 2)];
	SecureZeroMemory(newDialog, (nMem + (nMem % 2))*2);

	size_t index = 0;
	if (!CountMemReplaceDialogResource(lpDlg, &index, newDialog))
	{
		delete [] newDialog;
		goto DONE_ERROR;
	}
	
	if (!UpdateResource(m_hUpdateRes, RT_DIALOG, MAKEINTRESOURCE(nID), (m_wTargetLang ? m_wTargetLang : wLanguage), newDialog, (DWORD)(nMem + (nMem % 2))*2))
	{
		delete [] newDialog;
		goto DONE_ERROR;
	}
	
	if ((m_wTargetLang)&&(!UpdateResource(m_hUpdateRes, RT_DIALOG, MAKEINTRESOURCE(nID), wLanguage, NULL, 0)))
	{
		delete [] newDialog;
		goto DONE_ERROR;
	}

	delete [] newDialog;
	UnlockResource(hGlblDlgTemplate);
	FreeResource(hGlblDlgTemplate);
	return TRUE;

DONE_ERROR:
	UnlockResource(hGlblDlgTemplate);
	FreeResource(hGlblDlgTemplate);
	MYERROR;
}
Example #7
0
BOOL CVersionInfo::LoadVersionInfoResource(const CString& strModulePath, CVersionInfoBuffer &viBuf, LPCTSTR lpszResourceId, WORD wLangId)
{
	HRSRC hResInfo; 

	HMODULE hModule = LoadLibraryEx(strModulePath, NULL, DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE);
	if (NULL == hModule)
		return FALSE;

	if ((NULL == lpszResourceId) && (wLangId == 0xFFFF))
	{
		//Load first RT_VERSION resource that will be found
		
		m_lpszResourceId = NULL;

		EnumResourceNames(hModule, RT_VERSION, (ENUMRESNAMEPROC)EnumResourceNamesFuncFindFirst, (LONG_PTR)this);
		
		if (NULL == m_lpszResourceId)
		{
			FreeLibrary(hModule);
			return FALSE;
		}
		
		// Now the m_lpszResourceId must be the name of the resource
		m_wLangId = 0xFFFF;
		EnumResourceLanguages(hModule, RT_VERSION, m_lpszResourceId, (ENUMRESLANGPROC)EnumResourceLangFuncFindFirst, (LONG_PTR)this);

		// Found resource, copy the ID's to local vars
		lpszResourceId = m_lpszResourceId;
		wLangId = m_wLangId;
	}

	hResInfo = FindResourceEx(hModule, RT_VERSION, lpszResourceId, wLangId); 
	// Write the resource language to the resource information file. 

	DWORD dwSize = SizeofResource(hModule, hResInfo);
	if (dwSize)
	{
		HGLOBAL hgRes = LoadResource(hModule, hResInfo);
		if (hgRes)
		{
			LPVOID lpMemory = LockResource(hgRes);
			if (lpMemory)
			{
				viBuf.Write(lpMemory,dwSize);
				
				UnlockResource(hgRes);
				FreeLibrary(hModule);
				return TRUE;
			}
		}
	}

	FreeLibrary(hModule);
	return FALSE;

}
Example #8
0
//    FUNCTION: EnumLangsFunc(HANDLE, LPSTR, LPSTR, WORD, LONG)
//
//    PURPOSE:  Resource language callback
BOOL EnumLangsFunc(
		HMODULE hModule, // module handle
		LPCTSTR lpType,  // address of resource type
		LPCTSTR lpName,  // address of resource name
		WORD wLang,      // resource language
		LONG lParam)     // extra parameter, could be
// used for error checking
{
	HRSRC hResInfo;
	TCHAR szBuffer[80];  // print buffer for info file
	DWORD cbWritten;     // number of bytes written to resource info file
	size_t cbString;
	HRESULT hResult;

	hResInfo = FindResourceEx(hModule, lpType, lpName, wLang);
	// Write the resource language to the resource information file.
	hResult = StringCchPrintf(szBuffer, sizeof(szBuffer)/sizeof(TCHAR), TEXT("\t\tLanguage: %u\r\n"), (USHORT) wLang);
	if (FAILED(hResult))
	{
		// Add code to fail as securely as possible.
		return FALSE;
	}

	hResult = StringCchLength(szBuffer, sizeof(szBuffer)/sizeof(TCHAR), &cbString);
	if (FAILED(hResult))
	{
		// Add code to fail as securely as possible.
		return FALSE;
	}

	WriteFile(g_hFile, szBuffer, (DWORD) cbString, &cbWritten, NULL); 
	// Write the resource handle and size to buffer.
	hResult = StringCchPrintf(szBuffer,
			sizeof(szBuffer)/sizeof(TCHAR),
			TEXT("\t\thResInfo == %lx,  Size == %lu\r\n\r\n"),
			hResInfo,
			SizeofResource(hModule, hResInfo));
	if (FAILED(hResult))
	{
		// Add code to fail as securely as possible.
		return FALSE;
	}

	hResult = StringCchLength(szBuffer, sizeof(szBuffer)/sizeof(TCHAR), &cbString);
	if (FAILED(hResult))
	{
		// Add code to fail as securely as possible.
		return FALSE;
	}

	WriteFile(g_hFile, szBuffer, (DWORD)cbString, &cbWritten, NULL);
	return TRUE;
}
Example #9
0
/* We dig into the raw resources instead of using LoadStringW() with nBufferMax
 * set to zero.
 *
 * See http://blogs.msdn.com/b/oldnewthing/archive/2004/01/30/65013.aspx.
 *
 * This allows us to do two useful things:
 *  -- Verify easily the string is zero-terminated (the assertion).
 *  -- Implement a fall-back to English, as translations can be potentially
 *     incomplete.
 */
const TCHAR*
mc_str_load(UINT id)
{
#ifndef UNICODE
    #error mc_str_load() is not (yet?) implemented for ANSI build.
#endif

    const UINT lang_id[2] = { MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
                              MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT) };
    TCHAR* rsrc_id = MAKEINTRESOURCE(id/16 + 1);
    int str_num = (id & 15);
    HRSRC rsrc;
    HGLOBAL handle;
    WCHAR* str;
    UINT len;
    int i, j;

    for(i = 0; i < MC_SIZEOF_ARRAY(lang_id); i++) {
        rsrc = FindResourceEx(mc_instance, RT_STRING, rsrc_id, lang_id[i]);
        if(MC_ERR(rsrc == NULL))
            goto not_found;
        handle = LoadResource(mc_instance, rsrc);
        if(MC_ERR(handle == NULL))
            goto not_found;
        str = (WCHAR*) LockResource(handle);
        if(MC_ERR(str == NULL))
            goto not_found;

        for(j = 0; j < str_num; j++)
            str += 1 + (UINT) *str;

        len = (UINT) *str;
        if(MC_ERR(len == 0))
            goto not_found;
        str++;

        /* Verify string resources are '\0'-terminated. This is not default
         * behavior of RC.EXE as well as windres.exe. For windres.exe we need
         * to have resources in the form "foo bar\0". For RC.EXE, we need to
         * use option '/n' to terminate the strings as RC.EXE even strips final
         * '\0' from the string even when explicitly specified. */
        MC_ASSERT(str[len - 1] == L'\0');

        return str;

not_found:
        MC_TRACE("mc_str_load: String %u missing [language 0x%x].", id,
                 (DWORD)(lang_id[i] == MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)
                    ? LANGIDFROMLCID(GetThreadLocale()) : lang_id[i]));
    }

    return _T("");
}
Example #10
0
LPCVOID TaLocale_GetResourceEx (LPCTSTR pszType, LPCTSTR pszRes, LANGID lang, HINSTANCE *phInstFound, BOOL fSearchDefaultLanguageToo)
{
   PVOID pr = NULL;

   HINSTANCE hInstance;
   for (size_t iModule = 0; TaLocale_EnumModule (iModule, &hInstance); ++iModule)
      {
      HRSRC hr;
      if ((hr = FindResourceEx (hInstance, pszType, pszRes, lang)) == NULL)
         {
         // Our translation teams don't usually change the language
         // constants within .RC files, so we should look for English
         // language translations too.
         //
         if ((hr = FindResourceEx (hInstance, pszType, pszRes, MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US))) == NULL)
            {
            // If we still can't find it, we'll take anything...
            //
            if ((hr = FindResource (hInstance, pszType, pszRes)) == NULL)
               continue;
            }
         }

      // Once we get here, we'll only fail for weird out-of-memory problems.
      //
      HGLOBAL hg;
      if ((hg = LoadResource (hInstance, hr)) != NULL)
         {
         if ((pr = (PVOID)LockResource (hg)) != NULL)
            {
            if (phInstFound)
               *phInstFound = hInstance;
            break;
            }
         }
      }

   return pr;
}
Example #11
0
//    FUNCTION: EnumLangsFunc(HANDLE, LPSTR, LPSTR, WORD, LONG)
//
//    PURPOSE:  Resource language callback
// ----------------------------------------------------------------------------------
BOOL EnumLangsFunc(HMODULE hModule,LPTSTR lpType, LPTSTR lpName, WORD wLang, LONG lParam)
{
  HRSRC hResInfo;

  hResInfo = FindResourceEx(hModule, lpType, lpName, wLang);

	resources::Resource newResource = {"", "", 0};
	QString wLangStr = QString("%1").arg((USHORT) wLang);
	QString pTypeStr = resources::convertToString(lpType);
	QString pNameStr = resources::convertToString(lpName);
	newResource.Name = pNameStr;
	newResource.Type = pTypeStr;
	newResource.Lang = wLangStr;
	resources::Resources.push_front(newResource);

  return TRUE;
}
Example #12
0
UCHAR *winResGetResource(LPCTSTR resType, LPCTSTR resName)
{
  HINSTANCE winResInstance = winResGetInstance(resType, resName);

  HRSRC hRsrc = FindResourceEx(winResInstance, resType, resName, 0);

  if(hRsrc != NULL) {
    HGLOBAL hGlobal = LoadResource(winResInstance, hRsrc);

    if(hGlobal != NULL) {
      UCHAR * b = (UCHAR *)LockResource(hGlobal);

      return b;
    }
  }
  return NULL;
}
Example #13
0
BOOL CResModule::ReplaceMenu(UINT nID, WORD wLanguage)
{
	HRSRC		hrsrc = FindResourceEx(m_hResDll, RT_MENU, MAKEINTRESOURCE(nID), wLanguage);
	HGLOBAL		hglMenuTemplate;
	WORD		version, offset;
	LPWSTR		p;
	WORD *p0;
	DWORD dwHelpId;

	if (!hrsrc)
		MYERROR;	//just the language wasn't found

	hglMenuTemplate = LoadResource(m_hResDll, hrsrc);

	if (!hglMenuTemplate)
		MYERROR;

	p = (LPWSTR)LockResource(hglMenuTemplate);

	if (p == NULL)
		MYERROR;

	//struct MenuHeader { 
	//	WORD   wVersion;           // Currently zero 
	//	WORD   cbHeaderSize;       // Also zero 
	//};

	// MENUEX resource
	//struct MenuExHeader {
	//    WORD wVersion;           // One
	//    WORD wOffset;
	//    DWORD dwHelpId;
	//};
	p0 = (WORD *)p;
	version = GET_WORD(p);

	p++;

	switch (version)
	{
	case 0:
		{
			offset = GET_WORD(p);
			p += offset;
			p++;
			size_t nMem = 0;
			if (!CountMemReplaceMenuResource((WORD *)p, &nMem, NULL))
				goto DONE_ERROR;
			WORD * newMenu = new WORD[nMem + (nMem % 2)+2];
			SecureZeroMemory(newMenu, (nMem + (nMem % 2)+2)*2);
			size_t index = 2;		// MenuHeader has 2 WORDs zero
			if (!CountMemReplaceMenuResource((WORD *)p, &index, newMenu))
			{
				delete [] newMenu;
				goto DONE_ERROR;
			} 

			if (!UpdateResource(m_hUpdateRes, RT_MENU, MAKEINTRESOURCE(nID), (m_wTargetLang ? m_wTargetLang : wLanguage), newMenu, (DWORD)(nMem + (nMem % 2)+2)*2))
			{
				delete [] newMenu;
				goto DONE_ERROR;
			}

			if ((m_wTargetLang)&&(!UpdateResource(m_hUpdateRes, RT_MENU, MAKEINTRESOURCE(nID), wLanguage, NULL, 0)))
			{
				delete [] newMenu;
				goto DONE_ERROR;
			} 
			delete [] newMenu;
		}
		break;
	case 1:
		{
			offset = GET_WORD(p);
			p++;
			dwHelpId = GET_DWORD(p);
			size_t nMem = 0;
			if (!CountMemReplaceMenuExResource((WORD *)(p0 + offset), &nMem, NULL))
				goto DONE_ERROR;
			WORD * newMenu = new WORD[nMem + (nMem % 2) + 4];
			SecureZeroMemory(newMenu, (nMem + (nMem % 2) + 4) * 2);
			CopyMemory(newMenu, p0, 2 * sizeof(WORD) + sizeof(DWORD));
			size_t index = 4;		// MenuExHeader has 2 x WORD + 1 x DWORD
			if (!CountMemReplaceMenuExResource((WORD *)(p0 + offset), &index, newMenu))
			{
				delete [] newMenu;
				goto DONE_ERROR;
			} 

			if (!UpdateResource(m_hUpdateRes, RT_MENU, MAKEINTRESOURCE(nID), (m_wTargetLang ? m_wTargetLang : wLanguage), newMenu, (DWORD)(nMem + (nMem % 2) + 4) * 2))
			{
				delete [] newMenu;
				goto DONE_ERROR;
			}

			if ((m_wTargetLang)&&(!UpdateResource(m_hUpdateRes, RT_MENU, MAKEINTRESOURCE(nID), wLanguage, NULL, 0)))
			{
				delete [] newMenu;
				goto DONE_ERROR;
			} 
			delete [] newMenu;
		}
		break;
	default:
		goto DONE_ERROR;
	}

	UnlockResource(hglMenuTemplate);
	FreeResource(hglMenuTemplate);
	return TRUE;

DONE_ERROR:
	UnlockResource(hglMenuTemplate);
	FreeResource(hglMenuTemplate);
	MYERROR;
}
Example #14
0
BOOL CResModule::ReplaceString(UINT nID, WORD wLanguage)
{
	HRSRC		hrsrc = FindResourceEx(m_hResDll, RT_STRING, MAKEINTRESOURCE(nID), wLanguage);
	HGLOBAL		hglStringTable;
	LPWSTR		p;

	if (!hrsrc)
		MYERROR;
	hglStringTable = LoadResource(m_hResDll, hrsrc);

	if (!hglStringTable)
		goto DONE_ERROR;
	p = (LPWSTR)LockResource(hglStringTable);

	if (p == NULL)
		goto DONE_ERROR;
/*	[Block of 16 strings.  The strings are Pascal style with a WORD 
	length preceding the string.  16 strings are always written, even 
	if not all slots are full.  Any slots in the block with no string 
	have a zero WORD for the length.] 
*/
	
	//first check how much memory we need
	size_t nMem = 0;
	LPWSTR pp = p;
	for (int i=0; i<16; ++i)
	{
		nMem++;
		size_t len = GET_WORD(pp);
		pp++;
		std::wstring msgid = std::wstring(pp, len);
		WCHAR * pBuf = new WCHAR[MAX_STRING_LENGTH*2];
		SecureZeroMemory(pBuf, MAX_STRING_LENGTH*2*sizeof(WCHAR));
		wcscpy(pBuf, msgid.c_str());
		CUtils::StringExtend(pBuf);
		msgid = std::wstring(pBuf);

		RESOURCEENTRY resEntry;
		resEntry = m_StringEntries[msgid];
		wcscpy(pBuf, resEntry.msgstr.c_str());
		CUtils::StringCollapse(pBuf);
		size_t newlen = wcslen(pBuf);
		if (newlen)
			nMem += newlen;
		else
			nMem += len;
		pp += len;
		delete [] pBuf;
	}

	WORD * newTable = new WORD[nMem + (nMem % 2)];
	SecureZeroMemory(newTable, (nMem + (nMem % 2))*2);

	size_t index = 0;
	for (int i=0; i<16; ++i)
	{
		int len = GET_WORD(p);
		p++;
		std::wstring msgid = std::wstring(p, len);
		WCHAR * pBuf = new WCHAR[MAX_STRING_LENGTH*2];
		SecureZeroMemory(pBuf, MAX_STRING_LENGTH*2*sizeof(WCHAR));
		wcscpy(pBuf, msgid.c_str());
		CUtils::StringExtend(pBuf);
		msgid = std::wstring(pBuf);

		RESOURCEENTRY resEntry;
		resEntry = m_StringEntries[msgid];
		wcscpy(pBuf, resEntry.msgstr.c_str());
		CUtils::StringCollapse(pBuf);
		size_t newlen = wcslen(pBuf);
		if (newlen)
		{
			newTable[index++] = (WORD)newlen;
			wcsncpy((wchar_t *)&newTable[index], pBuf, newlen);
			index += newlen;
			m_bTranslatedStrings++;
		}
		else
		{
			newTable[index++] = (WORD)len;
			if (len)
				wcsncpy((wchar_t *)&newTable[index], p, len);
			index += len;
			if (len)
				m_bDefaultStrings++;
		}
		p += len;
		delete [] pBuf;
	}

	if (!UpdateResource(m_hUpdateRes, RT_STRING, MAKEINTRESOURCE(nID), (m_wTargetLang ? m_wTargetLang : wLanguage), newTable, (DWORD)(nMem + (nMem % 2))*2))
	{
		delete [] newTable;
		goto DONE_ERROR;
	}
	
	if ((m_wTargetLang)&&(!UpdateResource(m_hUpdateRes, RT_STRING, MAKEINTRESOURCE(nID), wLanguage, NULL, 0)))
	{
		delete [] newTable;
		goto DONE_ERROR;
	}
	delete [] newTable;
	UnlockResource(hglStringTable);
	FreeResource(hglStringTable);
	return TRUE;
DONE_ERROR:
	UnlockResource(hglStringTable);
	FreeResource(hglStringTable);
	MYERROR;
}
Example #15
0
bool SolverCalculateNormals::LoadShaders()
{
	if (true == mNeedProgramReload)
	{
		mProgramZero.Clear();
		mProgramNorm.Clear();
		mProgramRecomputeNormals.Clear();
		mNeedProgramReload = false;
	}

	// check for a compute shader
	/*
	if (false == mProgramZero.PrepProgram("\\GLSL_CS\\recomputeNormalsZero.cs") )
		return false;

	if (false == mProgramRecomputeNormals.PrepProgram("\\GLSL_CS\\recomputeNormals.cs") )
		return false;
	
	if (false == mProgramNorm.PrepProgram("\\GLSL_CS\\recomputeNormalsNorm.cs") )
		return false;
		*/

	
	if (0 == mProgramZero.GetProgramId() )
	{
		bool res = false;
		char* data = nullptr;
		HINSTANCE hInst = GetThisModuleHandle();
		
		HRSRC hRes = FindResourceEx(hInst,
                           "TEXT",
                           MAKEINTRESOURCE(IDR_TEXT1),
                           MAKELANGID(LANG_NEUTRAL,
                           SUBLANG_NEUTRAL));
						   
		if(nullptr != hRes)
		{
			HGLOBAL hData = LoadResource(hInst, hRes);
			if (hData)
			{
				DWORD dataSize = SizeofResource(hInst, hRes);
				data = (char*)LockResource(hData);
				
				char *shaderData = new char[dataSize+1];
				memset( shaderData, 0, sizeof(char) * (dataSize+1) );
				memcpy( shaderData, data, sizeof(char) * dataSize );

				res = mProgramZero.PrepProgramFromBuffer(shaderData, "recomputeNormalsZero");
				
				delete [] shaderData;

				UnlockResource(hData);
			}
		}

		if (false == res)
		{
			Active =false;
			return false;
		}
	}
	if (0 == mProgramNorm.GetProgramId() )
	{
		bool res = false;
		char* data = nullptr;
		HINSTANCE hInst = GetThisModuleHandle();
		
		HRSRC hRes = FindResourceEx(hInst,
                           "TEXT",
                           MAKEINTRESOURCE(IDR_TEXT2),
                           MAKELANGID(LANG_NEUTRAL,
                           SUBLANG_NEUTRAL));
		if(nullptr != hRes)
		{
			HGLOBAL hData = LoadResource(hInst, hRes);
			if (hData)
			{
				DWORD dataSize = SizeofResource(hInst, hRes);
				data = (char*)LockResource(hData);

				char *shaderData = new char[dataSize+1];
				memset( shaderData, 0, sizeof(char) * (dataSize+1) );
				memcpy( shaderData, data, sizeof(char) * dataSize );

				res = mProgramNorm.PrepProgramFromBuffer(shaderData, "recomputeNormalsNorm");
				
				delete [] shaderData;

				UnlockResource(hData);
			}
		}

		if (false == res)
		{
			Active =false;
			return false;
		}
	}
	if (0 == mProgramRecomputeNormals.GetProgramId() )
	{
		bool res = false;
		char* data = nullptr;
		HINSTANCE hInst = GetThisModuleHandle();
		
		HRSRC hRes = FindResourceEx(hInst,
                           "TEXT",
                           MAKEINTRESOURCE(IDR_TEXT3),
                           MAKELANGID(LANG_NEUTRAL,
                           SUBLANG_NEUTRAL));
		if(nullptr != hRes)
		{
			HGLOBAL hData = LoadResource(hInst, hRes);
			if (hData)
			{
				DWORD dataSize = SizeofResource(hInst, hRes);
				data = (char*)LockResource(hData);

				char *shaderData = new char[dataSize+1];
				memset( shaderData, 0, sizeof(char) * (dataSize+1) );
				memcpy( shaderData, data, sizeof(char) * dataSize );

				res = mProgramRecomputeNormals.PrepProgramFromBuffer(shaderData, "recomputeNormals");
				
				delete [] shaderData;

				UnlockResource(hData);
			}
		}

		if (false == res)
		{
			Active =false;
			return false;
		}
	}

	return true;
}
Example #16
0
	BOOL ReplaceIconResource(LPSTR lpFileName, LPCTSTR lpName, UINT langId, LPICONDIR pIconDir,	LPICONIMAGE* pIconImage)
	{
		BOOL res=true;
		HANDLE	hFile3=NULL;
		LPMEMICONDIR lpInitGrpIconDir=new MEMICONDIR;

		//LPICONIMAGE pIconImage;
		HINSTANCE hUi;
		BYTE *test,*test1,*temp,*temp1;
		DWORD cbInit=0,cbOffsetDir=0,cbOffset=0,cbInitOffset=0;
		WORD cbRes=0;
		int i;

		hUi = LoadLibraryExA(lpFileName,NULL,DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE);
		HRSRC hRsrc = FindResourceEx(hUi, RT_GROUP_ICON, lpName,langId);
		//nu stiu de ce returneaza 104 wtf???
		//cbRes=SizeofResource( hUi, hRsrc );
		HGLOBAL hGlobal = LoadResource( hUi, hRsrc );
		test1 =(BYTE*) LockResource( hGlobal );
		temp1=test1;
	//	temp1=new BYTE[118];
	//	CopyMemory(temp1,test1,118);
		if (test1)
			{
			lpInitGrpIconDir->idReserved=(WORD)*test1;
			test1=test1+sizeof(WORD);
			lpInitGrpIconDir->idType=(WORD)*test1;
			test1=test1+sizeof(WORD);
			lpInitGrpIconDir->idCount=(WORD)*test1;
			test1=test1+sizeof(WORD);
			}
		else
			{
			lpInitGrpIconDir->idReserved=0;
			lpInitGrpIconDir->idType=1;
			lpInitGrpIconDir->idCount=0;
			}

		lpInitGrpIconDir->idEntries=new MEMICONDIRENTRY[lpInitGrpIconDir->idCount];

		for(i=0;i<lpInitGrpIconDir->idCount;i++)
			{
			lpInitGrpIconDir->idEntries[i].bWidth=(BYTE)*test1;
			test1=test1+sizeof(BYTE);
			lpInitGrpIconDir->idEntries[i].bHeight=(BYTE)*test1;
			test1=test1+sizeof(BYTE);
			lpInitGrpIconDir->idEntries[i].bColorCount=(BYTE)*test1;
			test1=test1+sizeof(BYTE);
			lpInitGrpIconDir->idEntries[i].bReserved=(BYTE)*test1;
			test1=test1+sizeof(BYTE);
			lpInitGrpIconDir->idEntries[i].wPlanes=(WORD)*test1;
			test1=test1+sizeof(WORD);
			lpInitGrpIconDir->idEntries[i].wBitCount=(WORD)*test1;
			test1=test1+sizeof(WORD);
			//nu merge cu (DWORD)*test
			lpInitGrpIconDir->idEntries[i].dwBytesInRes=pIconDir->idEntries[i].dwBytesInRes;
			test1=test1+sizeof(DWORD);
			lpInitGrpIconDir->idEntries[i].nID=(WORD)*test1;
			test1=test1+sizeof(WORD);
			}
		//	memcpy( lpInitGrpIconDir->idEntries, test, cbRes-3*sizeof(WORD) );

		UnlockResource((HGLOBAL)test1);

		LPMEMICONDIR lpGrpIconDir=new MEMICONDIR;
		lpGrpIconDir->idReserved=pIconDir->idReserved;
		lpGrpIconDir->idType=pIconDir->idType;
		lpGrpIconDir->idCount=pIconDir->idCount;
		cbRes=3*sizeof(WORD)+lpGrpIconDir->idCount*sizeof(MEMICONDIRENTRY);
		test=new BYTE[cbRes];
		temp=test;
		CopyMemory(test,&lpGrpIconDir->idReserved,sizeof(WORD));
		test=test+sizeof(WORD);
		CopyMemory(test,&lpGrpIconDir->idType,sizeof(WORD));
		test=test+sizeof(WORD);
		CopyMemory(test,&lpGrpIconDir->idCount,sizeof(WORD));
		test=test+sizeof(WORD);

		lpGrpIconDir->idEntries=new MEMICONDIRENTRY[lpGrpIconDir->idCount];
		for(i=0;i<lpGrpIconDir->idCount;i++)
			{
			lpGrpIconDir->idEntries[i].bWidth=pIconDir->idEntries[i].bWidth;
			CopyMemory(test,&lpGrpIconDir->idEntries[i].bWidth,sizeof(BYTE));
			test=test+sizeof(BYTE);
			lpGrpIconDir->idEntries[i].bHeight=pIconDir->idEntries[i].bHeight;
			CopyMemory(test,&lpGrpIconDir->idEntries[i].bHeight,sizeof(BYTE));
			test=test+sizeof(BYTE);
			lpGrpIconDir->idEntries[i].bColorCount=pIconDir->idEntries[i].bColorCount;
			CopyMemory(test,&lpGrpIconDir->idEntries[i].bColorCount,sizeof(BYTE));
			test=test+sizeof(BYTE);
			lpGrpIconDir->idEntries[i].bReserved=pIconDir->idEntries[i].bReserved;
			CopyMemory(test,&lpGrpIconDir->idEntries[i].bReserved,sizeof(BYTE));
			test=test+sizeof(BYTE);
			lpGrpIconDir->idEntries[i].wPlanes=pIconDir->idEntries[i].wPlanes;
			CopyMemory(test,&lpGrpIconDir->idEntries[i].wPlanes,sizeof(WORD));
			test=test+sizeof(WORD);
			lpGrpIconDir->idEntries[i].wBitCount=pIconDir->idEntries[i].wBitCount;
			CopyMemory(test,&lpGrpIconDir->idEntries[i].wBitCount,sizeof(WORD));
			test=test+sizeof(WORD);
			lpGrpIconDir->idEntries[i].dwBytesInRes=pIconDir->idEntries[i].dwBytesInRes;
			CopyMemory(test,&lpGrpIconDir->idEntries[i].dwBytesInRes,sizeof(DWORD));
			test=test+sizeof(DWORD);
			if(i<lpInitGrpIconDir->idCount) //nu am depasit numarul initial de RT_ICON
				lpGrpIconDir->idEntries[i].nID=lpInitGrpIconDir->idEntries[i].nID;
			else
				{
				nMaxID++;
				lpGrpIconDir->idEntries[i].nID=i+1; //adaug noile ICO la sfarsitul RT_ICON-urilor
				}
			CopyMemory(test,&lpGrpIconDir->idEntries[i].nID,sizeof(WORD));
			test=test+sizeof(WORD);
			}

		//offsetul de unde incep structurile ICONIMAGE
		cbInitOffset=3*sizeof(WORD)+lpGrpIconDir->idCount*sizeof(ICONDIRENTRY);
		cbOffset=cbInitOffset; //cbOffset=118

		FreeLibrary(hUi);

		HANDLE hUpdate;
//		_chmod((char*)lpFileName,_S_IWRITE);
		hUpdate = BeginUpdateResourceA(lpFileName, FALSE); //false sa nu stearga resursele neupdated
		if(hUpdate==NULL)
			{
			QTextStream(stdout, QIODevice::WriteOnly) << "erreur BeginUpdateResource " << lpFileName << "\n";
			res=false;
			}
		//aici e cu lang NEUTRAL
		//res=UpdateResource(hUpdate,RT_GROUP_ICON,MAKEINTRESOURCE(6000),langId,lpGrpIconDir,cbRes);
		res=UpdateResource(hUpdate,RT_GROUP_ICON,lpName,langId,temp,cbRes);
		if(res==false)
			QTextStream(stdout, QIODevice::WriteOnly) << "erreur UpdateResource RT_GROUP_ICON " << lpFileName << "\n";

		for(i=0;i<lpGrpIconDir->idCount;i++)
			{
			res=UpdateResource(hUpdate,RT_ICON,MAKEINTRESOURCE(lpGrpIconDir->idEntries[i].nID),langId,pIconImage[i],lpGrpIconDir->idEntries[i].dwBytesInRes);
			if(res==false)
				QTextStream(stdout, QIODevice::WriteOnly) << "erreur UpdateResource RT_ICON " << lpFileName << "\n";
			}

		for(i=lpGrpIconDir->idCount;i<lpInitGrpIconDir->idCount;++i)
			{
			res=UpdateResource(hUpdate,RT_ICON,MAKEINTRESOURCE(lpInitGrpIconDir->idEntries[i].nID),langId,NULL,0);
			if(res==false)
				QTextStream(stdout, QIODevice::WriteOnly) << "erreur to delete resource " << lpFileName << "\n";	
			}

		if(!EndUpdateResource(hUpdate,FALSE)) //false ->resource updates will take effect.
			QTextStream(stdout, QIODevice::WriteOnly) << "eroare EndUpdateResource" << lpFileName << "\n";

		//	FreeResource(hGlobal);
		delete[] lpGrpIconDir->idEntries;
		delete lpGrpIconDir;
		delete[] temp;

		return res;
	}
Example #17
0
LPCSTRINGTEMPLATE TaLocale_GetStringResource (int ids, HINSTANCE *phInstFound)
{
    // Strings are organized into heaps of String Tables, each table
    // holding 16 strings (regardless of their length). The first table's
    // first string index is for string #1. When searching for a string,
    // the string's table is the index given to FindResource.
    //
    LPCSTRINGTEMPLATE pst = NULL;
    LANGID lang = TaLocale_GetLanguage();

    int iTable = (ids / 16) + 1;           // 1 = first string table
    int iIndex = ids - ((iTable-1) * 16);  // 0 = first string in the table

    HINSTANCE hInstance = NULL;
	size_t iModule = 0;
    for (; !pst && TaLocale_EnumModule (iModule, &hInstance); ++iModule)
    {
        HRSRC hr;
        if ((hr = FindResourceEx (hInstance, RT_STRING, MAKEINTRESOURCE( iTable ), lang)) == NULL)
        {
            // Our translation teams don't usually change the language
            // constants within .RC files, so we should look for English
            // language translations too.
            //
            if ((hr = FindResourceEx (hInstance, RT_STRING, MAKEINTRESOURCE( iTable ), MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US))) == NULL)
            {
                // If we still can't find it, we'll take anything...
                //
                if ((hr = FindResource (hInstance, RT_STRING, MAKEINTRESOURCE( iTable ))) == NULL)
                    continue;
            }
        }

        HGLOBAL hg;
        if ((hg = LoadResource (hInstance, hr)) != NULL)
        {
            const WORD *pTable;
            if     ((pTable = (WORD*)LockResource (hg)) != NULL)
            {
                try {
                    // Skip words in the string table until we reach the string
                    // index we're looking for.
                    //
                    for (int iIndexWalk = iIndex; iIndexWalk && ((LPCSTRINGTEMPLATE)pTable)->cchString; --iIndexWalk) {
                        pTable += 1 + ((LPCSTRINGTEMPLATE)pTable)->cchString;
                    }

                    if (IsValidStringTemplate ((LPCSTRINGTEMPLATE)pTable))
                    {
                        pst = (LPCSTRINGTEMPLATE)pTable;
                        if (phInstFound)
                            *phInstFound = hInstance;
                    } else {
                        UnlockResource(pTable);
                        FreeResource(hg);
                    }
                }
                catch(...)
                {
                    UnlockResource(pTable);
                    FreeResource(hg);
                    // If we walked off the end of the table, then the
                    // string we want just wasn't there.
                }
            }
        }
    }

    return pst;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// Load string from resource with special langID
//
BOOL LoadStringExx(
					 HINSTANCE hInst,			// Hinstance of lib
					 WORD wLangID,				// Language ID of resource
					 PRES_STRING_INFO pInfo		// Pointer to the string info
					 )

{
	HRSRC			hFindRes;		// Handle of the resources has been found
	HGLOBAL			hLoadRes;		// Handle of the resources has been loaded
	LPVOID			pRes;			// Pointer to the resources
	UINT			nBlockID;		// String block ID

	pInfo->dwFileOffset	= 0;		// String offset in the file
	pInfo->dwBytes		= 0;		// String length, in bytes
	pInfo->pszText		= NULL;

	nBlockID = pInfo->uStringID / 16 + 1;

	__try
	{
		// find the string block
		hFindRes = FindResourceEx(hInst, RT_STRING, MAKEINTRESOURCE(nBlockID), wLangID);
		if(!hFindRes )
		{
			__leave;
		}	

		hLoadRes = LoadResource(hInst, hFindRes);
		if(!hLoadRes )
		{
			__leave;
		}

		pRes = LockResource(hLoadRes);
		if(!pRes )
		{
			__leave;
		}

		WCHAR*		pParse		= (WCHAR *)pRes;				// Pointer to the String block
		UINT		nIndex		= pInfo->uStringID % 16;		// Calculate the string index
		int			nLen;
		UINT		i;

		// 16 strings per block
		for( i = 0; i < (nIndex & 15); i++ )
		{
			pParse += 1 + (int)*pParse;
		}

		// OK, we get it
		nLen = (UINT)*pParse;		// The length of the target string.
		pParse += 1;				// Pointer to the target string


		// Main point, calculate the string offset
		pInfo->dwFileOffset	= (DWORD) ( (DWORD_PTR)pParse - (DWORD_PTR)hInst ) + 1;
		pInfo->dwBytes		= nLen * sizeof(WCHAR);

		// allocate memory
		pInfo->pszText = (LPWSTR)MALLOC((nLen + 1) * sizeof(WCHAR));
		if (!pInfo->pszText)
			__leave;

		// copy string for return
		CopyMemory((LPVOID)pInfo->pszText, (LPVOID)pParse, pInfo->dwBytes);
		*(PWCHAR)((DWORD_PTR)pInfo->pszText + pInfo->dwBytes) = 0;


		//TRACEF(_T("String ID: %5d \t%s"), pszText);

	}
	__finally
	{
		// Clean up, free memory

		if (pRes)
			UnlockResource(pRes);

		if (hFindRes)
			FreeResource(hFindRes);
	}

	// if pointer is null, we return a NULL string
	if (!pInfo->pszText)
	{
		pInfo->pszText		= (LPWSTR)MALLOC(sizeof(WCHAR));
		pInfo->pszText[0]	= 0;
	}

	return TRUE;

} // LoadStringExx()
HRESULT FindStringResourceEx(
    __in                        HINSTANCE         hInstance,
    __in                        UINT              uID,
    __in                        UINT              nLangID,
    __deref_out_ecount(*pcch)   const WCHAR     **ppcstrResource,
    __out                       size_t           *pcch)
{
    *ppcstrResource = NULL;
    *pcch = 0;

    // Convert the string ID into a bundle number
    // Strings listed in .rc files are grouped together in
    // bundles of 16, starting with bundle number 1.
    HRSRC hrsrc = FindResourceEx(
                        hInstance,
                        RT_STRING,
                        MAKEINTRESOURCE(uID / 16 + 1),
                        (WORD) nLangID);
    HRESULT hr = (hrsrc != NULL) ? S_OK : ResultFromKnownLastError();
    if (SUCCEEDED(hr))
    {
        // Load the resource bundle into memory.
        HGLOBAL hglob = LoadResource(hInstance, hrsrc);
        hr = (hglob != NULL) ? S_OK : ResultFromKnownLastError();
        if (SUCCEEDED(hr))
        {
            // Strings in the resource table are not null terminated.
            // Treat a failure to lock the resource as an empty string, since
            // LockResource() isn't documented to call SetLastError().
            const WCHAR *pcstr = NULL;
            pcstr = reinterpret_cast<const WCHAR *>(LockResource(hglob));