std::unique_ptr with custom deleter for win32 LocalFree

后端 未结 4 954
离开以前
离开以前 2021-02-12 23:24

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

CommandLineToArgvW allocates a

4条回答
  •  攒了一身酷
    2021-02-13 00:13

    And what about an answer using Microsoft Windows Implementation Libraries (WIL)?

    First of all "install" WIL (from a terminal):

    c:\dev>git clone https://github.com/microsoft/wil.git
    

    And then:

    #include 
    #include 
    
    #include "c:/dev/wil/include/wil/resource.h"
    
    int main(int argc, char* argv[])
    {
       {
          int n = 0;
          wil::unique_hlocal_ptr 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;
    }
    

提交回复
热议问题