Can I use C++ function _wspawnlp() to call the ssh.exe from my Visual Studio C++ codes?

Yong Su 21 Reputation points
2021-09-01T21:21:59.76+00:00

Hi, experts,
I need to call ssh.exe from VS c++ codes. I tried following from the Visual Studio 2013 c++ codes:
String sExeName("ssh");
String sHost;

    errno_t err = _set_errno(0);
    intptr_t t = _wspawnlp(_P_WAIT, sExeName.c_str(), sExeName.c_str(), sHost.c_str(), NULL);
_get_errno(&err);

ssh.exe is in my path: "%SYSTEMROOT%\System32\OpenSSH". But I always get t = -1, and err = 2, meaning that they cannot find the file. Why is that? What is the correct way to call the ssh.exe?

Thanks,

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
727 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,426 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,537 questions
Sysinternals
Sysinternals
Advanced system utilities to manage, troubleshoot, and diagnose Windows and Linux systems and applications.
1,093 questions
{count} votes

Accepted answer
  1. Castorix31 81,741 Reputation points
    2021-09-02T19:58:52.387+00:00

    You can also use ShellExecute

    This test works for me, with hardcoded paths for testing (x86 on Windows 10 64) :

    BOOL bWow64 = false;
    IsWow64Process(GetCurrentProcess(), &bWow64);
    if (bWow64)
    {
        PVOID OldValue = NULL;
        if (Wow64DisableWow64FsRedirection(&OldValue))
        {
        }
    }
    HINSTANCE h = ShellExecute(NULL, NULL, L"cmd.exe", L"/c C:\\Windows\\System32\\OpenSSH\\ssh.exe -Q key > c:\\temp\\output.txt", NULL, SW_SHOWNORMAL);
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,516 Reputation points
    2021-09-02T00:21:39.253+00:00

    Go to the Configuration Manager and change the platform to x64. The following also got the error code 2 but then changing to x64 fixed it.

    const TCHAR* sExeName = _T("C:\\Windows\\System32\\OpenSSH\\ssh.exe");
    std::wcout << sExeName << '\n';
    errno_t err = _set_errno(0);
    intptr_t t = _tspawnlp(_P_WAIT, sExeName, sExeName, NULL);
    _get_errno(&err);
    std::cout << "Error: " << err << '\n';
    

    There are many variations of spawn so it might be difficult to find answers. Most Windows developers use CreateProcess so searching with that I found Createprocess() of "C:\Windows\System32\OpenSSH\ssh.exe" fails with error=2 - Stack Overflow.

    CreateProcess is the Windows API function that a C language spawn would use in Windows.

    1 person found this answer helpful.

  2. Yong Su 21 Reputation points
    2021-09-11T20:09:13.377+00:00

    Using IsWow64Process(GetCurrentProcess(), &bWow64); works for me. I cannot compile 64 bit because of other dependency.

    But now I have another question:
    I tried to use intptr_t t = _wspawnlp(_P_WAIT, sExeName.c_str(), sExeName.c_str(), sHost.c_str(), NULL); to call ssh.exe to link linux machine. It didn't return the -1, which means that it already found the ssh.exe. But it return 255, and didn't call my function correctly. But if I run from dos prompt, following command works:

    ssh xxx@永爱不变 . ~/.zzz.sh

    0 comments No comments