Discussion:
GetModuleHandle problem
(too old to reply)
sleeper
2006-08-29 07:14:58 UTC
Permalink
Hello

I am trying to use GetModuleHandle and in the documentation it says:

"Retrieves a module handle for the specified module. The module must
have been loaded by the calling process."

So I am having a problem because in my Executable, it loads

user32.dll which loads advapi32.dll which loads secur32.dll which loads
ntdsapi.dll which loads ws2_32.dll. I got this information from
Dependecy Walker. So in other words, the chain looks something like
this:

user32.dll -> advapi32.dll -> secur32.dll -> ntdsapi.dll -> ws2_32.dll

I try to call GetModuleHandle("ws2_32.dll") but it returns NULL,
meaning that it can't get the module handle. I think the problem is
because my executable doesn't load it directly. Instead it loads a DLL
that loads another DLL that eventually loads my DLL.

I hope that makes sense. So like i said, in the msdn documentation it
says that the "module must have been loaded by the calling process" in
which in this case, it doesn't.

How would I go about getting the module handle of ws2_32.dll from my
process?

thanks
Mihajlo Cvetanović
2006-08-29 09:45:08 UTC
Permalink
Post by sleeper
I try to call GetModuleHandle("ws2_32.dll") but it returns NULL,
meaning that it can't get the module handle. I think the problem is
because my executable doesn't load it directly. Instead it loads a DLL
that loads another DLL that eventually loads my DLL.
I hope that makes sense. So like i said, in the msdn documentation it
says that the "module must have been loaded by the calling process" in
which in this case, it doesn't.
There is only one process, and that process loads the module. The fact
that the code that the process executes (or rather the thread within
process) is from another module doesn't matter.
Post by sleeper
How would I go about getting the module handle of ws2_32.dll from my
process?
I think that GetModuleHandle function is case sensitive. Try
"WS2_32.DLL", or some other derivation.
Vipin
2006-08-29 15:58:45 UTC
Permalink
module name need not be case sensitive when passed to GetModuleHandle(...).
The best way to see if the module is loaded in a process is to attach a
debugger to the process and then watch the modules list.
--
Vipin Aravind
http://www.explorewindows.com/Blogs
Post by Mihajlo Cvetanović
Post by sleeper
I try to call GetModuleHandle("ws2_32.dll") but it returns NULL,
meaning that it can't get the module handle. I think the problem is
because my executable doesn't load it directly. Instead it loads a DLL
that loads another DLL that eventually loads my DLL.
I hope that makes sense. So like i said, in the msdn documentation it
says that the "module must have been loaded by the calling process" in
which in this case, it doesn't.
There is only one process, and that process loads the module. The fact
that the code that the process executes (or rather the thread within
process) is from another module doesn't matter.
Post by sleeper
How would I go about getting the module handle of ws2_32.dll from my
process?
I think that GetModuleHandle function is case sensitive. Try "WS2_32.DLL",
or some other derivation.
Joseph M. Newcomer
2006-08-29 16:07:21 UTC
Permalink
It isn't case-sensitive. From the documentation

"The name is compared (case-independently) to the names of modules currently mapped into
the address space of the calling process."
joe
Post by Mihajlo Cvetanović
Post by sleeper
I try to call GetModuleHandle("ws2_32.dll") but it returns NULL,
meaning that it can't get the module handle. I think the problem is
because my executable doesn't load it directly. Instead it loads a DLL
that loads another DLL that eventually loads my DLL.
I hope that makes sense. So like i said, in the msdn documentation it
says that the "module must have been loaded by the calling process" in
which in this case, it doesn't.
There is only one process, and that process loads the module. The fact
that the code that the process executes (or rather the thread within
process) is from another module doesn't matter.
Post by sleeper
How would I go about getting the module handle of ws2_32.dll from my
process?
I think that GetModuleHandle function is case sensitive. Try
"WS2_32.DLL", or some other derivation.
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
sleeper
2006-08-29 19:16:02 UTC
Permalink
Thanks for the help everyone, I appreciate it a lot.

so I decided to dig into the program more. I decided to use the
dependency walker which told me all the DLLs that the executable is
loaded up with. It shows that my executable is loaded up with only
most common DLLs directly ( i.e Kernel32.dll, User32.dll etc), yet it
doesn't load WS2_32.dll. I decided to load the process into memory and
use the Process Explorer, which tells me all the DLLs that are loaded
into its memory space. In Process Explorer, it says that WS2_32.DLL is
loaded into its memory space.

This is really wierd to me. In Dependecy Walker, it shows me a tree
like structure and it shows that WS2_32 is only loaded to satisfy the
dependecy of other DLLs. I should still be able to call
GetModuleHandle("ws2_32.dll") correct? but it returns NULL.

I am now thinking that maybe it is because ws2_32 is dynamically
linked? as opposed to the other DLLs ( i.e Kernel32.dll, User32.dll
etc. ) that are statically linked? So therefore I can't get the module
handle of the dynamically linked ws2_32?

thanks
Joseph M. Newcomer
2006-08-30 15:10:53 UTC
Permalink
Well, the AfxInitSockets might be causing the load of the DLL to happen. In that case,
you can't do a GetModuleHandle until it is loaded. Then you can retrieve the handle.

Set some breakpoints in your code, as I suggested. Set a breakpoint at your
GetModuleHandle, and when you take the breakpoint, use the process explorer. If the
process explorer at that point says that ws2_32.dll is loaded, then there's something else
wrong; if it is not loaded, then you can't do it at that point.

Show your actual code, and say where the code is executed.
joe
Post by sleeper
Thanks for the help everyone, I appreciate it a lot.
so I decided to dig into the program more. I decided to use the
dependency walker which told me all the DLLs that the executable is
loaded up with. It shows that my executable is loaded up with only
most common DLLs directly ( i.e Kernel32.dll, User32.dll etc), yet it
doesn't load WS2_32.dll. I decided to load the process into memory and
use the Process Explorer, which tells me all the DLLs that are loaded
into its memory space. In Process Explorer, it says that WS2_32.DLL is
loaded into its memory space.
This is really wierd to me. In Dependecy Walker, it shows me a tree
like structure and it shows that WS2_32 is only loaded to satisfy the
dependecy of other DLLs. I should still be able to call
GetModuleHandle("ws2_32.dll") correct? but it returns NULL.
I am now thinking that maybe it is because ws2_32 is dynamically
linked? as opposed to the other DLLs ( i.e Kernel32.dll, User32.dll
etc. ) that are statically linked? So therefore I can't get the module
handle of the dynamically linked ws2_32?
thanks
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Mihajlo Cvetanović
2006-08-31 09:37:58 UTC
Permalink
Post by Joseph M. Newcomer
Set some breakpoints in your code, as I suggested. Set a breakpoint at your
GetModuleHandle, and when you take the breakpoint, use the process explorer. If the
process explorer at that point says that ws2_32.dll is loaded, then there's something else
wrong; if it is not loaded, then you can't do it at that point.
Visual Studio can show you the list of loaded modules in one of its
debugging windows.

And I was of course wrong about case sensitivity. Took me a while to
figure out why I had that idea; it's related to delay loading of DLL-s
and __HrLoadAllImportsForDll function.

David Ching
2006-08-29 14:13:14 UTC
Permalink
Post by sleeper
I try to call GetModuleHandle("ws2_32.dll") but it returns NULL,
I think it may be because I think ws2_32.dll just forwards all the function
requests to another DLL. Maybe it doesn't load and stay in memory. I would
use Process Explorer (sysinternals.com) to see which DLL's are loaded in
your process. Maybe there's another Winsock DLL there instead.

-- David
http://www.dcsoft.com
Joseph M. Newcomer
2006-08-29 16:06:20 UTC
Permalink
Note that the specification says "retrieves a module handle for the specified module if
the file has been mapped into the address space of the calling process". It does not
indicate in any way the dependency graph plays any part in it. So the dependency path
doesn't matter.

What you haven't indicated is the point where you do this. There are some weird corner
cases, such as static constructors and DllMain, where you could have a failure, but it
seems unlikely.

One way to check this out is to get the process viewer from www.sysinternals.com and at
the point where you are about to call GetModuleHandle, set a breakpoint, then use the
process viewer to examine the contents of the process. Go to view, lower pane, DLLs. If
the DLL is present, then there is something weird going on; if it isn't present, this
would indicate why you are getting the error.

By the way, for Unicode compliance you should always program Unicode-aware, which means
that the call should have been
GetModuleHandle(_T("ws2_32.dll"));
but that would have nothing to do with the problem you are seeing.
joe
Post by sleeper
Hello
"Retrieves a module handle for the specified module. The module must
have been loaded by the calling process."
So I am having a problem because in my Executable, it loads
user32.dll which loads advapi32.dll which loads secur32.dll which loads
ntdsapi.dll which loads ws2_32.dll. I got this information from
Dependecy Walker. So in other words, the chain looks something like
user32.dll -> advapi32.dll -> secur32.dll -> ntdsapi.dll -> ws2_32.dll
I try to call GetModuleHandle("ws2_32.dll") but it returns NULL,
meaning that it can't get the module handle. I think the problem is
because my executable doesn't load it directly. Instead it loads a DLL
that loads another DLL that eventually loads my DLL.
I hope that makes sense. So like i said, in the msdn documentation it
says that the "module must have been loaded by the calling process" in
which in this case, it doesn't.
How would I go about getting the module handle of ws2_32.dll from my
process?
thanks
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Loading...