extracting embedded dll

Jul 27, 2017 at 2:31pm
I'm learning to extract a dll, embedded to an executable via .rc file like this:

1 BINARY MOVEABLE PURE "MyLib.dll"

I'm trying to do that using this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
bool ExtractResource(std::uint16_t ResourceID, std::string OutputFileName, const char* ResType)
{
	try
	{
		HRSRC hResource = FindResource(NULL, MAKEINTRESOURCE(ResourceID), ResType);
		if (hResource == NULL)
		{
			return false;
		}

		HGLOBAL hFileResource = LoadResource(NULL, hResource);
		if (hFileResource == NULL)
		{
			return false;
		}

		void* lpFile = LockResource(hFileResource);
		if (lpFile == NULL)
		{
			return false;
		}

		std::uint32_t dwSize = SizeofResource(NULL, hResource);
		if (dwSize == 0)
		{
			return false;
		}

		HANDLE hFile = CreateFile(OutputFileName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
		HANDLE hFilemap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwSize, NULL);
		if (hFilemap == NULL)
		{
			return false;
		}

		void* lpBaseAddress = MapViewOfFile(hFilemap, FILE_MAP_WRITE, 0, 0, 0);
		CopyMemory(lpBaseAddress, lpFile, dwSize);
		UnmapViewOfFile(lpBaseAddress);
		CloseHandle(hFilemap);
		CloseHandle(hFile);

		return true;
	}
	catch (...) {}
	return false;
}


procedure call in main():

ExtractResource(1,libpath,"BINARY");


However, the dll is created in specified directory but is not usable. Message I'm getting with this extracted dll is:

The ordinal 20 could not be located in the dynamic link library


Any ideas, what could cause this error?

Last edited on Jul 27, 2017 at 2:55pm
Jul 28, 2017 at 10:49am
Not sure where you found the code but it looks very odd.
BINARY is not a valid resource type - have a look at MSDN
https://msdn.microsoft.com/en-us/library/windows/desktop/ms648009(v=vs.85).aspx

Also just returning false will not help you to find the problem. Much better to use GetLastError and display the error code.
Topic archived. No new replies allowed.