Windows API Reference

LPSTR

LPSTR

A pointer to a null-terminated ANSI string.

Remarks

The LPSTR type is used to represent a pointer to a null-terminated string that uses the ANSI character set. In the Windows API, this is often referred to as a "character string" or "ANSI string". The characters are typically 1-byte wide.

This type is commonly used in functions that deal with file paths, registry keys, configuration settings, and other text-based data where the characters are represented using the ANSI encoding.

For Unicode strings, use the LPWSTR type. Most modern Windows applications should prefer using Unicode (LPWSTR) for broader character support and internationalization. However, LPSTR remains relevant for backward compatibility and certain low-level operations.

Type Definition

typedef char *LPSTR;

Related Types

  • LPWSTR: A pointer to a null-terminated Unicode string.
  • LPCSTR: A pointer to a constant null-terminated ANSI string.
  • PSTR: An alias for LPSTR.
  • PCSTR: An alias for LPCSTR.

Example Usage (Conceptual)

The following C++ snippet illustrates how LPSTR might be used:

#include <windows.h>
#include <iostream>

int main() {
    // Example ANSI string
    char szBuffer[] = "Hello, World!";
    LPSTR pString = szBuffer; // pString now points to the ANSI string

    // Using a Windows API function that accepts LPSTR (e.g., GetUserNameA)
    // Note: Actual usage would involve buffer allocation and error handling.
    // TCHAR userName[256]; // Using TCHAR for generality, but can also use char
    // DWORD userNameLen = sizeof(userName) / sizeof(TCHAR);
    // if (GetUserName(userName, &userNameLen)) {
    //     std::wcout << L"User: " << userName << std::endl;
    // } else {
    //     std::cerr << "Failed to get username." << std::endl;
    // }

    std::cout << "The string pointed to by LPSTR is: " << pString << std::endl;

    return 0;
}

In this example, LPSTR is used to point to a character array that represents an ANSI string. Many Windows API functions have ANSI-specific versions ending with 'A' (e.g., MessageBoxA, CreateFileA) which accept LPSTR arguments.