Thursday, August 21, 2008

About LoadLibraryEx(), FreeLibrary() and GetProcAddress()

About LoadLibraryEx(), FreeLibrary()
HINSTANCE LoadLibraryEx(
PCTSTR pszDLLPathName,
HANDLE hFile,
DWORD dwFlags);
BOOL FreeLibrary(HINSTANCE hinstDll);
1. They maintain a DLL's usage count on a per-process basis.
2. Assume that, thread a and thread b are in a same process. First, thread a calls LoadLibraryEx, the system maps the DLL's file image into the calling process's address space and sets the DLL's usage count to 1. And then, when thread b calls LoadLibraryEx to load the same DLL file image, the system simply increments the usage count instead of the mapping.
3. Assume that, there are two processes, process A and process B. If process A calls LoadLibraryEx, the system maps the DLL's file image into the calling process's address space and sets the DLL's usage count to 1. And process B calls the same function result in the same behavior as process A.
4. I think the DLL's usage count is only for process, not thread, that is to say, only process has this usage count.

Note: What are the differences between FreeLibraryAndExitThread() and FreeLibrary()?

About GetProcAddress()
FARPROC GetProcAddress(
HINSTANCE hinstDll,
PCSTR pszSymbolName);
1. Notice that the pszSymbolName parameter is prototyped as a PCSTR, as opposed to a PCTSTR. This means that the GetProcAddress function accepts only ANSI strings.
2. If you pass an ordinal number that hasn't been assigned to any of the exported functions, GetProcAddress might return a non-NULL value. So, we can access the non-exported functions in DLLs?

1 comment:

Anonymous said...

a very useful article for me!