0

Is there a way of connecting to remote windows server programmatically in C?

I was looking into MSDN Remote Desktop Services API, but couldn't figure out. Can you provide me an example? I have no idea how to start with that.

NOTE

I don't want to show GUI, I just want to connect to the server without any UI interface and perform couple of tasks.

  • @SergeBallesta That's actually a great idea. –  Mar 12 '21 at 14:05
  • @SergeBallesta Just wondering is there any protocol manual? Currently I'm connecting to my remote computer with TCP, but I have no idea what data to send in order to hint the remote computer (Let's say execute a shell command). –  Mar 13 '21 at 09:28
  • 1
    By default Windows has no remote command service. You can either install an OpenSSH server, and then use an ssh client to send commands to the remote (probably the simpler way), or build from scratch a service that will use a custom protocol to encapsulate the command (probably more risky but more customizable). – Serge Ballesta Mar 13 '21 at 10:53

1 Answers1

0

You can use PowerShell remoting like described in https://theitbros.com/how-to-remotely-enable-remote-desktop-using-powershell/ to enable RDP on a remote computer.

Then you use an RDP client to connect, a client available in C is in https://github.com/rdesktop/rdesktop/blob/master/rdp.c

Alternatively you can use the command line interface of mstsc.exe from within C code (https://learn.microsoft.com/en-us/windows/win32/shell/launch), i think this is what you want as mstsc commands :

login to remote using "mstsc /admin" with password

Instead of plain RDP you can also use PowerShell sessions for remote managing :

https://learn.microsoft.com/de-de/powershell/scripting/learn/ps101/08-powershell-remoting?view=powershell-7.1#powershell-sessions and https://learn.microsoft.com/de-de/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-7.1

and also more concisely in

https://www.howtogeek.com/117192/how-to-run-powershell-commands-on-remote-computers/

Enter-PSSession -ComputerName COMPUTER -Credential USER

ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • I'm not a fan of PS, sorry. If you can update your question for C then I'll accept it ofc. –  Mar 12 '21 at 14:41