Jean-Marc,

attached are two patches that almost complete our support for old versions
of Windows. OK?

Two small issues remain:
1. A bug in Boost.Filesystem's exists(path) function that is triggered on
Win98 when attempting to access a drive that doesn't exist. One line
work-around to follow. Interesting that the problem doesn't seem to exist
on Win95...
2. Luis has reported some problems with paths on Win95.
Things like "C:\Foo\C:Foo\Bar".  I suspect some wackiness with MS's
GetLongPathName emulation, but I think that it'll be easier to resolve
once this patch is in. Doesn't affect Windows versions > WIn95.

Thereafter, I think we can punt all remaining problems with these flavours
of Windows over to Qt/WinFree. They should, eventually, just dissapear
when we migrate to Qt4.

-- 
Angus
Index: config/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/config/ChangeLog,v
retrieving revision 1.86.2.50
diff -u -p -r1.86.2.50 ChangeLog
--- config/ChangeLog	29 Sep 2005 13:26:41 -0000	1.86.2.50
+++ config/ChangeLog	30 Sep 2005 11:17:54 -0000
@@ -1,3 +1,8 @@
+2005-09-30  Angus Leeming  <[EMAIL PROTECTED]>
+
+	* configure.ac: 
+	* configure.in: check for NewAPIs.h.
+
 2005-09-29  Angus Leeming  <[EMAIL PROTECTED]>
 
 	* lyxinclude213.m4:
Index: config/configure.ac
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/config/Attic/configure.ac,v
retrieving revision 1.24.2.36
diff -u -p -r1.24.2.36 configure.ac
--- config/configure.ac	29 Sep 2005 13:26:41 -0000	1.24.2.36
+++ config/configure.ac	30 Sep 2005 11:17:54 -0000
@@ -239,7 +239,7 @@ AC_LANG_POP(C)
 # some standard header files
 AC_HEADER_DIRENT
 AC_HEADER_MAJOR
-AC_CHECK_HEADERS(sys/time.h sys/types.h sys/select.h strings.h locale.h)
+AC_CHECK_HEADERS(sys/time.h sys/types.h sys/select.h strings.h locale.h NewAPIs.h)
 
 # some standard structures
 AC_HEADER_STAT
Index: config/configure.in
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/config/Attic/configure.in,v
retrieving revision 1.17.2.34
diff -u -p -r1.17.2.34 configure.in
--- config/configure.in	29 Sep 2005 13:26:41 -0000	1.17.2.34
+++ config/configure.in	30 Sep 2005 11:17:54 -0000
@@ -242,7 +242,7 @@ AC_LANG_CPLUSPLUS
 # some standard header files
 AC_HEADER_DIRENT
 AC_HEADER_MAJOR
-AC_CHECK_HEADERS(sys/time.h sys/types.h sys/select.h strings.h locale.h)
+AC_CHECK_HEADERS(sys/time.h sys/types.h sys/select.h strings.h locale.h NewAPIs.h)
 
 # some standard structures
 AC_HEADER_STAT
Index: src/support/Makefile.am
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/Makefile.am,v
retrieving revision 1.53.2.4
diff -u -p -r1.53.2.4 Makefile.am
--- src/support/Makefile.am	29 Sep 2005 13:26:46 -0000	1.53.2.4
+++ src/support/Makefile.am	30 Sep 2005 11:18:02 -0000
@@ -5,7 +5,7 @@ noinst_LTLIBRARIES = libsupport.la
 INCLUDES = -I$(srcdir)/.. $(BOOST_INCLUDES)
 
 EXTRA_DIST = lyxstring.C lyxstring.h \
-	     os_cygwin.C os_unix.C os_win32.C os_os2.C
+	     os_cygwin.C os_unix.C os_win32.C os_win32.h os_os2.C
 
 if USE_LYXSTRING
 LYXSTRING = lyxstring.C lyxstring.h
Index: src/support/os_win32.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/os_win32.C,v
retrieving revision 1.8.2.16
diff -u -p -r1.8.2.16 os_win32.C
--- src/support/os_win32.C	29 Apr 2005 08:42:22 -0000	1.8.2.16
+++ src/support/os_win32.C	30 Sep 2005 11:18:02 -0000
@@ -3,15 +3,58 @@
 // Various OS specific functions
 #include <config.h>
 
+/* The GetLongPathNameA function declaration in
+ * <winbase.h> under MinGW or Cygwin is protected
+ * by the WINVER macro which is defined in <windef.h>
+ *
+ * SHGFP_TYPE_CURRENT is defined in <shlobj.h> for __W32API_VERSION >= 3.2
+ * where it is protected by _WIN32_IE.
+ * It is missing in earlier versions of the MinGW w32api headers.
+ */
+#if defined(__MINGW32__)  || defined(__CYGWIN__) || defined(__CYGWIN32__)
+# include <w32api.h>
+# define WINVER 0x0500
+# define _WIN32_IE 0x0500
+#endif
+
 #include "os.h"
+#include "support/os_win32.h"
 #include "support/filetools.h"
+#include "support/LAssert.h"
 #include "support/lstrings.h"
 #include "debug.h"
 
+#include <cstdlib>
+#include <vector>
+
 #include <windows.h>
+
+/* The GetLongPathName macro may be defined on the compiling machine,
+ * but we must use a bit of trickery if the resulting executable is
+ * to run on a Win95 machine.
+ * Fortunately, Microsoft provide the trickery. All we need is the
+ * NewAPIs.h header file, available for download from Microsoft as
+ * part of the Platform SDK.
+ */
+#if defined (HAVE_NEWAPIS_H)
+# define WANT_GETLONGPATHNAME_WRAPPER 1
+# define COMPILE_NEWAPIS_STUBS
+# include <NewAPIs.h>
+# undef COMPILE_NEWAPIS_STUBS
+# undef WANT_GETLONGPATHNAME_WRAPPER
+#endif
+
 #include <io.h>
 #include <direct.h> // _getdrive
+#include <shlobj.h>  // SHGetFolderPath
 
+// Needed by older versions of MinGW.
+#if defined (__W32API_MAJOR_VERSION) && \
+    defined (__W32API_MINOR_VERSION) && \
+    (__W32API_MAJOR_VERSION < 3 || \
+     __W32API_MAJOR_VERSION == 3 && __W32API_MINOR_VERSION  < 2)
+# define SHGFP_TYPE_CURRENT 0
+#endif
 
 string const os::nulldev_ = "nul";
 os::shell_type os::shell_ = os::CMD_EXE;
@@ -136,9 +179,30 @@ string os::external_path(string const & 
 }
 
 
+namespace {
+
+string const get_long_path(string const & short_path)
+{
+	std::vector<char> long_path(PATH_MAX);
+	DWORD result = GetLongPathName(short_path.c_str(),
+				       &long_path[0], long_path.size());
+
+	if (result > long_path.size()) {
+		long_path.resize(result);
+		result = GetLongPathName(short_path.c_str(),
+					 &long_path[0], long_path.size());
+		lyx::Assert(result <= long_path.size());
+	}
+
+	return (result == 0) ? short_path : &long_path[0];
+}
+
+} // namespace anon
+
+
 string os::internal_path(string const & p)
 {
-	return subst(p, "\\", "/");
+	return subst(get_long_path(p), "\\", "/");
 }
 
 
@@ -183,3 +247,68 @@ char os::path_separator()
 
 void os::cygwin_path_fix(bool)
 {}
+
+
+namespace {
+
+void bail_out()
+{
+#ifndef CXX_GLOBAL_CSTD
+	using std::exit;
+#endif
+	exit(1);
+}
+
+} // namespace anon 
+
+
+GetFolderPath::GetFolderPath()
+	: folder_module_(0),
+	  folder_path_func_(0)
+{
+	folder_module_ = LoadLibrary("shfolder.dll");
+	if (!folder_module_) {
+		lyxerr << "Unable to load shfolder.dll\nPlease install."
+		       << std::endl;
+		bail_out();
+	}
+
+	folder_path_func_ = reinterpret_cast<function_pointer>(::GetProcAddress(folder_module_, "SHGetFolderPathA"));
+	if (folder_path_func_ == 0) {
+		lyxerr << "Unable to find SHGetFolderPathA in shfolder.dll\n"
+		          "Don't know how to proceed. Sorry."
+		       << std::endl;
+		bail_out();
+	}
+}
+
+
+GetFolderPath::~GetFolderPath()
+{
+	if (folder_module_)
+		FreeLibrary(folder_module_);
+}
+
+
+// Given a folder ID, returns the folder name (in unix-style format).
+// Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
+string const GetFolderPath::operator()(folder_id _id) const
+{
+	char folder_path[PATH_MAX];
+
+	int id = 0;
+	switch (_id) {
+	case PERSONAL:
+		id = CSIDL_PERSONAL;
+		break;
+	case APPDATA:
+		id = CSIDL_APPDATA;
+		break;
+	default:
+		lyx::Assert(false);
+	}
+	HRESULT const result = (folder_path_func_)(0, id, 0,
+						   SHGFP_TYPE_CURRENT,
+						   folder_path);
+	return (result == 0) ? os::internal_path(folder_path) : string();
+}
Index: src/support/os_win32.h
===================================================================
RCS file: src/support/os_win32.h
diff -N src/support/os_win32.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/support/os_win32.h	30 Sep 2005 11:18:02 -0000
@@ -0,0 +1,76 @@
+// -*- C++ -*-
+/**
+ * \file os_win32.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
+ *
+ * These classes should be used only on Windows machines.
+ */
+
+#ifndef OS_WIN32_H
+#define OS_WIN32_H
+
+#include "LString.h"
+
+#if !defined(_WIN32)
+# error os_win32.h should be compiled only under Windows.
+#endif
+
+/* The GetLongPathNameA function declaration in
+ * winbase.h under MinGW or Cygwin is protected
+ * by the WINVER macro which is defined in windef.h
+ *
+ * We need to #include this file to make available the
+ * DWORD, HMODULE et al. typedefs, so check WINVER now.
+ *
+ * Note: __CYGWIN__ can be defined here if building in _WIN32 mode.
+ */
+#if defined(__MINGW32__)  || defined(__CYGWIN__) || defined(__CYGWIN32__)
+# if !defined(WINVER) || WINVER < 0x0500
+#  error WINVER must be >= 0x0500
+# endif
+#endif
+
+#include <windef.h>
+
+
+/** Win98 and earlier don't have SHGetFolderPath in shell32.dll.
+ *  Microsoft recommend that we load shfolder.dll at run time and
+ *  access the function through that.
+ *
+ *  shfolder.dll is loaded dynamically in the constructor. If loading
+ *  fails or if the .dll is found not to contain SHGetFolderPathA then
+ *  the program exits immediately. Otherwise, the .dll is unloaded in
+ *  the destructor
+ *
+ *  The class makes SHGetFolderPath available through its function operator.
+ *  It will work on all versions of Windows >= Win95.
+ */
+class GetFolderPath {
+public:
+	enum folder_id {
+		/// CSIDL_PERSONAL
+		PERSONAL,
+		/// CSIDL_APPDATA
+		APPDATA
+	};
+
+	GetFolderPath();
+	~GetFolderPath();
+
+	/** Wrapper for SHGetFolderPathA, returning
+	 *  the path asscociated with @c id.
+	 */
+	string const operator()(folder_id id) const;
+private:
+	typedef HRESULT (__stdcall * function_pointer)(HWND, int, HANDLE, DWORD, LPCSTR);
+
+	HMODULE folder_module_;
+	function_pointer folder_path_func_;
+};
+
+#endif // OS_WIN32_H
Index: src/support/package.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/Attic/package.C,v
retrieving revision 1.1.2.6
diff -u -p -r1.1.2.6 package.C
--- src/support/package.C	15 Jul 2005 15:57:10 -0000	1.1.2.6
+++ src/support/package.C	30 Sep 2005 11:18:03 -0000
@@ -22,6 +22,10 @@
 #include "support/lstrings.h"
 #include "support/os.h"
 
+#if defined (USE_WINDOWS_PACKAGING)
+# include "support/os_win32.h"
+#endif
+
 #include <boost/tuple/tuple.hpp>
 
 #include <list>
@@ -35,32 +39,7 @@
 
 
 #if defined (USE_WINDOWS_PACKAGING)
-
-/*
- * MinGW's version of winver.h contains this comment:
- *
- * If you need Win32 API features newer the Win95 and WinNT then you must
- * define WINVER before including windows.h or any other method of including
- * the windef.h header.
- *
- * GetLongPathNameA requires WINVER == 0x0500.
- *
- * It doesn't matter if the Windows version is older than this because the
- * function will compile but will fail at run time. See
- * http://msdn.microsoft.com/library/en-us/mslu/winprog/microsoft_layer_for_unicode_apis_with_limited_support.asp
- */
-# if defined(__MINGW32__)
-#  define WINVER 0x0500
-# endif
-
 # include <windows.h>
-# include <shlobj.h>  // SHGetFolderPath
-
-  // Needed for MinGW:
-# ifndef SHGFP_TYPE_CURRENT
-#  define SHGFP_TYPE_CURRENT 0
-# endif
-
 #elif defined (USE_MACOSX_PACKAGING)
 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
 #endif
@@ -187,20 +166,6 @@ string const relative_locale_dir();
 string const relative_system_support_dir();
 
 
-#if defined (USE_WINDOWS_PACKAGING)
-// Given a folder ID, returns the folder name (in unix-style format).
-// Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
-string const win32_folder_path(int folder_id)
-{
-	char folder_path[PATH_MAX + 1];
-	if (SUCCEEDED(SHGetFolderPath(0, folder_id, 0,
-				      SHGFP_TYPE_CURRENT, folder_path)))
-		return os::internal_path(folder_path);
-	return string();
-}
-#endif
-
-
 std::pair<string, string> const get_build_dirs(string const & abs_binary)
 {
 	string const check_text = "Checking whether LyX is run in place...";
@@ -259,7 +224,8 @@ string const get_document_dir(string con
 {
 #if defined (USE_WINDOWS_PACKAGING)
 	(void)home_dir; // Silence warning about unused variable.
-	return win32_folder_path(CSIDL_PERSONAL);
+	GetFolderPath win32_folder_path;
+	return win32_folder_path(GetFolderPath::PERSONAL);
 #else // Posix-like.
 	return home_dir;
 #endif
@@ -317,7 +283,6 @@ string const get_temp_dir()
 	// Typical example: C:/TEMP/.
 	char path[PATH_MAX + 1];
 	GetTempPath(PATH_MAX, path);
-	GetLongPathName(path, path, PATH_MAX + 1);
 	return os::internal_path(path);
 #else // Posix-like.
 	return "/tmp";
@@ -325,8 +290,6 @@ string const get_temp_dir()
 }
 
 
-// If we use a 'lazy' lyxerr in the hope of setting the locale before
-// printing any messages, then we should ensure that it is flushed first.
 void bail_out()
 {
 #ifndef CXX_GLOBAL_CSTD
@@ -590,7 +553,8 @@ string const get_default_user_support_di
 	(void)home_dir; // Silence warning about unused variable.
 
 	string const user_dir = (string(PACKAGE) == "lyx") ? "LyX" : PACKAGE;
-	return AddPath(win32_folder_path(CSIDL_APPDATA), user_dir);
+	GetFolderPath win32_folder_path;
+	return AddPath(win32_folder_path(GetFolderPath::APPDATA), user_dir);
 
 #elif defined (USE_MACOSX_PACKAGING)
 	(void)home_dir; // Silence warning about unused variable.
Index: src/support/Makefile.am
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/Makefile.am,v
retrieving revision 1.96
diff -u -p -r1.96 Makefile.am
--- src/support/Makefile.am	29 Sep 2005 13:25:36 -0000	1.96
+++ src/support/Makefile.am	30 Sep 2005 10:48:13 -0000
@@ -4,7 +4,8 @@ SUBDIRS = . tests
 
 CLEANFILES += $(BUILT_SOURCES)
 
-EXTRA_DIST = package.C.in pch.h os_cygwin.C os_os2.C os_unix.C os_win32.C
+EXTRA_DIST = package.C.in pch.h \
+	os_cygwin.C os_os2.C os_unix.C os_win32.C os_win32.h
 
 noinst_LTLIBRARIES = libsupport.la
 
Index: src/support/os_win32.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/os_win32.C,v
retrieving revision 1.29
diff -u -p -r1.29 os_win32.C
--- src/support/os_win32.C	29 Apr 2005 08:42:02 -0000	1.29
+++ src/support/os_win32.C	30 Sep 2005 10:48:14 -0000
@@ -14,14 +14,65 @@
 
 #include <config.h>
 
+/* The GetLongPathNameA function declaration in
+ * <winbase.h> under MinGW or Cygwin is protected
+ * by the WINVER macro which is defined in <windef.h>
+ *
+ * SHGFP_TYPE_CURRENT is defined in <shlobj.h> for __W32API_VERSION >= 3.2
+ * where it is protected by _WIN32_IE.
+ * It is missing on earlier versions of the MinGW w32api headers.
+ */
+#if defined(__MINGW32__)  || defined(__CYGWIN__) || defined(__CYGWIN32__)
+# include <w32api.h>
+# define WINVER 0x0500
+# define _WIN32_IE 0x0500
+#endif
+
 #include "support/os.h"
+#include "support/os_win32.h"
 #include "support/lstrings.h"
 
 #include "debug.h"
 
+#include <boost/assert.hpp>
+
+#include <cstdlib>
+#include <vector>
+
+#include <string>
+
 #include <windows.h>
+
+/* The GetLongPathName macro may be defined on the compiling machine,
+ * but we must use a bit of trickery if the resulting executable is
+ * to run on a Win95 machine.
+ * Fortunately, Microsoft provide the trickery. All we need is the
+ * NewAPIs.h header file, available for download from Microsoft as
+ * part of the Platform SDK.
+ */
+#if defined (HAVE_NEWAPIS_H)
+// This should be defined already to keep Boost.Filesystem happy.
+# if !defined (WANT_GETFILEATTRIBUTESEX_WRAPPER)
+#   error Expected WANT_GETFILEATTRIBUTESEX_WRAPPER to be defined!
+# endif
+# define WANT_GETLONGPATHNAME_WRAPPER 1
+# define COMPILE_NEWAPIS_STUBS
+# include <NewAPIs.h>
+# undef COMPILE_NEWAPIS_STUBS
+# undef WANT_GETLONGPATHNAME_WRAPPER
+#endif
+
+// Needed by older versions of MinGW.
+#if defined (__W32API_MAJOR_VERSION) && \
+    defined (__W32API_MINOR_VERSION) && \
+    (__W32API_MAJOR_VERSION < 3 || \
+     __W32API_MAJOR_VERSION == 3 && __W32API_MINOR_VERSION  < 2)
+# define SHGFP_TYPE_CURRENT 0
+#endif
+
 #include <io.h>
 #include <direct.h> // _getdrive
+#include <shlobj.h>  // SHGetFolderPath
 
 using std::endl;
 using std::string;
@@ -31,7 +82,7 @@ namespace lyx {
 namespace support {
 namespace os {
 
-void os::init(int /* argc */, char * argv[])
+void init(int /* argc */, char * argv[])
 {
 	/* Note from Angus, 17 Jan 2005:
 	 *
@@ -153,13 +204,30 @@ string external_path(string const & p)
 }
 
 
-// (Claus H.) Parsing the latex log file in an Win32 environment all
-// files are mentioned in Win32/DOS syntax. Because LyX uses the dep file
-// entries to check if any file has been changed we must retranslate
-// the Win32/DOS pathnames into Cygwin pathnames.
+namespace {
+
+string const get_long_path(string const & short_path)
+{
+	std::vector<char> long_path(PATH_MAX);
+	DWORD result = GetLongPathName(short_path.c_str(),
+				       &long_path[0], long_path.size());
+
+	if (result > long_path.size()) {
+		long_path.resize(result);
+		result = GetLongPathName(short_path.c_str(),
+					 &long_path[0], long_path.size());
+		BOOST_ASSERT(result <= long_path.size());
+	}
+
+	return (result == 0) ? short_path : &long_path[0];
+}
+
+} // namespace anon
+
+
 string internal_path(string const & p)
 {
-	return subst(p, "\\", "/");
+	return subst(get_long_path(p), "\\", "/");
 }
 
 
@@ -210,6 +278,71 @@ char path_separator()
 
 void cygwin_path_fix(bool)
 {}
+
+
+namespace {
+
+void bail_out()
+{
+#ifndef CXX_GLOBAL_CSTD
+	using std::exit;
+#endif
+	exit(1);
+}
+
+} // namespace anon 
+
+
+GetFolderPath::GetFolderPath()
+	: folder_module_(0),
+	  folder_path_func_(0)
+{
+	folder_module_ = LoadLibrary("shfolder.dll");
+	if (!folder_module_) {
+		lyxerr << "Unable to load shfolder.dll\nPlease install."
+		       << std::endl;
+		bail_out();
+	}
+
+	folder_path_func_ = reinterpret_cast<function_pointer>(::GetProcAddress(folder_module_, "SHGetFolderPathA"));
+	if (folder_path_func_ == 0) {
+		lyxerr << "Unable to find SHGetFolderPathA in shfolder.dll\n"
+		          "Don't know how to proceed. Sorry."
+		       << std::endl;
+		bail_out();
+	}
+}
+
+
+GetFolderPath::~GetFolderPath()
+{
+	if (folder_module_)
+		FreeLibrary(folder_module_);
+}
+
+
+// Given a folder ID, returns the folder name (in unix-style format).
+// Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
+string const GetFolderPath::operator()(folder_id _id) const
+{
+	char folder_path[PATH_MAX];
+
+	int id = 0;
+	switch (_id) {
+	case PERSONAL:
+		id = CSIDL_PERSONAL;
+		break;
+	case APPDATA:
+		id = CSIDL_APPDATA;
+		break;
+	default:
+		BOOST_ASSERT(false);
+	}
+	HRESULT const result = (folder_path_func_)(0, id, 0,
+						   SHGFP_TYPE_CURRENT,
+						   folder_path);
+	return (result == 0) ? os::internal_path(folder_path) : string();
+}
 
 } // namespace os
 } // namespace support
Index: src/support/os_win32.h
===================================================================
RCS file: src/support/os_win32.h
diff -N src/support/os_win32.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/support/os_win32.h	30 Sep 2005 10:48:14 -0000
@@ -0,0 +1,84 @@
+// -*- C++ -*-
+/**
+ * \file os_win32.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
+ *
+ * These classes should be used only on Windows machines.
+ */
+
+#ifndef OS_WIN32_H
+#define OS_WIN32_H
+
+#include <string>
+
+#if !defined(_WIN32)
+# error os_win32.h should be compiled only under Windows.
+#endif
+
+/* The GetLongPathNameA function declaration in
+ * winbase.h under MinGW or Cygwin is protected
+ * by the WINVER macro which is defined in windef.h
+ *
+ * We need to #include this file to make available the
+ * DWORD, HMODULE et al. typedefs, so check that WINVER.
+ *
+ * Note: __CYGWIN__ can be defined here if building in _WIN32 mode.
+ */
+#if defined(__MINGW32__)  || defined(__CYGWIN__) || defined(__CYGWIN32__)
+# if !defined(WINVER) || WINVER < 0x0500
+#  error WINVER must be >= 0x0500
+# endif
+#endif
+
+#include <windef.h>
+
+
+namespace lyx {
+namespace support {
+namespace os {
+
+/** Win98 and earlier don't have SHGetFolderPath in shell32.dll.
+ *  Microsoft recommend that we load shfolder.dll at run time and
+ *  access the function through that.
+ *
+ *  shfolder.dll is loaded dynamically in the constructor. If loading
+ *  fails or if the .dll is found not to contain SHGetFolderPathA then
+ *  the program exits immediately. Otherwise, the .dll is unloaded in
+ *  the destructor
+ *
+ *  The class makes SHGetFolderPath available through its function operator.
+ *  It will work on all versions of Windows >= Win95.
+ */
+class GetFolderPath {
+public:
+	enum folder_id {
+		/// CSIDL_PERSONAL
+		PERSONAL,
+		/// CSIDL_APPDATA
+		APPDATA
+	};
+
+	GetFolderPath();
+	~GetFolderPath();
+
+	/** Wrapper for SHGetFolderPathA, returning
+	 *  the path asscociated with @c id.
+	 */
+	std::string const operator()(folder_id id) const;
+private:
+	typedef HRESULT (__stdcall * function_pointer)(HWND, int, HANDLE, DWORD, LPCSTR);
+
+	HMODULE folder_module_;
+	function_pointer folder_path_func_;
+};
+
+} // namespace os
+} // namespace support
+} // namespace lyx
+
+#endif // OS_WIN32_H
Index: src/support/package.C.in
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/package.C.in,v
retrieving revision 1.12
diff -u -p -r1.12 package.C.in
--- src/support/package.C.in	17 Jul 2005 01:59:18 -0000	1.12
+++ src/support/package.C.in	30 Sep 2005 10:48:15 -0000
@@ -24,7 +24,10 @@
 #include "support/lstrings.h"
 #include "support/os.h"
 
-#include <boost/assert.hpp>
+#if defined (USE_WINDOWS_PACKAGING)
+# include "support/os_win32.h"
+#endif
+
 #include <boost/filesystem/operations.hpp>
 #include <boost/tuple/tuple.hpp>
 
@@ -38,32 +41,7 @@
 #endif
 
 #if defined (USE_WINDOWS_PACKAGING)
-
-/*
- * MinGW's version of winver.h contains this comment:
- *
- * If you need Win32 API features newer the Win95 and WinNT then you must
- * define WINVER before including windows.h or any other method of including
- * the windef.h header.
- *
- * GetLongPathNameA requires WINVER == 0x0500.
- *
- * It doesn't matter if the Windows version is older than this because the
- * function will compile but will fail at run time. See
- * http://msdn.microsoft.com/library/en-us/mslu/winprog/microsoft_layer_for_unicode_apis_with_limited_support.asp
- */
-# if defined(__MINGW32__)
-#  define WINVER 0x0500
-# endif
-
 # include <windows.h>
-# include <shlobj.h>  // SHGetFolderPath
-
-  // Needed for MinGW:
-# ifndef SHGFP_TYPE_CURRENT
-#  define SHGFP_TYPE_CURRENT 0
-# endif
-
 #elif defined (USE_MACOSX_PACKAGING)
 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
 #endif
@@ -234,20 +212,6 @@ string const relative_locale_dir();
 string const relative_system_support_dir();
 
 
-#if defined (USE_WINDOWS_PACKAGING)
-// Given a folder ID, returns the folder name (in unix-style format).
-// Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
-string const win32_folder_path(int folder_id)
-{
-	char folder_path[PATH_MAX + 1];
-	if (SUCCEEDED(SHGetFolderPath(0, folder_id, 0,
-				      SHGFP_TYPE_CURRENT, folder_path)))
-		return os::internal_path(folder_path);
-	return string();
-}
-#endif
-
-
 std::string const
 get_build_support_dir(std::string const & binary_dir,
 		      exe_build_dir_to_top_build_dir top_build_dir_location)
@@ -325,7 +289,8 @@ string const get_document_dir(string con
 {
 #if defined (USE_WINDOWS_PACKAGING)
 	(void)home_dir; // Silence warning about unused variable.
-	return win32_folder_path(CSIDL_PERSONAL);
+	os::GetFolderPath win32_folder_path;
+	return win32_folder_path(os::GetFolderPath::PERSONAL);
 #else // Posix-like.
 	return home_dir;
 #endif
@@ -379,9 +344,8 @@ string const get_temp_dir()
 {
 #if defined (USE_WINDOWS_PACKAGING)
 	// Typical example: C:/TEMP/.
-	char path[PATH_MAX + 1];
+	char path[PATH_MAX];
 	GetTempPath(PATH_MAX, path);
-	GetLongPathName(path, path, PATH_MAX + 1);
 	return os::internal_path(path);
 #else // Posix-like.
 	return "/tmp";
@@ -389,8 +353,6 @@ string const get_temp_dir()
 }
 
 
-// If we use a 'lazy' lyxerr in the hope of setting the locale before
-// printing any messages, then we should ensure that it is flushed first.
 void bail_out()
 {
 #ifndef CXX_GLOBAL_CSTD
@@ -631,7 +593,8 @@ string const get_default_user_support_di
 #if defined (USE_WINDOWS_PACKAGING)
 	(void)home_dir; // Silence warning about unused variable.
 
-	return AddPath(win32_folder_path(CSIDL_APPDATA), PACKAGE);
+	os::GetFolderPath win32_folder_path;
+	return AddPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE);
 
 #elif defined (USE_MACOSX_PACKAGING)
 	(void)home_dir; // Silence warning about unused variable.

Reply via email to