home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / validadd.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  2KB  |  55 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_AUX_SEG
  14. #pragma code_seg(AFX_AUX_SEG)
  15. #endif
  16.  
  17.  
  18. // AfxIsValidString() returns TRUE if the passed pointer
  19. // references a string of at least the given length in characters.
  20. // A length of -1 (the default parameter) means that the string
  21. // buffer's minimum length isn't known, and the function will
  22. // return TRUE no matter how long the string is. The memory
  23. // used by the string can be read-only.
  24.  
  25. BOOL AFXAPI AfxIsValidString(LPCWSTR lpsz, int nLength /* = -1 */)
  26. {
  27.     if (lpsz == NULL)
  28.         return FALSE;
  29.     return afxData.bWin95 || ::IsBadStringPtrW(lpsz, nLength) == 0;
  30. }
  31.  
  32. // As above, but for ANSI strings.
  33.  
  34. BOOL AFXAPI AfxIsValidString(LPCSTR lpsz, int nLength /* = -1 */)
  35. {
  36.     if (lpsz == NULL)
  37.         return FALSE;
  38.     return ::IsBadStringPtrA(lpsz, nLength) == 0;
  39. }
  40.  
  41. // AfxIsValidAddress() returns TRUE if the passed parameter points
  42. // to at least nBytes of accessible memory. If bReadWrite is TRUE,
  43. // the memory must be writeable; if bReadWrite is FALSE, the memory
  44. // may be const.
  45.  
  46. BOOL AFXAPI AfxIsValidAddress(const void* lp, UINT nBytes,
  47.     BOOL bReadWrite /* = TRUE */)
  48. {
  49.     // simple version using Win-32 APIs for pointer validation.
  50.     return (lp != NULL && !IsBadReadPtr(lp, nBytes) &&
  51.         (!bReadWrite || !IsBadWritePtr((LPVOID)lp, nBytes)));
  52. }
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55.