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

  • SEARCH
  • Home
  • 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 7697677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:04:15+00:00 2026-05-31T22:04:15+00:00

I have the win32 API CommandLineToArgvW which returns a LPWSTR* and warns me that

  • 0

I have the win32 API CommandLineToArgvW which returns a LPWSTR* and
warns me that

CommandLineToArgvW allocates a block of contiguous memory for
pointers to the argument strings, and for the argument strings
themselves; the calling application must free the memory used by the
argument list when it is no longer needed. To free the memory, use a
single call to the LocalFree function.

See
http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx

What is a C++ idiomatic way to free the memory in the above case?

I was thinking to a std::unique_ptr with a custom deleter, something like that:

#include <Windows.h>
#include <memory>
#include <iostream>

template< class T >
struct Local_Del
{
   void operator()(T*p){::LocalFree(p);}
};

int main(int argc, char* argv[])
{
   {
      int n = 0;
      std::unique_ptr< LPWSTR, Local_Del< LPWSTR > > p( ::CommandLineToArgvW(L"cmd.exe p1 p2 p3",&n) );
      for ( int i = 0; i < n; i++ ) {
         std::wcout << p.get()[i] << L"\n";
      }
   }

    return 0;
}

Is there any problem in the above code?

  • 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-05-31T22:04:16+00:00Added an answer on May 31, 2026 at 10:04 pm

    It looks correct to me. You could make it a little more succinct by specifying the unique_ptr‘s deleter inline rather than creating a functor for it.

    std::unique_ptr<LPWSTR, HLOCAL(__stdcall *)(HLOCAL)> 
          p( ::CommandLineToArgvW( L"cmd.exe p1 p2 p3", &n ), ::LocalFree );
    

    Or, if you don’t want to mess with LocalFree‘s signature and calling conventions you can use a lambda to do the deletion.

    std::unique_ptr<LPWSTR, void(*)(LPWSTR *)> 
          p( ::CommandLineToArgvW( L"cmd.exe p1 p2 p3", &n ), 
             [](LPWSTR *ptr){ ::LocalFree( ptr ); } );
    

    Note: At the time this answer was first written, VS2010 was the released VS version available. It doesn’t support conversion of capture-less lambdas to function pointers, so you’d have to use std::function in the second example

    std::unique_ptr<LPWSTR, std::function<void(LPWSTR *)>> 
          p( ::CommandLineToArgvW( L"cmd.exe p1 p2 p3", &n ), 
             [](LPWSTR *ptr){ ::LocalFree( ptr ); } );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ application that uses the Win32 API for Windows, and I'm
You're stepping through C/C++ code and have just called a Win32 API that has
I have a Windows server application, implemented in C++ using the Win32 API, that
I have a library that I'm writing which processes keystroke events using the Win32
When we want to use Win32 API, we'd specify the import libraries that have
I have created a minidump using win32 api [DllImport(DbgHelp.dll, SetLastError = true)] private static
I have developend a window application(Win32 API) in visual C++. I have to add
I using the Win32 API and C/C++. I have a HFONT and want to
We have a Win32 application that hosts the .NET runtime and opens up .NET
I have a win32 GUI (MFC) application which I need to port to a

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.