w2000 HeapAlloc and RtlAllocateHeap

I have an NT program that calls HeapAlloc by specifying the import from KERNEL32.DLL

When I debug the application using MSVC I see that the code that should be calling the HeapAlloc function in KERNEL32.DLL is instead pointing to RtlAllocateHeap in NTDLL.DLL

Now this would not normally matter but it is important for me because of the following reasons.

I am writing a 'wrapper' which encrypts the original exe file and when I unwrap it I effectively carry out the functions of the Windows loader and automatically point the import to the HeapAlloc in KERNEL32.DLL as specified in the import table.

On running the program however it causes an exception shortly after entering HeapAlloc.

Looking at other 'Heap' routines such as HeapCreate I can see that they are pointing to the ones I expect in KERNEL32.DLL.

Question.
What is causing this effect?
How can I emulate the windows loader to change this (and perhaps other imports) which are being forced from one dll to another?
LVL 5
icdAsked:
Who is Participating?
 
pjknibbsCommented:
Under NT the HeapAlloc() function does not actually exist anywhere on the system--any code which calls it is automatically forwarded to this RtlAllocateHeap() function you've come across. (This is an instance of Win32 function forwarding, which is a feature of Win32 DLLs). It's implemented like this because the heap allocation procedure has to work at a lower level than KERNEL32.DLL allows. If you called GetProcAddress() with the HeapAlloc implementation in KERNEL32 it would silently call itself to find the right entrypoint in NTDLL.DLL, which may or may not help your other situation. To be honest trying to emulate the Windows loader is going to be very difficult under NT, which does a lot of its work at a level you can't access from user-mode code.
0
 
icdAuthor Commented:
pjknibbs

Are there any resources you are aware of that explain this action? Do you know if this type of forwarding is used on any other call other than HeapAlloc or can it be done on any call?
0
 
NickRepinCommented:
HeapReAlloc, HeapSize, xxxCriticalSection
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
NickRepinCommented:
0
 
pjknibbsCommented:
icd: You can find out which calls in a DLL are forwarded by using DUMPBIN /EXPORTS on it. I tried this with KERNEL32 and it listed four of the HeapXXXX functions, plus critical section functions, as "forwarded to XXX".

You can set your own DLLs up to have forwarded functions, but the documentation on how to do it is woefully skimpy.
0
 
icdAuthor Commented:
pjknibbs
You were the first to explain what was going on.

NickRepin
Your web references were exactly what I needed to change my code (which now works!)

I feel that NickRepin deserves the points since his comment was the most relevent to my solving the problem but I don't want to upset pjnibbs.
0
 
pjknibbsCommented:
You have two choices:

1) Ignore my contribution, since, as you admit, NickRepin's references solved your problem.

2) Split the points by asking in the Community Support section if they'll split the question.
0
 
darinwCommented:
Community Support has reduced points from 200 to 100
0
 
darinwCommented:
Hello everyone,

Reducing points to one half to allow for split.

You can now accept one of the comments in this thread as an answer. To award the other Expert, you can create a new question in this topic area with a title of 'For ExpertName -- 10345456' using that Experts username.

Remember, the Accept Comment as Answer button is in the header of the comment.

For your convenience, you can use this link to create the new question:
http://www.experts-exchange.com/bin/NewQForm?ta=45

darinw
Customer Service
0
 
icdAuthor Commented:
pjknibbs
Thanks for your help, once again E-E comes through!

NickRepin.
I will post another question for you to 'answer'.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.