Ignore:
Timestamp:
Jun 22, 1999, 7:25:28 PM (26 years ago)
Author:
phaller
Message:

Fix: FormatMessageA fixed, A/W potential memoryleak fixed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/message.cpp

    r100 r157  
    1 /* $Id: message.cpp,v 1.4 1999-06-10 20:48:00 phaller Exp $ */
     1/* $Id: message.cpp,v 1.5 1999-06-22 17:25:28 phaller Exp $ */
    22
    33/*
     
    125125        DWORD   nolinefeed = 0;
    126126
    127         dprintf(("FormatMessageA\n"));
     127        dprintf(("KERNEL32: FormatMessageA (%08xh,%s,%08xh,%08xh,%08xh,%08xh,%08xh)\n",
     128                 dwFlags,
     129                 lpSource,
     130                 dwMessageId,
     131                 dwLanguageId,
     132                 lpBuffer,
     133                 nSize,
     134                 args));
    128135
    129136        from = NULL;
    130         if (dwFlags & FORMAT_MESSAGE_FROM_STRING) {
    131                 from = (char *)HeapAlloc(GetProcessHeap(),0, strlen((char *)lpSource)+1);
    132                 UnicodeToAscii((LPWSTR)lpSource, from);
    133         }
    134         if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
    135                 from = (char *)HeapAlloc(GetProcessHeap(),0,200 );
    136                 sprintf(from,"Systemmessage, messageid = 0x%08lx\n",dwMessageId);
    137         }
    138         if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
    139                 int bufsize;
    140 
    141                 dwMessageId &= 0xFFFF;
    142                 bufsize=LoadMessageA(0,dwMessageId,dwLanguageId,NULL,100);
    143                 if (bufsize) {
    144                         from = (char *)HeapAlloc(GetProcessHeap(), 0, bufsize + 1 );
    145                         LoadMessageA(0,dwMessageId,dwLanguageId,from,bufsize+1);
    146                 }
    147         }
    148         target  = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
    149         t       = target;
    150         talloced= 100;
     137
     138  if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
     139  {
     140    from = (char *)HeapAlloc(GetProcessHeap(),
     141                             0,
     142                             strlen((char*)lpSource) + 1);
     143    strcpy (from,
     144            (char*)lpSource);
     145  }
     146  else
     147    if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
     148    {
     149      from = (char *)HeapAlloc(GetProcessHeap(), 0, 200 );
     150      sprintf(from,"Systemmessage, messageid = 0x%08lx\n",dwMessageId);
     151    }
     152    else
     153    if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
     154    {
     155      int bufsize;
     156
     157      dwMessageId &= 0xFFFF;
     158      bufsize=LoadMessageA(0,dwMessageId,dwLanguageId,NULL,100);
     159      if (bufsize)
     160      {
     161        from = (char *)HeapAlloc(GetProcessHeap(), 0, bufsize + 1 );
     162        LoadMessageA(0,dwMessageId,dwLanguageId,from,bufsize+1);
     163      }
     164    }
     165
     166  target  = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
     167  t       = target;
     168  talloced= 100;
    151169
    152170#define ADD_TO_T(c) \
     
    250268                *t='\0';
    251269        }
    252         if (!nolinefeed && t[-1]!='\n')
    253                 ADD_TO_T('\n');
    254         talloced = strlen(target)+1;
    255         if (nSize && talloced<nSize) {
    256                 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
    257         }
    258         if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
    259                 /* nSize is the MINIMUM size */
    260                 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,talloced);
    261                 memcpy(*(LPSTR*)lpBuffer,target,talloced);
    262         } else
    263                 strncpy(lpBuffer,target,nSize);
    264         HeapFree(GetProcessHeap(),0,target);
    265         if (from) HeapFree(GetProcessHeap(),0,from);
    266         dprintf(("FormatMessageA returned %s\n",
    267                                 (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
    268                                 *(LPSTR*)lpBuffer:
    269                                 lpBuffer));
    270         return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
    271                         strlen(*(LPSTR*)lpBuffer):
    272                         strlen(lpBuffer);
     270
     271  if (!nolinefeed && t[-1]!='\n')
     272    ADD_TO_T('\n');
     273
     274  talloced = strlen(target)+1;
     275  if (nSize && talloced<nSize)
     276  {
     277    target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
     278  }
     279
     280  if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
     281  {
     282    /* nSize is the MINIMUM size */
     283    *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(GMEM_ZEROINIT,talloced);
     284    memcpy(*(LPSTR*)lpBuffer,target,talloced);
     285  }
     286  else
     287    strncpy(lpBuffer,target,nSize);
     288
     289  HeapFree(GetProcessHeap(),0,target);
     290
     291  if (from) HeapFree(GetProcessHeap(),0,from);
     292
     293  dprintf(("KERNEL32: FormatMessageA returned %s\n",
     294           (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
     295            *(LPSTR*)lpBuffer:
     296            lpBuffer));
     297
     298  return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
     299            strlen(*(LPSTR*)lpBuffer):
     300            strlen(lpBuffer);
    273301}
    274302#undef ADD_TO_T
     
    292320        DWORD   nolinefeed = 0;
    293321
    294         dprintf(("FormatMessageW\n"));
     322        dprintf(("KERNEL32: FormatMessageW\n"));
    295323
    296324        from = NULL;
     
    299327                UnicodeToAscii((LPWSTR)lpSource, from);
    300328        }
     329        else
    301330        if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
    302331                /* gather information from system message tables ... */
     
    304333                sprintf(from,"Systemmessage, messageid = 0x%08lx\n",dwMessageId);
    305334        }
     335        else
    306336        if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
    307337                int bufsize;
     
    315345                }
    316346        }
     347
    317348        target  = (char *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 );
    318349        t       = target;
     
    432463        else    AsciiToUnicode(target, lpBuffer);
    433464
    434         dprintf(("FormatMessageW returned %s\n", target));
     465        dprintf(("KERNEL32: FormatMessageW returned %s\n", target));
    435466
    436467        HeapFree(GetProcessHeap(),0,target);
     
    456487 char *msgBuffer;
    457488
    458   dprintf(("FormatMessageA Not Implemented\n"));
     489  dprintf(("KERNEL32: FormatMessageA Not Implemented\n"));
    459490
    460491  if(dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
     
    479510 WCHAR *msgBuffer;
    480511
    481   dprintf(("FormatMessageW Not Implemented\n"));
     512  dprintf(("KERNEL32: FormatMessageW Not Implemented\n"));
    482513
    483514  if(dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
Note: See TracChangeset for help on using the changeset viewer.