简体   繁体   中英

How can I use the windows cmd command "convert" in std::system?

Whenever I run this line:

system("convert -quality 80 captureqwsx.bmp captureqwsx.jpg");

this is what I get:

Invalid Parameter - 80

(not sure if that matters but I'm using visual studio 2019)

The only convert command-line that is in the default PATH of a Windows system is a "Converts a FAT volume to NTFS" utility. If you run it from the command prompt with the parameters you provide, it in fact returns "Invalid Parameter - 80".

On Windows, the best option for launching a program from another program is to use ShellExecuteEx . It gives you the best control over the UI experience, especially when dealing with User Account Control (UAC) scenarios.

Here's an example helper function:

bool SpawnProcessAndWait( const wchar_t *szExePath,
    const wchar_t *szExeArgs,
    DWORD *pdwExitCode )
{
    if( !szExePath )
        return false;

    // Get working directory from executable path.
    WCHAR szDirectory[MAX_PATH] = {};
    wcscpy_s( szDirectory, szExePath );
    PathRemoveFileSpec( szDirectory );

    SHELLEXECUTEINFOW info = {};
    info.cbSize = sizeof( info );
    info.lpVerb = L"open";
    info.fMask = SEE_MASK_FLAG_NO_UI | SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS;
    info.lpFile = szExePath;
    info.lpParameters = szExeArgs;
    info.lpDirectory = szDirectory;
    info.nShow = SW_SHOW;
    if( !ShellExecuteExW( &info ) )
        return false;
    
    // Wait for process to finish.
    WaitForSingleObject( info.hProcess, INFINITE );
    
    // Return exit code from process, if requested by caller.
    if( pdwExitCode )
        GetExitCodeProcess( info.hProcess, pdwExitCode );

    CloseHandle( info.hProcess );
    return true;
}

You should not assume that the specific utility you want to call is located in the search PATH. Instead, find a way to obtains it's actual location and use that as the szExePath .

If you are in fact using ImageMagick, you can read Windows registry entries from HKEY_LOCAL_MACHINE\SOFTWARE\ImageMagick\Current using the BinPath key which tells where to find magick.exe . Then you call magick.exe with convert as the first paraemter.

You likely want to provide a CWD that matches a different directory than use the CWD of the executable like this sample method does. You'd provide this parameter in info.lpDirectory .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM