Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.


Use this function to determine whether a pointer to a string is valid.

BOOL AfxIsValidString(
   LPCSTR lpsz,
   int nLength = -1 



The pointer to test.


Specifies the length of the string to be tested, in bytes. A value of –1 indicates that the string will be null-terminated.

In debug builds, nonzero if the specified pointer points to a string of the specified size; otherwise 0.

In non-debug builds, nonzero if lpsz is not NULL; otherwise 0.

// Create a character string which should be valid.
char str[12] = "hello world";

// Create a null pointer, which should be an invalid string.
char *null = (char *)0x0;

ASSERT(AfxIsValidString(str, 12));
ASSERT(!AfxIsValidString(null, 5));

Community Additions

© 2015 Microsoft