Skip to content

Instantly share code, notes, and snippets.

@decagondev
Created September 22, 2025 21:37
Show Gist options
  • Save decagondev/899437c8ba0045d65d5cf2445ca95b87 to your computer and use it in GitHub Desktop.
Save decagondev/899437c8ba0045d65d5cf2445ca95b87 to your computer and use it in GitHub Desktop.

Documentation and Summary for std::wstring Constructor and MultiByteToWideChar

std::wstring(size_t count, wchar_t ch)

Summary

The std::wstring(size_t count, wchar_t ch) constructor is a member of the C++ Standard Library's std::basic_string class, specialized for wide characters (wchar_t) as std::wstring. It creates a wide string of length count, where each character is initialized to the specified wide character ch. In the provided code, it is used to allocate a std::wstring of size size - 1 filled with null characters (0) to prepare a buffer for the MultiByteToWideChar conversion.

Documentation

  • Function Signature: std::wstring::basic_string(size_t count, wchar_t ch, const Allocator& alloc = Allocator())
  • Parameters:
    • count: The number of characters in the resulting string.
    • ch: The wide character (wchar_t) to initialize each position in the string.
    • alloc: (Optional) The allocator object used for memory management (defaults to the default allocator).
  • Return Value: A std::wstring object of length count, with each character initialized to ch.
  • Behavior:
    • Constructs a wide string with count copies of the character ch.
    • Allocates sufficient storage for the string using the provided or default allocator.
    • If count is zero, an empty string is constructed.
  • Exceptions:
    • May throw std::length_error if count exceeds the maximum size of the string (max_size()).
    • May throw exceptions from the allocator if memory allocation fails.
  • Reference: cppreference.com - std::basic_string constructor (see constructor (2)).
  • Example Usage in Code:
    std::wstring result(size - 1, 0); // Creates a wstring of size-1 characters, all initialized to 0

MultiByteToWideChar

Summary

MultiByteToWideChar is a Windows API function that converts a string from a specified code page to a wide-character (Unicode UTF-16) string. In the provided code, it is used to determine the required size for the output std::wstring and then to perform the conversion of a null-terminated const char* string (using the ANSI code page, CP_ACP) into a std::wstring.

Documentation

  • Function Signature:
    int MultiByteToWideChar(
        UINT CodePage,
        DWORD dwFlags,
        LPCCH lpMultiByteStr,
        int cbMultiByte,
        LPWSTR lpWideCharStr,
        int cchWideChar
    );
  • Parameters:
    • CodePage: The code page to use for the conversion (e.g., CP_ACP for the system's ANSI code page, CP_UTF8 for UTF-8).
    • dwFlags: Flags controlling the conversion (e.g., 0 for default behavior, or flags like MB_PRECOMPOSED).
    • lpMultiByteStr: Pointer to the input multi-byte string to convert.
    • cbMultiByte: Size (in bytes) of the input string. If -1, the function assumes a null-terminated string and calculates the length.
    • lpWideCharStr: Pointer to the output buffer for the wide-character string. If nullptr, the function returns the required buffer size.
    • cchWideChar: Size (in wide characters) of the output buffer. If 0, the function returns the required size without performing the conversion.
  • Return Value:
    • If cchWideChar is 0, returns the required size (in wide characters) for the output buffer, including the null terminator.
    • Otherwise, returns the number of wide characters written to lpWideCharStr (excluding the null terminator).
    • Returns 0 on error; use GetLastError() to retrieve the error code.
  • Behavior:
    • Converts a multi-byte string to a wide-character (Unicode UTF-16) string based on the specified code page.
    • When called with lpWideCharStr = nullptr and cchWideChar = 0, it computes the required size for the output buffer.
    • When called with a valid lpWideCharStr and cchWideChar, it performs the conversion and writes the result to the output buffer.
  • Exceptions: Does not throw exceptions but returns 0 on failure, with error details available via GetLastError().
  • Reference: Microsoft Learn - MultiByteToWideChar
  • Example Usage in Code:
    int size = MultiByteToWideChar(CP_ACP, 0, str, -1, nullptr, 0); // Get required size
    MultiByteToWideChar(CP_ACP, 0, str, -1, &result[0], size); // Perform conversion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment