--- _21.3/src/w32.c	2002-07-14 20:00:37.000000000 -0400
+++ 21.3/src/w32.c	2002-11-15 12:50:32.000000000 -0500
@@ -105,6 +105,171 @@
 extern Lisp_Object Vw32_num_mouse_buttons;
 
 
+/*
+  BEGIN: Wrapper functions around OpenProcessToken
+  and other functions in advapi32.dll that are only
+  supported in Windows NT / 2k / XP
+*/
+  /* ** Function pointer typedefs ** */
+typedef BOOL (WINAPI * OpenProcessToken_Proc) (
+    HANDLE ProcessHandle,
+    DWORD DesiredAccess,
+    PHANDLE TokenHandle);
+typedef BOOL (WINAPI * GetTokenInformation_Proc) (
+    HANDLE TokenHandle,
+    TOKEN_INFORMATION_CLASS TokenInformationClass,
+    LPVOID TokenInformation,
+    DWORD TokenInformationLength,
+    PDWORD ReturnLength);
+#ifdef _UNICODE
+const char * const LookupAccountSid_Name = "LookupAccountSidW";
+#else
+const char * const LookupAccountSid_Name = "LookupAccountSidA";
+#endif
+typedef BOOL (WINAPI * LookupAccountSid_Proc) (
+    LPCTSTR lpSystemName,
+    PSID Sid,
+    LPTSTR Name,
+    LPDWORD cbName,
+    LPTSTR DomainName,
+    LPDWORD cbDomainName,
+    PSID_NAME_USE peUse);
+typedef PSID_IDENTIFIER_AUTHORITY (WINAPI * GetSidIdentifierAuthority_Proc) (
+    PSID pSid);
+
+  /* ** A utility function ** */
+static BOOL is_windows_9x ()
+{
+  BOOL b_ret=0;
+  OSVERSIONINFO os_ver;
+  ZeroMemory(&os_ver, sizeof(OSVERSIONINFO));
+  os_ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+  if (GetVersionEx (&os_ver))
+    {
+      b_ret = (os_ver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
+    }
+  return b_ret;
+}
+
+  /* ** The wrapper functions ** */
+
+BOOL WINAPI open_process_token (
+    HANDLE ProcessHandle,
+    DWORD DesiredAccess,
+    PHANDLE TokenHandle)
+{
+  OpenProcessToken_Proc pfn_Open_Process_Token = NULL;
+  HMODULE hm_advapi32 = NULL;
+  if (is_windows_9x () == TRUE)
+    {
+      return FALSE;
+    }
+  hm_advapi32 = LoadLibrary ("Advapi32.dll");
+  pfn_Open_Process_Token =
+    (OpenProcessToken_Proc) GetProcAddress (hm_advapi32, "OpenProcessToken");
+  if (pfn_Open_Process_Token == NULL)
+    {
+      return FALSE;
+    }
+  return (
+      pfn_Open_Process_Token (
+          ProcessHandle,
+          DesiredAccess,
+          TokenHandle)
+      );
+}
+
+BOOL WINAPI get_token_information (
+    HANDLE TokenHandle,
+    TOKEN_INFORMATION_CLASS TokenInformationClass,
+    LPVOID TokenInformation,
+    DWORD TokenInformationLength,
+    PDWORD ReturnLength)
+{
+  GetTokenInformation_Proc pfn_Get_Token_Information = NULL;
+  HMODULE hm_advapi32 = NULL;
+  if (is_windows_9x () == TRUE)
+    {
+      return FALSE;
+    }
+  hm_advapi32 = LoadLibrary ("Advapi32.dll");
+  pfn_Get_Token_Information =
+    (GetTokenInformation_Proc) GetProcAddress (hm_advapi32, "GetTokenInformation");
+  if (pfn_Get_Token_Information == NULL)
+    {
+      return FALSE;
+    }
+  return (
+      pfn_Get_Token_Information (
+          TokenHandle,
+          TokenInformationClass,
+          TokenInformation,
+          TokenInformationLength,
+          ReturnLength)
+      );
+}
+
+BOOL WINAPI lookup_account_sid (
+    LPCTSTR lpSystemName,
+    PSID Sid,
+    LPTSTR Name,
+    LPDWORD cbName,
+    LPTSTR DomainName,
+    LPDWORD cbDomainName,
+    PSID_NAME_USE peUse)
+{
+  LookupAccountSid_Proc pfn_Lookup_Account_Sid = NULL;
+  HMODULE hm_advapi32 = NULL;
+  if (is_windows_9x () == TRUE)
+    {
+      return FALSE;
+    }
+  hm_advapi32 = LoadLibrary ("Advapi32.dll");
+  pfn_Lookup_Account_Sid =
+    (LookupAccountSid_Proc) GetProcAddress (hm_advapi32, LookupAccountSid_Name);
+  if (pfn_Lookup_Account_Sid == NULL)
+    {
+      return FALSE;
+    }
+  return (
+      pfn_Lookup_Account_Sid (
+          lpSystemName,
+          Sid,
+          Name,
+          cbName,
+          DomainName,
+          cbDomainName,
+          peUse)
+      );
+}
+
+PSID_IDENTIFIER_AUTHORITY WINAPI get_sid_identifier_authority (
+    PSID pSid)
+{
+  GetSidIdentifierAuthority_Proc pfn_Get_Sid_Identifier_Authority = NULL;
+  HMODULE hm_advapi32 = NULL;
+  if (is_windows_9x () == TRUE)
+    {
+      return NULL;
+    }
+  hm_advapi32 = LoadLibrary ("Advapi32.dll");
+  pfn_Get_Sid_Identifier_Authority =
+    (GetSidIdentifierAuthority_Proc) GetProcAddress (
+        hm_advapi32, "GetSidIdentifierAuthority");
+  if (pfn_Get_Sid_Identifier_Authority == NULL)
+    {
+      return NULL;
+    }
+  return (pfn_Get_Sid_Identifier_Authority (pSid));
+}
+
+/*
+  END: Wrapper functions around OpenProcessToken
+  and other functions in advapi32.dll that are only
+  supported in Windows NT / 2k / XP
+*/
+
+
 /* Equivalent of strerror for W32 error codes.  */
 char *
 w32_strerror (int error_no)
@@ -254,11 +419,15 @@
   HANDLE          token = NULL;
   SID_NAME_USE    user_type;
 
-  if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token)
-      && GetTokenInformation (token, TokenUser,
+  if (
+			open_process_token (GetCurrentProcess (), TOKEN_QUERY, &token)
+      && get_token_information (
+					token, TokenUser,
 			      (PVOID) user_sid, sizeof (user_sid), &trash)
-      && LookupAccountSid (NULL, *((PSID *) user_sid), name, &length,
-			   domain, &dlength, &user_type))
+      && lookup_account_sid (
+					NULL, *((PSID *) user_sid), name, &length,
+			   domain, &dlength, &user_type)
+			)
     {
       strcpy (the_passwd.pw_name, name);
       /* Determine a reasonable uid value. */
@@ -271,7 +440,7 @@
 	{
 	  SID_IDENTIFIER_AUTHORITY * pSIA;
 
-	  pSIA = GetSidIdentifierAuthority (*((PSID *) user_sid));
+	  pSIA = get_sid_identifier_authority (*((PSID *) user_sid));
 	  /* I believe the relative portion is the last 4 bytes (of 6)
 	     with msb first. */
 	  the_passwd.pw_uid = ((pSIA->Value[2] << 24) +
@@ -282,12 +451,12 @@
 	  the_passwd.pw_uid = the_passwd.pw_uid % 60001;
 
 	  /* Get group id */
-	  if (GetTokenInformation (token, TokenPrimaryGroup,
+	  if (get_token_information (token, TokenPrimaryGroup,
 				   (PVOID) user_sid, sizeof (user_sid), &trash))
 	    {
 	      SID_IDENTIFIER_AUTHORITY * pSIA;
 
-	      pSIA = GetSidIdentifierAuthority (*((PSID *) user_sid));
+	      pSIA = get_sid_identifier_authority (*((PSID *) user_sid));
 	      the_passwd.pw_gid = ((pSIA->Value[2] << 24) +
 				   (pSIA->Value[3] << 16) +
 				   (pSIA->Value[4] << 8)  +
--- _21.3/src/w32.h	2002-05-03 16:41:03.000000000 -0400
+++ 21.3/src/w32.h	2002-11-15 12:44:48.000000000 -0500
@@ -124,5 +124,10 @@
 
 extern void init_ntproc ();
 extern void term_ntproc ();
+extern void syms_of_w32term ();
+extern void syms_of_w32fns ();
+extern void syms_of_w32select ();
+extern void syms_of_w32menu ();
+extern void syms_of_fontset ();
 
 #endif /* EMACS_W32_H */
--- _21.3/src/w32fns.c	2002-10-23 12:55:07.000000000 -0400
+++ 21.3/src/w32fns.c	2002-11-15 12:44:48.000000000 -0500
@@ -283,7 +283,12 @@
 
 /* Window that is tracking the mouse.  */
 static HWND track_mouse_window;
-FARPROC track_mouse_event_fn;
+
+typedef BOOL (WINAPI * TrackMouseEvent_Proc) (
+    IN OUT LPTRACKMOUSEEVENT lpEventTrack
+    );
+
+TrackMouseEvent_Proc track_mouse_event_fn=NULL;
 
 /* W95 mousewheel handler */
 unsigned int msh_mousewheel = 0;
@@ -4929,6 +4934,30 @@
       goto dflt;
 
     case WM_SETFOCUS:
+      /*
+        Reinitialize the function pointer track_mouse_event_fn here.
+        This is required even though it is initialized in syms_of_w32fns
+        which is called in main (emacs.c).
+        Reinitialize the function pointer track_mouse_event_fn here.
+        Even though this function pointer is initialized in
+        syms_of_w32fns which is called from main (emacs.c),
+        we need to initialize it again here in order to prevent
+        a crash that occurs in Windows 9x (possibly only when Emacs
+        was built on Windows NT / 2000 / XP?) when handling the
+        WM_MOUSEMOVE message.
+        The crash occurs when attempting to call the Win32 API
+        function TrackMouseEvent through the function pointer.
+        It appears as if the function pointer that is obtained when
+        syms_of_w32fns is called from main is no longer valid
+        (possibly due to DLL relocation?).
+        To resolve this issue, I have placed a call to reinitialize
+        this function pointer here because this message gets received
+        when the Emacs window gains focus.        
+       */
+      track_mouse_event_fn =
+        (TrackMouseEvent_Proc) GetProcAddress (
+            GetModuleHandle ("user32.dll"),
+            "TrackMouseEvent");
       dpyinfo->faked_key = 0;
       reset_modifiers ();
       register_hot_keys (hwnd);
@@ -14801,7 +14830,7 @@
 
   /* TrackMouseEvent not available in all versions of Windows, so must load
      it dynamically.  Do it once, here, instead of every time it is used.  */
-  track_mouse_event_fn = GetProcAddress (user32_lib, "TrackMouseEvent");
+  track_mouse_event_fn = (TrackMouseEvent_Proc) GetProcAddress (user32_lib, "TrackMouseEvent");
   track_mouse_window = NULL;
 
   w32_visible_system_caret_hwnd = NULL;
--- _21.3/src/w32menu.c	2002-08-05 12:33:44.000000000 -0400
+++ 21.3/src/w32menu.c	2002-11-15 12:44:48.000000000 -0500
@@ -129,8 +129,23 @@
 
 static HMENU current_popup_menu;
 
-FARPROC get_menu_item_info;
-FARPROC set_menu_item_info;
+void syms_of_w32menu ();
+
+typedef BOOL (WINAPI * GetMenuItemInfoA_Proc) (
+    IN HMENU,
+    IN UINT,
+    IN BOOL,
+    IN OUT LPMENUITEMINFOA
+    );
+typedef BOOL (WINAPI * SetMenuItemInfoA_Proc) (
+    IN HMENU,
+    IN UINT,
+    IN BOOL,
+    IN LPCMENUITEMINFOA
+    );
+
+GetMenuItemInfoA_Proc get_menu_item_info=NULL;
+SetMenuItemInfoA_Proc set_menu_item_info=NULL;
 
 Lisp_Object Vmenu_updating_frame;
 
@@ -1591,6 +1606,26 @@
 initialize_frame_menubar (f)
      FRAME_PTR f;
 {
+  HMODULE user32 = GetModuleHandle ("user32.dll");
+  /*
+    Reinitialize the function pointers set_menu_item_info and
+    get_menu_item_info here.
+    Even though these function pointers are initialized in
+    syms_of_w32menu which is called from main (emacs.c),
+    we need to initialize them again here in order to prevent
+    a crash that occurs in Windows 9x (possibly only when Emacs
+    was built on Windows NT / 2000 / XP?) in add_menu_item.
+    The crash occurs when attempting to call the Win32 API
+    function SetMenuItemInfo through the function pointer.
+    It appears as if the function pointer that is obtained when
+    syms_of_w32menu is called from main is no longer valid
+    (possibly due to DLL relocation?).
+    To resolve this issue, I have placed calls to reinitialize
+    these function pointers here because this function is the
+    entry point for menu creation.
+   */
+  get_menu_item_info = (GetMenuItemInfoA_Proc) GetProcAddress (user32, "GetMenuItemInfoA");
+  set_menu_item_info = (SetMenuItemInfoA_Proc) GetProcAddress (user32, "SetMenuItemInfoA");
   /* This function is called before the first chance to redisplay
      the frame.  It has to be, so the frame will have the right size.  */
   FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
@@ -2355,13 +2390,12 @@
 
 #endif /* HAVE_MENUS */
 
-
-syms_of_w32menu ()
+void syms_of_w32menu ()
 {
   /* See if Get/SetMenuItemInfo functions are available.  */
   HMODULE user32 = GetModuleHandle ("user32.dll");
-  get_menu_item_info = GetProcAddress (user32, "GetMenuItemInfoA");
-  set_menu_item_info = GetProcAddress (user32, "SetMenuItemInfoA");
+  get_menu_item_info = (GetMenuItemInfoA_Proc) GetProcAddress (user32, "GetMenuItemInfoA");
+  set_menu_item_info = (SetMenuItemInfoA_Proc) GetProcAddress (user32, "SetMenuItemInfoA");
 
   staticpro (&menu_items);
   menu_items = Qnil;
