Re: Autoconfiscation patch for GTK+ on Win32



Owen Taylor writes:
 > Perhaps we should have something like:
 > 
 >  G_WIN32_DEFINE_DLL_NAME (static, dll_name)
 > 
 > This seems like a lot of magic code to cut-and-paste into multiple
 > places. 

What about this in glib/gutils.h:

/*
 * On Windows, this macro defines a DllMain function that stores the
 * actual DLL name that the code being compiled will be included in.
 * STATIC should be empty or 'static'. DLL_NAME is the name of a
 * pointer to the char array where the DLL name will be stored. If
 * this is used, you must also include <windows.h>. If you need a more complex
 * DLL entry point function, you cannot use this.
 *
 * On non-Windows platforms, expands to nothing.
 */

#ifndef G_PLATFORM_WIN32
# define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
#else
# define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)			   \
static char *dll_name;							   \
									   \
BOOL WINAPI								   \
DllMain (HINSTANCE hinstDLL,						   \
	 DWORD     fdwReason,						   \
	 LPVOID    lpvReserved)						   \
{									   \
  char bfr[1000];							   \
  switch (fdwReason)							   \
    {									   \
    case DLL_PROCESS_ATTACH:						   \
      GetModuleFileName ((HMODULE) hinstDLL, bfr, sizeof (bfr));	   \
      dll_name = g_path_get_basename (bfr);				   \
      break;								   \
    }									   \
									   \
  return TRUE;								   \
}
#endif /* G_PLATFORM_WIN32 */

/*
 * Expands to a function called _get_##suffix that on non-Windows
 * returns hardcoded_value. On Windows, deduce a pathname from the
 * package, module_name and subdir, as described for
 * g_win32_get_package_installation_subdirectory(). If the code being
 * compiled goes into a DLL, module_name should be the dll_name passed
 * to G_WIN32_DLLMAIN_FOR_DLL_NAME. If the code is for a .EXE, use
 * NULL.
 *
 * Elsewhere in your code, wherever you would be tempted to write the
 * name of a compile-time path macro, like FOOBAR_LIBDIR, instead use
 * _get_libdir(), for instance.
 */

#ifndef G_PLATFORM_WIN32
# define G_HARDCODED_PATH_WRAPPER(hardcoded_value, package, suffix, module_name, subdir) \
const gchar *			\
_get_##suffix (void)		\
{				\
  return hardcoded_value;	\
}
#else
# define G_HARDCODED_PATH_WRAPPER(hardcoded_value, package, suffix, module_name, subdir) \
const gchar *								\
_get_##suffix (void)							\
{									\
  static char *cached_##suffix = NULL;					\
  if (cached_##suffix == NULL)						\
    cached_##suffix = g_win32_get_package_installation_subdirectory	\
      (package, module_name, subdir);					\
									\
  return cached_##suffix;						\
}
#endif /* G_PLATFORM_WIN32 */

In gtkmain.c, one would then have:

G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)

G_HARDCODED_PATH_WRAPPER (GTK_LIBDIR, GETTEXT_PACKAGE, libdir, dll_name, "lib")
G_HARDCODED_PATH_WRAPPER (GTK_LOCALEDIR, GETTEXT_PACKAGE, localedir, dll_name, "lib/locale")
G_HARDCODED_PATH_WRAPPER (GTK_SYSCONFDIR, GETTEXT_PACKAGE, sysconfdir, dll_name, "etc")
G_HARDCODED_PATH_WRAPPER (GTK_DATA_PREFIX, GETTEXT_PACKAGE, data_prefix, dll_name, "")

and code like

    default_dir = g_build_filename (_get_libdir (), "gtk-2.0", "modules", NULL);

Is this any cleaner? I am not sure myself...

--tml





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]