Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:54:51+00:00 2026-06-18T08:54:51+00:00

Given a WinAPI function which returns it’s result via a C style string OUT

  • 0

Given a WinAPI function which returns it’s result via a C style string OUT parameter e.g.:

int WINAPI GetWindowTextW(
   _In_   HWND hWnd,
   _Out_  LPTSTR lpString,
   _In_   int nMaxCount
);

Is there a better way of using the function than what I’m doing below?

HWND handle; // Assume this is initialised to contain a real window handle
std::wstring title;
wchar_t buffer[512];
GetWindowTextW(handle, buffer, sizeof(buffer));
title = buffer;

The above code works, but I have the following issues with it:

  1. The buffer size is completely arbitrary since I have no way to know the length of the string that the function might return. This “feels” wrong to me – I have always tried to avoid magic numbers in my code.

  2. If the function returns a string which is larger than the buffer, it will get truncated – this is bad!

  3. Whenever the function returns a string which is smaller than the buffer, I will be wasting memory. This is not as bad as (2), but I’m not thrilled about the idea of setting aside large chunks of memory (e.g. 1024 bytes in my example above) for something that might only need a few bytes in practice.

Are there any other alternatives?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:54:52+00:00Added an answer on June 18, 2026 at 8:54 am

    Call the function multiple times with temporary buffers of different sizes. Begin with a buffer of, say, 8. Double the buffer size and call it again. Repeat until it returns the same count as the last time. Then you can allocate the exact size buffer and copy what you’ve got there. There are a number of Win32 functions with similar behavior.

    You may use GetWindowTextLength(), but it probably won’t help much if there are race conditions (you may end up with a truncated text because of them).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code: $(.force-selection).blur(function() { var value = $('matched-item').val(); //check if the input's
Given that an input String may be specified as follows: read(xpath(‘...’)) or xpath(‘...’) or
I get a List<string> of files via recursion through FileFinderEx via win32 api (DllImport
I'm using Visual Studion 2010, and given the following example function obtained from WinGDI.h:
The Microsoft WinAPI documentation appears to suggest that user32.dll contains a function called GetNextWindow()
I need a function that simply returns bool if there is an entity at
I'm trying to figure out the executable used to open files of a given
I can't seem to get a reliable timestamp using winapi functions. For example: int
#include <windows.h> #include <iostream> using namespace std; int count = 0; DWORD WINAPI Tf1
I got two questions about ReadFile function from Win32 API. First of all, given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.