Click here to Skip to main content
15,905,068 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I can't connect through RDP using MsTscAx. I always get succeed,even if I set wrong ip:login:password. And I have no errors during requests. I don't understand why? Can anybody help me? Code snippet:

C++
#include <Windows.h>
#include <CommCtrl.h>
#include <tchar.h>
#include "resource.h"
#include <atlbase.h>
#pragma comment(linker, \
  "\"/manifestdependency:type='Win32' "\
  "name='Microsoft.Windows.Common-Controls' "\
  "version='6.0.0.0' "\
  "processorArchitecture='*' "\
  "publicKeyToken='6595b64144ccf1df' "\
  "language='*'\"")

#pragma comment(lib, "ComCtl32.lib")
#import "C:\\Windows\\System32\\mstscax.dll"
using namespace MSTSCLib;

class CTscEventSink : public IMsTscAxEvents
{
public:
	CTscEventSink()
	{
		_ulRefs = 1;
		m_dwEvtCookie = 0;
	}

	~CTscEventSink()
	{
	}

	BOOL	Attach(IMsTscAx* pTscAx)
	{
		CComPtr<IConnectionPointContainer>	cpCPCont;
		HRESULT hr;

		hr = pTscAx->QueryInterface(IID_IConnectionPointContainer, (LPVOID *)&cpCPCont);
		if (FAILED(hr))
			return FALSE;

		hr = cpCPCont->FindConnectionPoint(__uuidof(IMsTscAxEvents), &m_cpConnect);
		if (FAILED(hr))
		{
			return FALSE;
		}

		hr = m_cpConnect->Advise( this , &m_dwEvtCookie);
		if (FAILED(hr))
		{
			m_dwEvtCookie = 0;
			return FALSE;
		}

		return TRUE;
	}

	void	Detach()
	{
		if( (m_dwEvtCookie) && (m_cpConnect) )
			m_cpConnect->Unadvise(m_dwEvtCookie);
		if(m_cpConnect)
			m_cpConnect = NULL;
	}

	//IUnknown Methods
	STDMETHOD(QueryInterface) (REFIID riid, LPVOID * ppv)
	{
		if (riid == __uuidof(IMsTscAxEvents))
		{
			*ppv = (IMsTscAxEvents *)this;
		}
		else
		{
			*ppv = NULL;
			return E_NOINTERFACE;
		}
		AddRef();
		return S_OK;
	}
   STDMETHOD_(ULONG, AddRef) (void)
   {
		InterlockedIncrement((LONG*)&_ulRefs);
		return _ulRefs;
	}
	STDMETHOD_(ULONG, Release) (void)
	{
		ULONG ulRefs = _ulRefs;
		if (InterlockedDecrement((LONG*)&_ulRefs) == 0)
		{
			delete this;
			return 0;
		}
		return _ulRefs;
	}

	HRESULT OnLoginComplete ( )
	{
		::MessageBoxA(NULL,"Login complete","",0);
	}

	HRESULT OnLogonError (
        long lError )
	{
		::MessageBoxA(NULL,"OnLogonError","",0);
	}
	HRESULT OnDisconnected (
        long discReason )
	{
		::MessageBoxA(NULL,"OnDisconnected","",0);
	}
	//IDispatch Methods

	STDMETHOD(GetTypeInfoCount)(UINT FAR* pctinfo)          
	{ return E_NOTIMPL;}

	STDMETHOD(GetTypeInfo)(UINT itinfo,LCID lcid,ITypeInfo FAR* FAR* pptinfo)   
	{ return E_NOTIMPL;}

	STDMETHOD(GetIDsOfNames)(REFIID riid,OLECHAR FAR* FAR* rgszNames,UINT cNames,
                             LCID lcid, DISPID FAR* rgdispid)                  
	{ return E_NOTIMPL;}

	STDMETHOD(Invoke)(DISPID dispidMember,REFIID riid,LCID lcid,WORD wFlags,
                      DISPPARAMS FAR* pdispparams, VARIANT FAR* pvarResult,
                      EXCEPINFO FAR* pexcepinfo,UINT FAR* puArgErr)
	{
		//AfxTrace(_T("Invoke!\n"));
		return S_OK;
	}

protected:
	ULONG		_ulRefs;

	DWORD								m_dwEvtCookie;
	CComPtr<IConnectionPoint>	m_cpConnect;

};

CTscEventSink * pCTscEventSink;
MSTSCLib::IMsRdpClient5* pInterface = NULL;

void RdpConnect()
{
	HRESULT hrInit = CoInitialize(NULL);
	if (FAILED(hrInit)) return; 

	CLSID clsid = __uuidof(MSTSCLib::MsRdpClient5);
	IID iid = __uuidof(MSTSCLib::IMsRdpClient5);

	HRESULT hrInterface = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, iid, (void**)&pInterface);
	 
	
	if (SUCCEEDED(hrInterface))
	{
		pCTscEventSink = new CTscEventSink();
		BOOL b = pCTscEventSink->Attach(pInterface);

		int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
		int iScreenHeight = GetSystemMetrics(SM_CYSCREEN);

		pInterface->PutServer(L"109.83.252.250");
		pInterface->PutFullScreen(true);
		pInterface->PutDesktopWidth(iScreenWidth);
		pInterface->PutDesktopHeight(iScreenHeight);
		pInterface->PutUserName(L"admin");
		pInterface->AdvancedSettings2->PutClearTextPassword(L"12345");
		 
		HRESULT hrConnect = pInterface->Connect();
		if (FAILED(hrConnect))
		{
			MessageBoxW(NULL,L"pInterface->Connect()", L"Error",0);
		}
		else MessageBoxW(NULL,L"pInterface->Connect() Success!!!", L"Success",0);
		 
	}
	else
	{
		wchar_t buf[16] = {0};
		_ltow(hrInterface, buf, 16);
		MessageBoxW(NULL,buf, L"CoCreateInstance failed",0);
	}
	 
}
Posted
Updated 28-Mar-17 11:16am
Comments
NullMiracle 7-Dec-13 3:27am    
@andreybandasov: Hi, I have the same problem, the Connect() function always success but the connection didn't initial.
Did You fix this ?
bitov27 1-Apr-14 10:46am    
Hi! I don't fix it, because I decide learn RDP protocol using open source realization - Rdesktop , www.rdesktop.org.

1 solution

The reason you dont see anything is because you don't implement Invoke() method, you are just returning S_OK. you have to code dispId members to tell you when server is connected, logincomplete, loginerror and etc.
 
Share this answer
 
Comments
Dave Kreskowiak 23-Mar-17 19:21pm    
You do realize that this question was asked THREE AND HALF YEARS ago? I doubt the OP is still looking for an answer.
Member 11165867 28-Mar-17 17:17pm    
Dave: i know the question is old, however i post here because someone else might be looking for solution
ankitbtrivedi 19-Jul-19 3:28am    
Thanks "Member11165867" for posting the answer. It will people like me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900