• 0

[c/c++] External DLL Resource export


Question

6 answers to this question

Recommended Posts

  • 0

Programmatically? LoadLibrary to load the DLL, FindResource to get the resource handle, LoadResource to turn that into a memory handle, LockResource to turn that into a memory pointer, and then from that pointer, write SizeofResource bytes to a file of your choosing.

Or you can use any old resource editor to extract it manually.

Link to comment
Share on other sites

  • 0

Coded this quick in Textmate, untested:

DWORD SaveResource(__in_z LPCTSTR lpcszFileName, __in_z LPCTSTR lpcszType, __in_z LPCTSTR lpcszName, __out_ecount(nSize) PBYTE pbData, __in SIZE_T nSize)
{
  HMODULE hModule;
  HGLOBAL hGlobal;
  LPVOID  lpBuffer;
  HRSRC   hResource;
  DWORD   dwSize, dwRET = 0;

  hModule = LoadLibrary(lpcszFileName);
  if (hModule != NULL)
  {
	hResource = FindResource(hModule, lpcszName, lpcszType);
	if (hResource != NULL)
	{
	  dwSize  = SizeofResource(hModule, hResource);
	  hGlobal = LoadResource(hModule, hResource);
	  if (hGlobal != NULL || dwSize != 0)
	  {
		lpBuffer = LockResource(hGlobal);
		__try {
		  CopyMemory(pbData, lpBuffer, min(dwSize, nSize));
		  dwRET = dwSize;
		}
		__catch (EXCEPTION_EXECUTE_HANDLER) {
#ifdef _DEBUG
		  OutputDebugString(TEXT("Exception occurred whilst trying to copy resource!\r\n"));
#endif
		}
	  }
	}
	FreeLibrary(hModule); // dec load count
  }
  return dwRET;
}

Example Usage:

  DWORD   dwSize;
  BYTE	bResource[1024];

  dwSize = SaveResource(TEXT("lol.dll"), TEXT("AUDIO"), TEXT("gaysound.mp3"), bResource, sizeof(bResource));
  if (dwSize != 0)
	_tprintf(TEXT("resource saved! resource size: 0x%08X"), dwSize);
  else
	_ftprintf(stderr, TEXT("could not save resource!"));

Link to comment
Share on other sites

  • 0

If you're still having trouble, I had a thread about creating and using a DLL in C/C++. I've since refined the code a bit(some of the code is unnecessary) I could try finding it if you still need help.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.