Fix warning cause by previous libdcp bump.
[dcpomatic.git] / src / lib / cross_windows.cc
index 0549615787214dbd0ce4ef18b31ced6b5df78aca..9181b6c8abf1deb8861a21c65b3db63d4d62a883 100644 (file)
@@ -30,6 +30,7 @@
 #include "dcpomatic_assert.h"
 #include "util.h"
 #include <dcp/file.h>
+#include <dcp/filesystem.h>
 #include <dcp/raw_convert.h>
 #include <glib.h>
 extern "C" {
@@ -44,10 +45,11 @@ extern "C" {
 #include <setupapi.h>
 #include <fileapi.h>
 #undef DATADIR
-#include <shlwapi.h>
-#include <shlobj.h>
-#include <shellapi.h>
 #include <knownfolders.h>
+#include <processenv.h>
+#include <shellapi.h>
+#include <shlobj.h>
+#include <shlwapi.h>
 #include <fcntl.h>
 #include <fstream>
 #include <map>
@@ -124,37 +126,51 @@ cpu_info ()
 
 
 void
-run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
+run_ffprobe(boost::filesystem::path content, boost::filesystem::path out, bool err, string args)
 {
        SECURITY_ATTRIBUTES security;
        security.nLength = sizeof (security);
        security.bInheritHandle = TRUE;
        security.lpSecurityDescriptor = 0;
 
-       HANDLE child_stderr_read;
-       HANDLE child_stderr_write;
-       if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
+       HANDLE child_out_read;
+       HANDLE child_out_write;
+       if (!CreatePipe(&child_out_read, &child_out_write, &security, 0)) {
                LOG_ERROR_NC ("ffprobe call failed (could not CreatePipe)");
                return;
        }
 
+       if (!SetHandleInformation(child_out_read, HANDLE_FLAG_INHERIT, 0)) {
+               LOG_ERROR_NC("ffprobe call failed (could not SetHandleInformation)");
+               return;
+       }
+
        wchar_t dir[512];
        MultiByteToWideChar (CP_UTF8, 0, directory_containing_executable().string().c_str(), -1, dir, sizeof(dir));
 
        STARTUPINFO startup_info;
        ZeroMemory (&startup_info, sizeof (startup_info));
        startup_info.cb = sizeof (startup_info);
-       startup_info.hStdError = child_stderr_write;
+       if (err) {
+               startup_info.hStdError = child_out_write;
+       } else {
+               startup_info.hStdOutput = child_out_write;
+       }
        startup_info.dwFlags |= STARTF_USESTDHANDLES;
 
        wchar_t command[512];
-       wcscpy (command, L"ffprobe.exe \"");
+       wcscpy(command, L"ffprobe.exe ");
+
+       wchar_t tmp[512];
+       MultiByteToWideChar(CP_UTF8, 0, args.c_str(), -1, tmp, sizeof(tmp));
+       wcscat(command, tmp);
+
+       wcscat(command, L" \"");
 
-       wchar_t file[512];
-       MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
-       wcscat (command, file);
+       MultiByteToWideChar(CP_UTF8, 0, dcp::filesystem::canonical(content).make_preferred().string().c_str(), -1, tmp, sizeof(tmp));
+       wcscat(command, tmp);
 
-       wcscat (command, L"\"");
+       wcscat(command, L"\"");
 
        PROCESS_INFORMATION process_info;
        ZeroMemory (&process_info, sizeof (process_info));
@@ -169,12 +185,12 @@ run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
                return;
        }
 
-       CloseHandle (child_stderr_write);
+       CloseHandle(child_out_write);
 
        while (true) {
                char buffer[512];
                DWORD read;
-               if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
+               if (!ReadFile(child_out_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
                        break;
                }
                o.write(buffer, read, 1);
@@ -185,7 +201,7 @@ run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
        WaitForSingleObject (process_info.hProcess, INFINITE);
        CloseHandle (process_info.hProcess);
        CloseHandle (process_info.hThread);
-       CloseHandle (child_stderr_read);
+       CloseHandle(child_out_read);
 }
 
 
@@ -657,3 +673,17 @@ show_in_file_manager (boost::filesystem::path, boost::filesystem::path select)
        return (reinterpret_cast<int64_t>(r) <= 32);
 }
 
+
+ArgFixer::ArgFixer(int, char**)
+{
+       auto cmd_line = GetCommandLineW();
+       auto wide_argv = CommandLineToArgvW(cmd_line, &_argc);
+
+       _argv_strings.resize(_argc);
+       _argv = new char*[_argc];
+       for (int i = 0; i < _argc; ++i) {
+               _argv_strings[i] = wchar_to_utf8(wide_argv[i]);
+               _argv[i] = const_cast<char*>(_argv_strings[i].c_str());
+       }
+}
+