Vedivi Free Trial.
No setup fee. No monthly payments. No credit cards. No hassle.
| How to enable Remote Desktop programmatically |
|
There is a lot out there about enabling Remote Desktop remotely with different methods (registry or script), but not much about enabling Remote Desktop within your application (programmatically), especially for C++ developers . All we want to achieve here is Enable/Disable Remote Desktop as we would do in the System Properties.
This would be useful for admin softwares or installers that need to enable/disable Terminal Services. Let's see the other methods first.Using the RegistryThis solution is simple, all you need to do is connect to the registry on the computer you need to update. Browse to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server In the right panel, select fDenyTSConnection (REG_DWORD). Change the value data from 1 (Remote Desktop disabled) to 0 (Remote Desktop enabled). This is the most widely used solution as it is the simplest, unfortunately things are not that simple:
That is a nice hack, but too limited for a real use. Using a WMI scriptWindows Management Instrumentation (WMI) is the infrastructure for management data and operations on Windows-based operating systems. You can write WMI scripts or applications to automate administrative tasks on remote computers. This is the proper way to enable Remote Desktop as it also configures the firewall. We will use SetAllowTSConnections Method of the Win32_TerminalServiceSetting Class. The easier way to access WMI providers is through the use of WMIC To query TS Connections setting: wmic RDToggle get AllowTSConnections To set Terminal Services connections setting: wmic RDToggle where servername=”ServerName" call SetAllowTSConnections 1 We can use the WMIC global switches for the remote case: To query the TS connections setting: wmic /node:"RemoteServer" /user:"domain\AdminUser" /password:"password" To enable Terminal Services connections: wmic /node:"RemoteServer" /user:"domain\AdminUser" /password:"password" To Enable Remote Desktop on the local computer: wmic PATH win32_terminalservicesetting WHERE (__Class!="") CALL That is a good solution, all we need to do now is see how to do this in C++ rather than in a command line. Using VC++To create an application for WMI using C++: you must initialize COM, access and set WMI protocols, and perform a manual cleanup. However, C++ does have the advantage of flexibility and power. Here are the steps involved:1) HeaderYou need to add the following references and #include statements to compile correctly.#define _WIN32_DCOM 2) Initialize COMBecause WMI is based on COM technology, you must perform calls to the CoInitializeEx and CoInitializeSecurity functions to access WMI.// Initialize COM. Note: To connect to the \root\CIMV2\TerminalServices namespace, the authentication level must include packet privacy. For C/C++ calls, this is an authentication level of RPC_C_AUTHN_LEVEL_PKT_PRIVACY. 3) Create a connection to a WMI namespaceBy definition, WMI runs in a different process than your application. Therefore, you must create a connection between your application and WMI.Note: To connect to a remote computer, for example Computer_B, use the following parameters: _bstr_t(L"\\COMPUTER_B\ROOT\\CIMV2") // WMI namespaceHere is the main code: IWbemLocator *pLocator = NULL; Note: The code above works for Win2k3/XP, for Vista, just update the namespace from "root/cimv2" to "root/cimv2/TerminalServices". 4) Set the security levels on the WMI connectionTo use the connection you create to WMI, you must set the impersonation and authentication levels for your application.if (SUCCEEDED(hr)) 5) Make the WMI RequestLocate Win32_TerminalServiceSettings using IWbemServices.IEnumWbemClassObject* pEnumerator = NULL; 6) Check and Update TerminalServiceSettingsCheck the current value of AllowTSConnection flag and call SetAllowTSConnection.IWbemClassObject * pObject = NULL; 7) Cleanup and shutdownDestroy all COM pointers and shut down your application correctly.if(pLocator != NULL) ConclusionEnabling Remote Desktop locally or remotely is of great use for administrators or to automate installers (NSIS for instance). But it needs to be done properly (not just with the registry settings). WMI is the best way to achieve this and you I have described how to achieve this in VC++. You can find attached the full .cpp file of the example I will probably be discussing here about a plugin for NSIS if people are interrested. |
| Last Updated on Saturday, 13 June 2009 09:03 |
Vedivi Business is the latest generation of secure remote access solution, it combines a VPN with Remote Desktop and Remote Assistance integration.
With Vedivi you can:
Get started with Vedivi 30-Day free trial so you can see for yourself why so many businesses trust Vedivi for VPN & remote access.