Click here to Skip to main content
15,891,775 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Thread sync Pin
Randor 2-Jan-12 21:01
professional Randor 2-Jan-12 21:01 
AnswerRe: Thread sync Pin
Bob Ciora4-Jan-12 1:06
Bob Ciora4-Jan-12 1:06 
GeneralRe: Thread sync Pin
columbos149274-Jan-12 1:26
columbos149274-Jan-12 1:26 
AnswerRe: Thread sync Pin
JackDingler4-Jan-12 12:19
JackDingler4-Jan-12 12:19 
QuestionDLL Questions Pin
Richard Andrew x642-Jan-12 17:19
professionalRichard Andrew x642-Jan-12 17:19 
AnswerRe: DLL Questions Pin
Rajesh R Subramanian2-Jan-12 17:41
professionalRajesh R Subramanian2-Jan-12 17:41 
GeneralRe: DLL Questions Pin
Richard Andrew x642-Jan-12 17:45
professionalRichard Andrew x642-Jan-12 17:45 
AnswerRe: DLL Questions Pin
Randor 2-Jan-12 18:50
professional Randor 2-Jan-12 18:50 
Hi Richard,
Richard Andrew x64 wrote:
How do we determine the base address of a loaded DLL?


The HMODULE that you get from the GetModuleHandle function[^] is the same as the base address. If you are writing MFC applications... this will be the same address returned from AfxGetResourceHandle [^]. The AfxGetInstanceHandle[^] function is actually returning the base address of the main application image.

If you are using a Microsoft compiler then you also have access to an extern variable __ImageBase which can be used to access the these base addresses. Because it is equal to an HMODULE you can also use this variable for loading resources[^].

Now that we have the base address we can read the PE image headers [^]and check for a size:

C++
DWORD GetSizeOfImage(HMODULE hModule)
{
	DWORD dwSize = 0;
	PIMAGE_DOS_HEADER pDOSHeader = (PIMAGE_DOS_HEADER)hModule;
	PIMAGE_NT_HEADERS pNTHeader = (PIMAGE_NT_HEADERS)((PBYTE)hModule + pDOSHeader->e_lfanew);
	if(IMAGE_NT_SIGNATURE == pNTHeader->Signature)
	{
		PIMAGE_FILE_HEADER pFileHeader = (PIMAGE_FILE_HEADER)((PBYTE)&pNTHeader->FileHeader);
		PIMAGE_OPTIONAL_HEADER pOptionalHeader = (PIMAGE_OPTIONAL_HEADER)((PBYTE)&pNTHeader->OptionalHeader);
		if(IMAGE_NT_OPTIONAL_HDR32_MAGIC ==  pNTHeader->OptionalHeader.Magic)
		{
			dwSize = pNTHeader->OptionalHeader.SizeOfImage;
		}
	}
	return dwSize;
}



Richard Andrew x64 wrote:
Also, is there some way to tell the system to load a DLL at a base address that I specify, regardless of the image's preferred base address?


Maybe... check out the /FIXED (Fixed Base Address)[^] linker option.

I consider fixed base addresses to be an insecure option... I highly recommend that all software engineers support a dynamic base address.

PE images with support for ASLR[^] will have the IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE bit set in the PE image characteristics. These modules are essentially saying 'put me anywhere you want... I don't care' and the kernel pseudo-randomly chooses an address to map the image.

PE images without support for ASLR... the NT kernel will make an attempt to load the image at its preferred address at pNTHeader->OptionalHeader.ImageBase. If the address is already allocated the kernel will make an attempt to map the image at another address and apply its fixups.

Best Wishes,
-David Delaune
GeneralRe: DLL Questions Pin
Richard Andrew x642-Jan-12 18:57
professionalRichard Andrew x642-Jan-12 18:57 
GeneralRe: DLL Questions Pin
Randor 2-Jan-12 19:13
professional Randor 2-Jan-12 19:13 
QuestionHow to modify hardware's property via win32 API Pin
freshtiny2-Jan-12 16:17
freshtiny2-Jan-12 16:17 
AnswerRe: How to modify hardware's property via win32 API Pin
Vaclav_2-Jan-12 17:04
Vaclav_2-Jan-12 17:04 
AnswerRe: How to modify hardware's property via win32 API Pin
freshtiny2-Jan-12 21:05
freshtiny2-Jan-12 21:05 
QuestionDynamic array of afx_msg functions - using CPtrArray ( MFC VC 6.0) Pin
Vaclav_2-Jan-12 15:56
Vaclav_2-Jan-12 15:56 
AnswerRe: Dynamic array of afx_msg functions - using CPtrArray ( MFC VC 6.0) Pin
Chuck O'Toole2-Jan-12 16:01
Chuck O'Toole2-Jan-12 16:01 
GeneralRe: Dynamic array of afx_msg functions - using CPtrArray ( MFC VC 6.0) Pin
Vaclav_2-Jan-12 16:39
Vaclav_2-Jan-12 16:39 
GeneralRe: Dynamic array of afx_msg functions - using CPtrArray ( MFC VC 6.0) Pin
ThatsAlok3-Jan-12 19:45
ThatsAlok3-Jan-12 19:45 
GeneralRe: Dynamic array of afx_msg functions - using CPtrArray ( MFC VC 6.0) Pin
Chuck O'Toole3-Jan-12 21:38
Chuck O'Toole3-Jan-12 21:38 
GeneralRe: Dynamic array of afx_msg functions - using CPtrArray ( MFC VC 6.0) Pin
ThatsAlok3-Jan-12 23:12
ThatsAlok3-Jan-12 23:12 
AnswerRe: Dynamic array of afx_msg functions - using CPtrArray ( MFC VC 6.0) Pin
Vaclav_3-Jan-12 4:55
Vaclav_3-Jan-12 4:55 
AnswerRe: Dynamic array of afx_msg functions - using CPtrArray ( MFC VC 6.0) Pin
ThatsAlok3-Jan-12 19:43
ThatsAlok3-Jan-12 19:43 
GeneralRe: Dynamic array of afx_msg functions - using CPtrArray ( MFC VC 6.0) Pin
Vaclav_4-Jan-12 7:16
Vaclav_4-Jan-12 7:16 
AnswerRe: Dynamic array of afx_msg functions - using CPtrArray ( MFC VC 6.0) Pin
ThatsAlok5-Jan-12 2:44
ThatsAlok5-Jan-12 2:44 
GeneralRe: Dynamic array of afx_msg functions - using CPtrArray ( MFC VC 6.0) Pin
Vaclav_5-Jan-12 6:08
Vaclav_5-Jan-12 6:08 
Questionclass array, better understanding of it Pin
jkirkerx2-Jan-12 13:14
professionaljkirkerx2-Jan-12 13:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.