This is the mail archive of the cygwin-patches@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[PATCH] dlfcn.cc


Dear list,

With this patch, moule self test seems to work.

Jong B. Lee

===================================================================
Thu Nov  1 20:52:16 2001  Jong B. Lee <jbdoll@kepri.re.kr>

              * dlfcn.cc : Add null module handling functions to dlfcn.cc


Index: winsup/cygwin/dlfcn.cc
===================================================================
--- dlfcn-orig.cc Sun Aug 26 10:41:56 2001
+++ dlfcn.cc Thu Nov  1 20:52:16 2001
@@ -13,6 +13,8 @@ details. */
 #include <stdlib.h>
 #include <unistd.h>
 #include <ctype.h>
+#include <windows.h>
+#include <tlhelp32.h>
 #include "security.h"
 #include "fhandler.h"
 #include "perprocess.h"
@@ -25,6 +27,114 @@ details. */
 #define _dl_error _reent_winsup()->_dl_error
 #define _dl_buffer _reent_winsup()->_dl_buffer
 
+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;
+}
+
 static void __stdcall
 set_dl_error (const char *str)
 {
@@ -82,15 +192,21 @@ get_full_path_of_dll (const char* str, c
   return ret;
 }
 
+static int dummy;
+void *null_module_handle = &dummy; /* null module handler */
+
 void *
 dlopen (const char *name, int)
 {
   SetResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlopen");
 
   void *ret;
-
+  
   if (name == NULL)
-    ret = (void *) GetModuleHandle (NULL); /* handle for the current module */
+    {
+      ret = (void *) GetModuleHandle (NULL); /* handle for the current module */
+      null_module_handle = ret;
+    }
   else
     {
       char buf[MAX_PATH];
@@ -118,6 +234,12 @@ void *
 dlsym (void *handle, const char *name)
 {
   void *ret = (void *) GetProcAddress ((HMODULE) handle, name);
+
+  if (ret == NULL && handle == null_module_handle)
+    {
+      ret = find_in_any_module (name);
+    }
+  
   if (!ret)
     set_dl_error ("dlsym");
   debug_printf ("ret %p", ret);
====================================================

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]