question

YongSu-3473 avatar image
0 Votes"
YongSu-3473 asked YongSu-3473 answered

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

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,

c++windows-sysinternals-procmonwindows-api-system-serviceswindows-app-sdk-general
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What happens if you use "ssh.exe" rather than "ssh"?

0 Votes 0 ·
YongSu-3473 avatar image YongSu-3473 DavidLowndes-6766 ·

II tried both, they are same.

0 Votes 0 ·

Also, show a more complete code snippet that illustrates what you're doing - you appear to be using ANSI strings with the Unicode function, but that wouldn't normally compile cleanly, so that raises the question of what is your real code?

0 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered

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);




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
1 Vote"
SimpleSamples answered RLWA32-6355 edited

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

After correcting for the character set mismatch described by @DavidLowndes-6766 and building for X64 as you suggest the OP's code will run ssh.exe. The problem is that the path environment variable for a 32-bit process does not contain the path to the OpenSSH folder (its not under SYSWOW64). Just for fun, try adding the path to the debug property page for a 32 bit build --

128793-64-bit-path-for-ssh.png


0 Votes 0 ·
YongSu-3473 avatar image
0 Votes"
YongSu-3473 answered

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@yyy . ~/.zzz.sh

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.