validadd.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #include "stdafx.h"
  11. #ifdef AFX_AUX_SEG
  12. #pragma code_seg(AFX_AUX_SEG)
  13. #endif
  14. // AfxIsValidString() returns TRUE if the passed pointer
  15. // references a string of at least the given length in characters.
  16. // A length of -1 (the default parameter) means that the string
  17. // buffer's minimum length isn't known, and the function will
  18. // return TRUE no matter how long the string is. The memory
  19. // used by the string can be read-only.
  20. BOOL AFXAPI AfxIsValidString(LPCWSTR lpsz, int nLength /* = -1 */)
  21. {
  22. if (lpsz == NULL)
  23. return FALSE;
  24. return /*WINSCP afxData.bWin95 || */::IsBadStringPtrW(lpsz, nLength) == 0;
  25. }
  26. // As above, but for ANSI strings.
  27. BOOL AFXAPI AfxIsValidString(LPCSTR lpsz, int nLength /* = -1 */)
  28. {
  29. if (lpsz == NULL)
  30. return FALSE;
  31. return ::IsBadStringPtrA(lpsz, nLength) == 0;
  32. }
  33. // AfxIsValidAddress() returns TRUE if the passed parameter points
  34. // to at least nBytes of accessible memory. If bReadWrite is TRUE,
  35. // the memory must be writeable; if bReadWrite is FALSE, the memory
  36. // may be const.
  37. BOOL AFXAPI AfxIsValidAddress(const void* lp, UINT nBytes,
  38. BOOL bReadWrite /* = TRUE */)
  39. {
  40. // simple version using Win-32 APIs for pointer validation.
  41. return (lp != NULL && !IsBadReadPtr(lp, nBytes) &&
  42. (!bReadWrite || !IsBadWritePtr((LPVOID)lp, nBytes)));
  43. }
  44. /////////////////////////////////////////////////////////////////////////////