C++: how to handle LPWSTR as output in Win32 applications

thebluetropics 1,046 Reputation points
2022-09-26T06:35:51.51+00:00

I have used mciSendStringW() in my programs to play audios

One of the parameter in this function takes LPWSTR as an output. I know it is a pointer to something string-like, wide-character.

Here is my try but it throws build error:

   LPWSTR returnedString;  
     
   mciSendStringW(..., &returnedString, 1024, ...);  

Note that the third parameter takes a something number, the string length, this means returnedString must be an array-like.

so I have tried this:

   wchar_t returnedString[1024];  

But still didnt work. It simply throws error.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,653 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,762 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RLWA32 45,701 Reputation points
    2022-09-26T07:39:20.933+00:00
       WCHAR wszCommand[]{ L"insert command here" };  
       WCHAR wszBuffer[1024]{};  
       LPWSTR pwszBuffer = static_cast<LPWSTR>(HeapAlloc(GetProcessHeap(), 0, 1024 * sizeof(WCHAR)));  
       	  
       mciSendStringW(wszCommand, wszBuffer, ARRAYSIZE(wszBuffer), hwnd);  
       mciSendStringW(wszCommand, pwszBuffer, 1024, hwnd);  
    

    Its just a buffer. Consider the two different ways to pass the pointer to the buffer as shown above.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.