discuss-gnustep
[Top][All Lists]
Advanced

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

system error messages in GNUstep (mingw)


From: Michael Scheibler
Subject: system error messages in GNUstep (mingw)
Date: Tue, 22 May 2001 13:09:41 +0200

Example:

- (BOOL) removeFileAtPath: (NSString*)path
    handler: handler


#if defined(__MINGW__)
      if (DeleteFile(cpath) == FALSE)
#else
      if (unlink(cpath) < 0)
#endif
...
       [info setObject: [NSString stringWithCString: strerror(errno)]
         forKey: @"Error"];


WinAPI functions do not save the global error number in errno. You have to
use GetLastError() instead. Unfortunately there is no simple equivalent to
strerror(errno).
WinAPI provides FormatMessage for this. Here are two functions, which
provide a simple possibility to get the error message:

#include <windows.h>

LPTSTR GetErrorMsg(DWORD msgId)
{
  LPVOID lpMsgBuf;

  FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL, msgId,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPTSTR)&lpMsgBuf, 0, NULL);

  return (LPTSTR)lpMsgBuf;
}

LPTSTR GetLastErrorMsg()
{
  return GetErrorMsg(GetLastError());
}




In the above mentioned example, the code must then be like this:

#if defined(__MINGW__)
       [info setObject: [NSString stringWithCString: GetLastErrorMsg()]
         forKey: @"Error"];
#else
       [info setObject: [NSString stringWithCString: strerror(errno)]
         forKey: @"Error"];
#endif





reply via email to

[Prev in Thread] Current Thread [Next in Thread]