• C

An API detour question

When I am load a DLL library in my process, is the shared DLL code shared same memory with other process that us the same DLL?

Here is the use case,
When I detour and API, say "CreateProcess" from "kernel32.dll", will it intercept the function calls only from this process or it can come from all process?

Thank you for any answer.
Evan LiSr SW EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shared DLL code shared same memory with other process that us the same DLL?
that is true for static data. it is false for local variables used or for dynamic memory allocated.

if using global or static data in the dll you should always make sure that it is constant data valid for all processes using the dll.

"CreateProcess" from "kernel32.dll", will it intercept the function calls only from this process or it can come from all process?
don't know exactly what you mean by that. kernel32.dll never would be unloaded and each call of one of the exported dll functions is independent to other calls running parallel or later regardless of whether the calls were coming from same process and same thread, or from other thread or from other process. of course you need to care for thread-safety if using global or shared data for input arguments but the code of the dll function is always safe since each call runs a copy of the original code.

Evan LiSr SW EngineerAuthor Commented:
Thank you Sara,
I am not asking questions of data. I am asking questions of the code, as when we do the detour of an API we are changing the code of that API for the first 5-6 bytes, here is the reference about what happens:

My question is that when we do the detour, are we going to detour only my the calls from my process or all calls from the system for this API.

Looks that it is only affect one my process, and I do not understand that.
detours creates new binary code for each dll function detoured and creates a safe copy of the code for each call. the new code is never a subject of to being shared with other threads let alone with other processes.

you may not worry about code but only about data.

Evan LiSr SW EngineerAuthor Commented:
Hi Sara,

Thank for your answer.

I am little more confused.

Here is more question now:

When we loadlibrary, will the OS duplicate the code that other people has loaded for other process in my process space? If yes, it will be too many GDI.dll, kernel.dll etc.

If not, when we do detour, we'll change other process space, is it possible?

Or are you saying that when we call getprocaddress, the function code gets duplicated in my process space.

Anyway, I need to know what is happening underline. I could not find the answer from Microsoft paper. Thanks for further help.

If yes, it will be too many GDI.dll, kernel.dll etc.
LoadLibrary doesn't copy code. it was copied after GetProcAddress returned a valid function pointer and if your code contains a call by using this pointer. detour would intercept directly at this point.

note, copying of code is essential since the process address spaces are strictly separated. even if your process calls into a COM function or into .NET the executed code always would be mapped into your process space.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Evan LiSr SW EngineerAuthor Commented:
You are right. if we get an address that is in my process space, it must be copied. But, I could not find any document that talk about when I get the address successfully, the function code is copied to process space?
Evan LiSr SW EngineerAuthor Commented:
Thank you for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.