Windows API Reference

Documentation for Windows Core and User Interface APIs

LPCSTR

The LPCSTR type defines a pointer to a constant null-terminated string of single-byte characters.

Definition

In C/C++, this type is typically defined as:

typedef const char * LPCSTR;
                

Details

LPCSTR is a common type used in the Windows API to represent strings that are passed into functions and should not be modified by the function. The 'LP' prefix stands for "Long Pointer" (an older convention, now largely equivalent to a standard pointer), 'C' stands for "Constant", and 'STR' stands for "String".

This type specifically refers to ANSI strings, which use single-byte character encoding. For wide character strings (Unicode), you would typically use LPCWSTR.

Usage

When a function in the Windows API expects a LPCSTR, you should pass a pointer to a null-terminated array of characters. This can be a string literal or a character array.

// Example: Passing a string literal
void DisplayMessage(const char * message);

DisplayMessage("Hello, Windows!");

// Example: Passing a character array
char myBuffer[] = "Another string";
DisplayMessage(myBuffer);
                

Related Types

  • LPSTR: A pointer to a null-terminated string of single-byte characters that can be modified.
  • LPCWSTR: A pointer to a constant null-terminated string of wide characters (Unicode).
  • LPWSTR: A pointer to a null-terminated string of wide characters (Unicode) that can be modified.
  • const char*: The standard C/C++ equivalent.

Function Examples

Many Windows API functions utilize LPCSTR for input parameters:

MessageBoxA

The MessageBoxA function displays a modal message box that is blocked until the user dismisses the message box. It uses the ANSI character set.

int MessageBoxA(
    HWND  hWnd,
    LPCSTR  lpText,
    LPCSTR  lpCaption,
    UINT  uType
);
                

Here, lpText and lpCaption are of type LPCSTR, indicating that the function receives constant pointers to ANSI strings for the message and title.

Best Practices and Considerations

Note: While LPCSTR is still supported for backward compatibility and for interacting with legacy systems or APIs that expect ANSI strings, modern Windows development strongly encourages the use of Unicode (LPCWSTR and LPWSTR) for better internationalization and broader character support.
  • Always ensure the string is null-terminated.
  • Be mindful of the character encoding. LPCSTR implies ANSI, which might not support all characters across different locales.
  • The memory pointed to by an LPCSTR must remain valid for the lifetime of the pointer. You should not deallocate or modify the string data if it was passed as an LPCSTR.