Subject: [patch] dlfcn.cc
From: "Jong B. Lee" <[email-masked]>
To: <[email-masked]>
Cc: <[email-masked]>
Date: Thu, 1 Nov 2001 21:32:27 +0900

Dear cygwin developers.

Cygwin dl functions (dlopen, dysym, ...) fail when they handle null module or the current module.

I made a little hack to <dlfcn.cc>  using gmodule sources in the gnome cvs repository.

With this patch, the gmodule (in glib-1.2.10, the stable version for now) works good like xNIX style. (After being dllized)
(Using gmodule-dl.c NOT gmodule-win32.c)

Attached file is the patched source.
Following is a diff result.

Jong B. Lee
===============================================
16,17d15
< #include <windows.h>
< #include <tlhelp32.h>
30,137d27
< void *
< find_in_any_module_using_toolhelp (const char *symbol_name)
< {
<   typedef HANDLE (WINAPI *PFNCREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
<   static PFNCREATETOOLHELP32SNAPSHOT pfnCreateToolhelp32Snapshot = NULL;
< 
<   typedef BOOL (WINAPI *PFNMODULE32FIRST)(HANDLE, MODULEENTRY32*);
<   static PFNMODULE32FIRST pfnModule32First= NULL;
< 
<   typedef BOOL (WINAPI *PFNMODULE32NEXT)(HANDLE, MODULEENTRY32*);
<   static PFNMODULE32NEXT pfnModule32Next = NULL;
< 
<   static HMODULE kernel32;
< 
<   HANDLE snapshot; 
<   MODULEENTRY32 me32;
< 
<   void * p;
< 
<   if (!pfnCreateToolhelp32Snapshot || !pfnModule32First || !pfnModule32Next)
<     {
<       if (!kernel32)
<  if (!(kernel32 = GetModuleHandle ("kernel32.dll")))
<    return NULL;
< 
<       if (!(pfnCreateToolhelp32Snapshot = (PFNCREATETOOLHELP32SNAPSHOT) GetProcAddress (kernel32, "CreateToolhelp32Snapshot"))
<    || !(pfnModule32First = (PFNMODULE32FIRST) GetProcAddress (kernel32, "Module32First"))
<    || !(pfnModule32Next = (PFNMODULE32NEXT) GetProcAddress (kernel32, "Module32Next")))
<  return NULL;
<     }
< 
<   if ((snapshot = (*pfnCreateToolhelp32Snapshot) (TH32CS_SNAPMODULE, 0)) == (HANDLE) -1)
<     return NULL;
< 
<   me32.dwSize = sizeof (me32);
<   p = NULL;
<   if ((*pfnModule32First) (snapshot, &me32))
<     {
<       do {
<  if ((p = GetProcAddress (me32.hModule, symbol_name)) != NULL)
<    break;
<       } while ((*pfnModule32Next) (snapshot, &me32));
<     }
< 
<   CloseHandle (snapshot);
< 
<   return p;
< }
< 
< void *
< find_in_any_module_using_psapi (const char *symbol_name)
< {
<   static HMODULE psapi = NULL;
< 
<   typedef BOOL (WINAPI *PFNENUMPROCESSMODULES) (HANDLE, HMODULE *, DWORD, LPDWORD) ;
<   static PFNENUMPROCESSMODULES pfnEnumProcessModules = NULL;
< 
<   HMODULE *modules;
<   HMODULE dummy;
<   unsigned i, size;
<   DWORD needed;
<   
<   void * p;
< 
<   if (!pfnEnumProcessModules)
<     {
<       if (!psapi)
<  if ((psapi = LoadLibrary ("psapi.dll")) == NULL)
<    return NULL;
< 
<       if (!(pfnEnumProcessModules = (PFNENUMPROCESSMODULES) GetProcAddress (psapi, "EnumProcessModules")))
<  return NULL;
<     }
< 
<   if (!(*pfnEnumProcessModules) (GetCurrentProcess (), &dummy,
<      sizeof (HMODULE), &needed))
<     return NULL;
< 
<   size = needed + 10 * sizeof (HMODULE);
<   modules = (HINSTANCE__ **) (unsigned long) (size);
< 
<   if (!(*pfnEnumProcessModules) (GetCurrentProcess (), modules,
<      size, &needed)
<       || needed > size)
<     {
<       return NULL;
<     }
<   
<   p = NULL;
<   for (i = 0; i < needed / sizeof (HMODULE); i++)
<     if ((p = GetProcAddress (modules[i], symbol_name)) != NULL)
<       break;
< 
<   return p;
< }
< 
< void *
< find_in_any_module (const char *symbol_name)
< {
<   void * result;
< 
<   if ((result = find_in_any_module_using_toolhelp (symbol_name)) == NULL
<       && (result = find_in_any_module_using_psapi (symbol_name)) == NULL)
<     return NULL;
<   else
<     return result;
< }
< 
195,197d84
< static int dummy;
< void *null_module_handle = &dummy; /* null module handler */
< 
204c91
<   
---
> 
206,209c93
<     {
<       ret = (void *) GetModuleHandle (NULL); /* handle for the current module */
<       null_module_handle = ret;
<     }
---
>     ret = (void *) GetModuleHandle (NULL); /* handle for the current module */
237,242d120
< 
<   if (ret == NULL && handle == null_module_handle)
<     {
<       ret = find_in_any_module (name);
<     }
===================================================================

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/