Jump to content
  • Advertisement
Sign in to follow this  
Plerion

Reading resources from PE-files

This topic is 3001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! After parsing several directory-entries of the PE-format (imports, exports, delayed imports/exports) im a bit stuck at parsing resources. I successfully get the roots of the tree, the categories, of the resources. My application hast some some dialogs, a menu and a manifest. Thats correctly reflected by the output of my code. Now i want to go to the next node of the tree. Im using the following code:
DWORD dwOffset = pEntries.OffsetToData & ~0x80000000;
PIMAGE_RESOURCE_DIRECTORY_ENTRY pNext = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(((LPBYTE)pResDir) + dwOffset);
The problem is now, that pNext is completly filled with 0. Name/Id and Offset are 0. That of course should not be. What am i doing wrong? This is my code:
void PEFile::InitResources()
{
	DWORD dwResRVA = m_ntHeader.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress;
	PIMAGE_RESOURCE_DIRECTORY pResDir = (PIMAGE_RESOURCE_DIRECTORY)GetAbsolutePtr(dwResRVA);
	if(!pResDir)
		return;


	PIMAGE_RESOURCE_DIRECTORY_ENTRY pEntries = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(((LPBYTE)pResDir) + sizeof(IMAGE_RESOURCE_DIRECTORY));
	for(UINT32 i = 0; i < pResDir->NumberOfNamedEntries; ++i)
	{
		if(pEntries.NameIsString)
		{
			DWORD dwOffset = pEntries.NameOffset;
			DWORD dwNameRVA = dwOffset + dwResRVA;
			PIMAGE_RESOURCE_DIRECTORY_STRING str = (PIMAGE_RESOURCE_DIRECTORY_STRING)GetAbsolutePtr(dwNameRVA);
			if(str)
				std::cout << str->NameString << std::endl;
		}
		else
			std::cout << "ID: " << pEntries.Id << std::endl;
		if(pEntries.DataIsDirectory)
			std::cout << "-> Subdirectory" << std::endl;
		else
			std::cout << "-> Data" << std::endl;
	}
	for(UINT32 i = pResDir->NumberOfNamedEntries, j = 0; j < pResDir->NumberOfIdEntries; ++i, ++j)
	{
		if(pEntries.Name & 0x80000000)
		{
			DWORD dwOffset = pEntries.Name & ~0x80000000;
			DWORD dwNameRVA = dwOffset + dwResRVA;
			PIMAGE_RESOURCE_DIRECTORY_STRING str = (PIMAGE_RESOURCE_DIRECTORY_STRING)GetAbsolutePtr(dwNameRVA);
			if(str)
				std::cout << str->NameString << std::endl;
		}

		if(pEntries.DataIsDirectory)
		{
			if(pEntries.Id <= (WORD)RT_MANIFEST)
			{
				std::cout << "Collection of " << gResourceTypes[pEntries.Id - 1] << "'s found!" << std::endl;
			}

			DWORD dwOffset = pEntries.OffsetToData & ~0x80000000;
			PIMAGE_RESOURCE_DIRECTORY_ENTRY pNext = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(((LPBYTE)pResDir) + dwOffset);

			if(pNext)
				RecursiveParseResource((DWORD)pResDir, pNext);
		}
		else
			std::cout << "Is data!" << std::endl;
	}

}
Greetings Plerion

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!