| 684 | } |
| 685 | |
| 686 | QString formatCommandLine(const QFileInfo& targetInfo, const QString& cmd) |
| 687 | { |
| 688 | // yeah, FormatMessage() expects at least as many arguments as there are |
| 689 | // placeholders and while the command for associations should typically only |
| 690 | // have %1, the user can actually enter anything in the registry |
| 691 | // |
| 692 | // since the maximum number of arguments is 99, this creates an array of 99 |
| 693 | // wchar_* where the first one (%1) points to the filename and the remaining |
| 694 | // 98 to "" |
| 695 | // |
| 696 | // FormatMessage() actually takes a va_list* for the arguments, but by passing |
| 697 | // FORMAT_MESSAGE_ARGUMENT_ARRAY, an array of DWORD_PTR can be given instead |
| 698 | |
| 699 | // 99 arguments |
| 700 | std::array<DWORD_PTR, 99> args; |
| 701 | |
| 702 | // first one is the filename |
| 703 | const auto wpath = targetInfo.absoluteFilePath().toStdWString(); |
| 704 | args[0] = reinterpret_cast<DWORD_PTR>(wpath.c_str()); |
| 705 | |
| 706 | // remaining are "" |
| 707 | std::fill(args.begin() + 1, args.end(), reinterpret_cast<DWORD_PTR>(L"")); |
| 708 | |
| 709 | // must be freed with LocalFree() |
| 710 | wchar_t* buffer = nullptr; |
| 711 | |
| 712 | const auto wcmd = cmd.toStdWString(); |
| 713 | |
| 714 | const auto n = |
| 715 | ::FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY | |
| 716 | FORMAT_MESSAGE_FROM_STRING, |
| 717 | wcmd.c_str(), 0, 0, reinterpret_cast<LPWSTR>(&buffer), 0, |
| 718 | reinterpret_cast<va_list*>(&args[0])); |
| 719 | |
| 720 | if (n == 0 || !buffer) { |
| 721 | const auto e = GetLastError(); |
| 722 | |
| 723 | log::error("failed to format command line '{}' with path '{}', {}", cmd, |
| 724 | targetInfo.absoluteFilePath(), formatSystemMessage(e)); |
| 725 | |
| 726 | return {}; |
| 727 | } |
| 728 | |
| 729 | auto s = QString::fromWCharArray(buffer, n); |
| 730 | ::LocalFree(buffer); |
| 731 | |
| 732 | return s.trimmed(); |
| 733 | } |
| 734 | |
| 735 | std::pair<QString, QString> splitExeAndArguments(const QString& cmd) |
| 736 | { |
no test coverage detected