dlls/kernel/console.c::SetConsoleCtrlHandler() cleanup/fixup

Peter Riocreux par+wine_devel at silistix.com
Thu Apr 15 10:18:18 CDT 2004


I have attempted to clean up/fix SetConsoleCtrlHandler() (patch
below). I have attended to the FIXME and fixed the removal code (I
*think* it could never work previously). I have not attempted to
adress any issues of the inheritance of some of the global variables
or their existance.

It gives every impression of working for me, but I have only tested it
with one application.

Suggestions for better extended error codes appreciated.

The MS documentation is to be found at:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/setconsolectrlhandler.asp>


Please CC me on replies - I am subscribed on wine-devel but not
wine-patches.


Peter

Index: dlls/kernel/console.c
===================================================================
RCS file: /home/wine/wine/dlls/kernel/console.c,v
retrieving revision 1.30
diff -u -r1.30 console.c
--- dlls/kernel/console.c	13 Apr 2004 21:16:26 -0000	1.30
+++ dlls/kernel/console.c	15 Apr 2004 15:15:55 -0000
@@ -1422,6 +1422,9 @@
  * Added global variables console_ignore_ctrl_c and handlers[]
  * Does not yet do any error checking, or set LastError if failed.
  * This doesn't yet matter, since these handlers are not yet called...!
+ * 
+ * Peter Riocreux (par at silistix.com) 2004-04-15
+ *   Added error checking and now uses SetLastError()
  */
 
 struct ConsoleHandler {
@@ -1448,17 +1451,20 @@
 {
     BOOL        ret = TRUE;
 
-    FIXME("(%p,%i) - no error checking or testing yet\n", func, add);
-
     if (!func)
     {
+        TRACE("(%p,%i) - first arg being NULL is not allowed in WinME, Win98 or Win95\n", func, add);
 	CONSOLE_IgnoreCtrlC = add;
     }
     else if (add)
     {
         struct ConsoleHandler*  ch = HeapAlloc(GetProcessHeap(), 0, sizeof(struct ConsoleHandler));
 
-        if (!ch) return FALSE;
+        if (!ch)
+	{
+	    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+	    return FALSE;
+	}
         ch->handler = func;
         RtlEnterCriticalSection(&CONSOLE_CritSect);
         ch->next = CONSOLE_Handlers;
@@ -1467,34 +1473,37 @@
     }
     else
     {
-        struct ConsoleHandler**  ch;
         RtlEnterCriticalSection(&CONSOLE_CritSect);
-        for (ch = &CONSOLE_Handlers; *ch; *ch = (*ch)->next)
-        {
-            if ((*ch)->handler == func) break;
-        }
-        if (*ch)
-        {
-            struct ConsoleHandler*   rch = *ch;
-
-            /* sanity check */
-            if (rch == &CONSOLE_DefaultConsoleHandler)
-            {
-                ERR("Who's trying to remove default handler???\n");
-                ret = FALSE;
-            }
-            else
-            {
-                rch = *ch;
-                *ch = (*ch)->next;
-                HeapFree(GetProcessHeap(), 0, rch);
-            }
-        }
-        else
-        {
-            WARN("Attempt to remove non-installed CtrlHandler %p\n", func);
-            ret = FALSE;
-        }
+	if (func == CONSOLE_DefaultHandler)
+	{
+	    ERR("Attempt to remove default CtrlHandler %p\n", func);
+	    SetLastError(ERROR_FUNCTION_FAILED);
+	    ret = FALSE;
+	}
+	else
+	{
+	    struct ConsoleHandler* ch, * prev ;
+	    prev = NULL;
+	    for (ch = CONSOLE_Handlers; ch; prev = ch, ch = ch->next)
+	    {
+	        if (ch->handler == func)
+		{
+		    if (ch == CONSOLE_Handlers)
+		        CONSOLE_Handlers = ch->next;
+		    else
+		        prev->next = ch->next;
+		    HeapFree(GetProcessHeap(), 0, ch);
+		    break;
+		}
+	    }
+
+	    if (! ch)
+	    {
+	        WARN("Attempt to remove non-installed CtrlHandler %p\n", func);
+		SetLastError(ERROR_INVALID_ADDRESS);
+		ret = FALSE;
+	    }
+	}
         RtlLeaveCriticalSection(&CONSOLE_CritSect);
     }
     return ret;



More information about the wine-patches mailing list