| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322 |
- #include "stdafx.h"
- #include "stdafx2.h"
- #include "SpWare.h"
- #include <gdiplus.h>
- #include "associated.h"
- #include <comutil.h>
- #include <commctrl.h>
- #pragma comment(lib, "Gdiplus.lib")
- #pragma comment(lib, "comsuppwd.lib")
- #pragma comment(lib, "Comctl32.lib")
- #ifdef MOD_FREERDPWRAP_EXPORTS
- #include "..\..\Module\mod_FreeRDPWrap\FreeRDPWrapFSM.h"
- CFreeRDPWrapFSM* pFsm = NULL;
- #define TITLE_NAME "FreeRDPWrap Host"
- #ifdef TWINKLE_LOCAL_DEBUG
- #define Display(fmt, ...) \
- Dbg("< SpWare> :" fmt, ##__VA_ARGS__)/*; \
- printf("< SpWare> :" fmt "\n", ##__VA_ARGS__)*/
- #else
- #define Display(fmt, ...) \
- Dbg("< SpWare> :" fmt, ##__VA_ARGS__)
- #endif
- #else
- #include "..\..\Module\mod_FreeRDPWrap\Logger.h"
- #define TITLE_NAME "SpDeskShare Host"
- #ifdef TWINKLE_LOCAL_DEBUG
- #define Display(fmt, ...) \
- printf("<RDPWarer> :" fmt "\n", ##__VA_ARGS__); \
- WRITE_DEBUG_PARAM(fmt, ##__VA_ARGS__)
- #else
- #define Display(fmt, ...) \
- WRITE_DEBUG_PARAM(fmt, ##__VA_ARGS__)
- #endif
- #endif
- #define FULL_SCREEN
- #define REG_MSG_DESKSHARE_FLAG "{5D709B21-6BBF-468B-8FC1-6E88E9F6A571}"
- UINT WM_EXIT_MSG = ::RegisterWindowMessageA(REG_MSG_DESKSHARE_FLAG);
- #define DESKSHARE_FLAG_WIN 1 //我的同屏窗口状态wParam
- #define FLAG_WIN_SHOW 0 //显示窗口 <!--lparam/>
- #define FLAG_WIN_HIDE 1 //隐藏窗口 <!--lparam/>
- static int s_nfullWidth = GetSystemMetrics(SM_CXSCREEN);
- static int s_nfullHeigh = GetSystemMetrics(SM_CYSCREEN);
- /******
- Remote Desktop Sharing
- Athenian & wap2k - Rohitab forums
- http://www.rohitab.com
- ******/
- /*THE CLIENT (VIEWER)*/
- #define _CRT_SECURE_NO_WARNINGS
- //this is to prevent noobs and total idiots like you from using this masterpiece!
- using namespace Gdiplus;
- /*GLOBALS*/
- #define APP MAKEINTRESOURCE(101)
- #define APPSMALL MAKEINTRESOURCE(102)
- #define MAX_ATTENDEE 1
- #define override
- HWND g_hWareHwnd = NULL;
- char inviteFileName[MAX_PATH] = {0};
- USHORT usCtrlLevel = CTRL_LEVEL_VIEW;
- HWND hWarerWnd = NULL;
- HWND startSharing, stopSharing;
- HWND hwndConnect, hwndDisconnect, view, ACTIVEX_WINDOW, levelList;
- LRESULT CALLBACK WareWndPro(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK scrollProc(HWND, UINT, WPARAM, LPARAM);
- static char className[] = "SPWARE-RDP";
- static HINSTANCE instance = NULL;
- static LONG s_nAttendees = 0;
- IRDPSRAPIViewer *viewer = NULL;
- IRDPSRAPISharingSession *session = NULL;
- IRDPSRAPIApplicationFilter *filter = NULL;
- IRDPSRAPIApplicationList *appList = NULL;
- IRDPSRAPIInvitationManager *invitationManager = NULL;
- IRDPSRAPIInvitation *invitation = NULL;
- IRDPSRAPIAttendeeManager *attendeeManager = NULL;
- IRDPSRAPIAttendee *attendee = NULL;
- IConnectionPointContainer* viewer_picpc = NULL;
- IConnectionPoint* viewer_picp = NULL;
- IConnectionPointContainer* sharer_picpc = NULL;
- IConnectionPoint* sharer_picp = NULL;
- SCROLLINFO si;
- int vs = 0;
- int vRemeber = NULL;
- int sRemeber = NULL;
- CRITICAL_SECTION csSession;
- int ConnectEvent(IUnknown* Container, REFIID riid, IUnknown* Advisor, IConnectionPointContainer** picpc, IConnectionPoint** picp)
- {
- HRESULT hr = 0;
- unsigned long tid = 0;
- IConnectionPointContainer* icpc = 0;
- IConnectionPoint* icp = 0;
- *picpc = 0;
- *picp = 0;
- Container->QueryInterface(IID_IConnectionPointContainer, (void **)&icpc);
- if (icpc)
- {
- *picpc = icpc;
- icpc->FindConnectionPoint(riid, &icp);
- if (icp)
- {
- *picp = icp;
- hr = icp->Advise(Advisor, &tid);
- //icp->Release();
- }
- //icpc->Release();
- }
- //Container->Release();
- return tid;
- }
- void DisconnectEvent(IConnectionPointContainer* icpc, IConnectionPoint* icp, unsigned int Cookie)
- {
- Display("Enter DisconnectEvent");
- unsigned long hr = 0;
- if(icp != nullptr) {
- icp->Unadvise(Cookie);
- icp->Release();
- }
- if(icpc != nullptr)
- icpc->Release();
- Display("Leave DisconnectEvent");
- }
- /*Event function prototypes*/
- /*viewer section*/
- void OnConnectionFailed();
- void OnConnectionInterrupt();
- void OnConnectionEstablished();
- /*sharer section*/
- void OnAttendeeConnected(IDispatch *pAttendee);
- void OnAttendeeDisconnected(IDispatch *pAttendee);
- void OnControlLevelChangeRequest(IDispatch *pAttendee, CTRL_LEVEL RequestedLevel);
- #pragma region [4/12/2018 10:34 Gifur]
- /*
- class EventSink : public _IRDPSessionEvents {
- public:
- EventSink(){
- }
- ~EventSink(){
- }
- // IUnknown
- virtual HRESULT STDMETHODCALLTYPE override QueryInterface(
- REFIID iid, void**ppvObject){
- *ppvObject = 0;
- if (iid == IID_IUnknown || iid == IID_IDispatch || iid == __uuidof(_IRDPSessionEvents))
- *ppvObject = this;
- if (*ppvObject)
- {
- ((IUnknown*)(*ppvObject))->AddRef();
- return S_OK;
- }
- return E_NOINTERFACE;
- }
- virtual ULONG STDMETHODCALLTYPE override AddRef(void){
- return 0;
- }
- virtual ULONG STDMETHODCALLTYPE override Release(void){
- return 0;
- }
- // IDispatch
- virtual HRESULT STDMETHODCALLTYPE override GetTypeInfoCount(
- __RPC__out UINT *pctinfo){
- return E_NOTIMPL;
- }
- virtual HRESULT STDMETHODCALLTYPE override GetTypeInfo(
- UINT iTInfo,
- LCID lcid,
- __RPC__deref_out_opt ITypeInfo **ppTInfo){
- return E_NOTIMPL;
- }
- virtual HRESULT STDMETHODCALLTYPE override GetIDsOfNames(
- __RPC__in REFIID riid,
- __RPC__in_ecount_full(cNames) LPOLESTR *rgszNames,
- UINT cNames,
- LCID lcid,
- __RPC__out_ecount_full(cNames) DISPID *rgDispId){
- return E_NOTIMPL;
- }
- virtual HRESULT STDMETHODCALLTYPE override EventSink::Invoke(
- DISPID dispIdMember,
- REFIID riid,
- LCID lcid,
- WORD wFlags,
- DISPPARAMS FAR* pDispParams,
- VARIANT FAR* pVarResult,
- EXCEPINFO FAR* pExcepInfo,
- unsigned int FAR* puArgErr){
- switch (dispIdMember){
- case DISPID_RDPSRAPI_EVENT_ON_VIEWER_CONNECTFAILED:
- OnConnectionFailed();
- break;
- case DISPID_RDPSRAPI_EVENT_ON_VIEWER_CONNECTED:
- OnConnectionEstablished();
- break;
- case DISPID_RDPSRAPI_EVENT_ON_ATTENDEE_CONNECTED:
- OnAttendeeConnected(pDispParams->rgvarg[0].pdispVal);
- break;
- case DISPID_RDPSRAPI_EVENT_ON_ATTENDEE_DISCONNECTED:
- OnAttendeeDisconnected(pDispParams->rgvarg[0].pdispVal);
- break;
- case DISPID_RDPSRAPI_EVENT_ON_CTRLLEVEL_CHANGE_REQUEST:
- OnControlLevelChangeRequest(pDispParams->rgvarg[1].pdispVal, (CTRL_LEVEL)pDispParams->rgvarg[0].intVal);
- break;
- }
- return S_OK;
- }
- };
- EventSink ev;
- */
- #pragma endregion
- class SharerEvtSink : public _IRDPSessionEvents {
- public:
- SharerEvtSink(){
- }
- ~SharerEvtSink(){
- }
- // IUnknown
- virtual HRESULT STDMETHODCALLTYPE override QueryInterface(
- REFIID iid, void**ppvObject){
- *ppvObject = 0;
- if (iid == IID_IUnknown || iid == IID_IDispatch || iid == __uuidof(_IRDPSessionEvents))
- *ppvObject = this;
- if (*ppvObject)
- {
- ((IUnknown*)(*ppvObject))->AddRef();
- return S_OK;
- }
- return E_NOINTERFACE;
- }
- virtual ULONG STDMETHODCALLTYPE override AddRef(void){
- return 0;
- }
- virtual ULONG STDMETHODCALLTYPE override Release(void){
- return 0;
- }
- // IDispatch
- virtual HRESULT STDMETHODCALLTYPE override GetTypeInfoCount(
- __RPC__out UINT *pctinfo){
- return E_NOTIMPL;
- }
- virtual HRESULT STDMETHODCALLTYPE override GetTypeInfo(
- UINT iTInfo,
- LCID lcid,
- __RPC__deref_out_opt ITypeInfo **ppTInfo){
- return E_NOTIMPL;
- }
- virtual HRESULT STDMETHODCALLTYPE override GetIDsOfNames(
- __RPC__in REFIID riid,
- __RPC__in_ecount_full(cNames) LPOLESTR *rgszNames,
- UINT cNames,
- LCID lcid,
- __RPC__out_ecount_full(cNames) DISPID *rgDispId){
- return E_NOTIMPL;
- }
- virtual HRESULT STDMETHODCALLTYPE override SharerEvtSink::Invoke(
- DISPID dispIdMember,
- REFIID riid,
- LCID lcid,
- WORD wFlags,
- DISPPARAMS FAR* pDispParams,
- VARIANT FAR* pVarResult,
- EXCEPINFO FAR* pExcepInfo,
- unsigned int FAR* puArgErr){
- switch (dispIdMember){
- case DISPID_RDPSRAPI_EVENT_ON_ATTENDEE_CONNECTED:
- OnAttendeeConnected(pDispParams->rgvarg[0].pdispVal);
- break;
- case DISPID_RDPSRAPI_EVENT_ON_ATTENDEE_DISCONNECTED:
- OnAttendeeDisconnected(pDispParams->rgvarg[0].pdispVal);
- break;
- case DISPID_RDPSRAPI_EVENT_ON_CTRLLEVEL_CHANGE_REQUEST:
- OnControlLevelChangeRequest(pDispParams->rgvarg[1].pdispVal, (CTRL_LEVEL)pDispParams->rgvarg[0].intVal);
- break;
- default:
- Display("SharerEvtSink: %d", dispIdMember);
- break;
- }
- return S_OK;
- }
- };
- SharerEvtSink sEv;
- class ViewerEvtSink : public _IRDPSessionEvents {
- public:
- ViewerEvtSink(){
- }
- ~ViewerEvtSink(){
- }
- // IUnknown
- virtual HRESULT STDMETHODCALLTYPE override QueryInterface(
- REFIID iid, void**ppvObject){
- *ppvObject = 0;
- if (iid == IID_IUnknown || iid == IID_IDispatch || iid == __uuidof(_IRDPSessionEvents))
- *ppvObject = this;
- if (*ppvObject)
- {
- ((IUnknown*)(*ppvObject))->AddRef();
- return S_OK;
- }
- return E_NOINTERFACE;
- }
- virtual ULONG STDMETHODCALLTYPE override AddRef(void){
- return 0;
- }
- virtual ULONG STDMETHODCALLTYPE override Release(void){
- return 0;
- }
- // IDispatch
- virtual HRESULT STDMETHODCALLTYPE override GetTypeInfoCount(
- __RPC__out UINT *pctinfo){
- return E_NOTIMPL;
- }
- virtual HRESULT STDMETHODCALLTYPE override GetTypeInfo(
- UINT iTInfo,
- LCID lcid,
- __RPC__deref_out_opt ITypeInfo **ppTInfo){
- return E_NOTIMPL;
- }
- virtual HRESULT STDMETHODCALLTYPE override GetIDsOfNames(
- __RPC__in REFIID riid,
- __RPC__in_ecount_full(cNames) LPOLESTR *rgszNames,
- UINT cNames,
- LCID lcid,
- __RPC__out_ecount_full(cNames) DISPID *rgDispId){
- return E_NOTIMPL;
- }
- virtual HRESULT STDMETHODCALLTYPE override ViewerEvtSink::Invoke(
- DISPID dispIdMember,
- REFIID riid,
- LCID lcid,
- WORD wFlags,
- DISPPARAMS FAR* pDispParams,
- VARIANT FAR* pVarResult,
- EXCEPINFO FAR* pExcepInfo,
- unsigned int FAR* puArgErr){
- switch (dispIdMember){
- case DISPID_RDPSRAPI_EVENT_ON_VIEWER_CONNECTFAILED:
- OnConnectionFailed();
- break;
- case DISPID_RDPSRAPI_EVENT_ON_VIEWER_CONNECTED:
- OnConnectionEstablished();
- break;
- case DISPID_RDPSRAPI_EVENT_ON_VIEWER_DISCONNECTED:
- OnConnectionInterrupt();
- break;
- default:
- Display("ViewerEvtSink: %d", dispIdMember);
- break;
- }
- return S_OK;
- }
- };
- ViewerEvtSink vEv;
- void GDIPLUS(HDC hdc){
- Graphics graphics(hdc);
- FontFamily family(L"Verdana");
- Font font(&family, 15, FontStyleRegular, UnitPixel);
- SolidBrush bBrush(Color(255, 0, 100, 200));
- // Fill the rectangle.
- graphics.FillRectangle(&bBrush, 0, 0, 150, 260);
- }
- BOOL bIsZoomed, bFullScreen = FALSE;
- DWORD ghwndStyle = (WS_OVERLAPPEDWINDOW | WS_SYSMENU | WS_VISIBLE);
- RECT rcPrevious;
- WINDOWPLACEMENT gOldPlacement;
- WINDOWPLACEMENT gNewPlacement;
- void HideMenuBar()
- {
- #ifndef FULL_SCREEN
- if (GetWindowLong(g_hWareHwnd, GWL_STYLE) & WS_OVERLAPPEDWINDOW)
- {
- Display("HideMenuBar@1");
-
- GetWindowPlacement(view, &gOldPlacement);
- GetWindowRect(g_hWareHwnd, &rcPrevious);
- int nfw = GetSystemMetrics(SM_CXSCREEN);
- int nfh = GetSystemMetrics(SM_CYSCREEN);
-
- // save window state
- //Judge whether the dialog is in maxshow status
- bIsZoomed = IsZoomed(g_hWareHwnd);
- SetMenu(g_hWareHwnd, NULL);
- //restore the dialog to its original size and position.
- if (bIsZoomed) ShowWindow(g_hWareHwnd, SW_RESTORE);
- SetWindowLong(g_hWareHwnd, GWL_STYLE, WS_POPUP);
- //ShowCursor(FALSE);
- SetThreadExecutionState(ES_CONTINUOUS //informs the system that the state should be remained
- | ES_SYSTEM_REQUIRED // Forces the system to be reseting the display idle timer.
- | ES_DISPLAY_REQUIRED // Forces the display to be reseting the display idle timer.
- );
- Display("w:%d, h:%d", nfw, nfh);
- MoveWindow(g_hWareHwnd, 0, 0, nfw, nfh, TRUE);
- ShowWindow(g_hWareHwnd, SW_RESTORE);
- MoveWindow(view, 0, 0, nfw, nfh, TRUE);
- bFullScreen = TRUE;
- }
- else
- {
- Display("HideMenuBar@2");
- ShowWindow(g_hWareHwnd, SW_RESTORE);
- SetWindowLong(g_hWareHwnd, GWL_STYLE, ghwndStyle);
- if (bIsZoomed) ShowWindow(g_hWareHwnd, SW_MAXIMIZE);
- //ShowCursor(TRUE);
- SetThreadExecutionState(ES_CONTINUOUS);
- // Trigger WM_SIZE message
- RECT& Rc = rcPrevious;
- //GetWindowRect(g_hWareHwnd, &Rc);
- SetWindowPos(g_hWareHwnd, HWND_TOPMOST, Rc.left, Rc.top,
- (Rc.right-Rc.left),(Rc.bottom-Rc.top), SWP_FRAMECHANGED);
- SetWindowPlacement(view, &gOldPlacement);
- bFullScreen = FALSE;
- }
- #endif
- }
- void ShowWareWindow(BOOL bShow = TRUE) {
- DWORD dwREcipents = BSM_APPLICATIONS | BSM_ALLDESKTOPS;
- if(bShow) {
- Display("topmostex %d, %d", s_nfullWidth, s_nfullHeigh);
- //SetWindowPos(g_hWareHwnd, HWND_TOPMOST, 0, 0, s_nfullWidth, s_nfullHeigh, TRUE);
- SetWindowPos(g_hWareHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
- //ShowWindow(g_hWareHwnd, 1);
- Display("TopMost or not: %d", !!(GetWindowLong(g_hWareHwnd, GWL_EXSTYLE) & WS_EX_TOPMOST));
- #ifndef MOD_FREERDPWRAP_EXPORTS
- if(0 >= BroadcastSystemMessageA(BSF_POSTMESSAGE | BSF_FORCEIFHUNG, &dwREcipents, WM_EXIT_MSG, DESKSHARE_FLAG_WIN, FLAG_WIN_SHOW)) {
- Display("BroadcastSystemMessageA failed %d", GetLastError());
- }
- #endif
- }
- else {
- Display("Set not topmost!");
- SetWindowPos(g_hWareHwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE);
- #ifndef MOD_FREERDPWRAP_EXPORTS
- if(0 >= BroadcastSystemMessageA(BSF_POSTMESSAGE | BSF_FORCEIFHUNG, &dwREcipents, WM_EXIT_MSG, DESKSHARE_FLAG_WIN, FLAG_WIN_HIDE)) {
- Display("BroadcastSystemMessageA failed %d", GetLastError());
- }
- #endif
- }
- Display("Leave ShowWareWindow(%d)", bShow);
- }
- bool CheckFilePath(const char *path){
- FILE *check = NULL;
- fopen_s(&check, path, "r");
- if (check){
- //check for size 0
- fseek(check, 0, SEEK_END);
- int size = ftell(check);
- rewind(check);
- if (size > 0){
- fclose(check);
- return true;
- }
- else{
- fclose(check);
- return false;
- }
- }
- return false;
- }
- bool ViewerConnect(const char* invite_file_path, DWORD select_id = 0)
- {
- HRESULT hr = S_OK;
- Display("Invitation file path: %s", invite_file_path);
- if (invite_file_path){
- if (CheckFilePath(invite_file_path)) {
- Display("Invitation file's integrity checked!");
- //__ComInvokeEventHandler()
- //To prevent another start procedure
- if (viewer == NULL){
- if ((hr = CoCreateInstance(__uuidof(RDPViewer),
- NULL, CLSCTX_INPROC_SERVER,
- __uuidof(IRDPSRAPIViewer),
- (void**)&viewer)) == S_OK) {
- Display("Instance created!");
- vRemeber = ConnectEvent((IUnknown*)viewer, __uuidof(_IRDPSessionEvents), (IUnknown*)&vEv, &viewer_picpc, &viewer_picp);
- Display("Start to read invitation file!");
- FILE *read = NULL;
- fopen_s(&read, invite_file_path, "r");
- if (read) {
- fseek(read, 0, SEEK_END);
- int flen = ftell(read);
- if (flen <= 0) {
- fclose(read);
- Display("Invalid inviation file !");
- return false;
- }
- char* inviteString = new char[flen + 1];
- if (inviteString == NULL) {
- Display("allocate buffer for file content failed !");
- fclose(read);
- return false;
- }
- ZeroMemory(inviteString, sizeof(char)*(flen + 1));
- fseek(read, 0, SEEK_SET);
- fgets(inviteString, flen, read);
- inviteString[flen] = '\0';
- fclose(read);
- if ((hr = viewer->Connect(B(inviteString), RVC_RDP_AUTH, B(""))) == S_OK) {
- Display("Connection line active!");
- delete[] inviteString;
- inviteString = NULL;
- return true;
- }
- else{
- delete[] inviteString;
- inviteString = NULL;
- Display("Connection line error, hr=0x%X GLE=%u !", hr, GetLastError());
- return false;
- }
- }
- else{
- Display("Error reading invitation file !");
- return false;
- }
- }
- else{
- Display("Error creating instance, hr=0x%X GLE=%u !", hr, GetLastError());
- return false;
- }
- }
- else{
- Display("Error starting: viewer already exists!");
- return false;
- }
- }
- else{
- Display("Invalid invitation file!");
- return false;
- }
- }
- else{
- Display("Error: An invitation file must be selected!");
- return false;
- }
- }
- void ViewerDisconnect()
- {
- Display("Disconnecting...0x%X", GetCurrentThreadId());
- if (viewer){
- DisconnectEvent(viewer_picpc, viewer_picp, vRemeber);
- viewer->Disconnect();
- viewer->Release();
- viewer = NULL;
- Display("Disconnected!");
- }
- else {
- Display("High warning at disconnecting: No active connection!");
- }
- }
- BOOL FilterApplication(long filterid, const char* filtr_name)
- {
- BOOL bFound = FALSE;
- HRESULT hr = S_OK;
- IRDPSRAPIApplicationList* list = NULL;
- if (session) {
- if ((hr = session->get_ApplicationFilter(&filter)) == S_OK) {
- hr = filter->put_Enabled(TRUE);
- if (hr != S_OK) {
- Display("Set enable for application filter failed, 0x%X", hr);
- return FALSE;
- }
- //list of objects in the session that can be shared.
- hr = filter->get_Applications(&list);
- if (hr != S_OK) {
- Display("get applicaion list from application filter failed, hr = 0x%X", hr);
- return FALSE;
- }
- for (int i = 0; i < 102400; i++) {
- IRDPSRAPIApplication* app = NULL;
- hr = list->get_Item(i, &app);
- if (hr != S_OK) {
- //WRITE_WARN_PARAM("get applicaion item(%d) from application list failed, hr = 0x%X", i, hr);
- continue;
- }
- if (app != NULL) {
- long id;
- BSTR bstrAppName;
- app->get_Id(&id);
- app->get_Name(&bstrAppName);
- if (filterid != 0 && id == filterid) {
- if(app->put_Shared(VARIANT_TRUE) == S_OK) {
- bFound = TRUE;
- }
- break;
- }
- _bstr_t b = bstrAppName;
- if(filtr_name != NULL && !_stricmp(filtr_name, (LPCTSTR)b)) {
- if(app->put_Shared(VARIANT_TRUE) == S_OK) {
- bFound = TRUE;
- }
- break;
- }
- Display("%d -- %s", id, (LPCTSTR)b);
- }
- }
- }
- else {
- Display("fetch application filter failed, hr = 0x%X !", hr);
- return FALSE;
- }
- }
- return bFound;
- }
- BOOL ShareAndInvite(LPCTSTR lpcszInviteFilePath, DWORD dwFilterId)
- {
- HRESULT hr = S_OK;
- if(session != NULL) {
- Display("Error starting: Session already exists!");
- //prevent from reporting error result in extreme situation. [4/16/2018 9:59 @Gifur]
- //Display("Destroy the previous one.");
- //SharerDisconnect();
- return FALSE;
- }
- if ((hr = CoCreateInstance(__uuidof(RDPSession),
- NULL, CLSCTX_INPROC_SERVER,
- __uuidof(IRDPSRAPISharingSession),
- (void**)&session)) != S_OK) {
- Display("Error creating instance, hr=0x%X GLE=%u !", hr, GetLastError());
- goto Error;
- }
- Display("Instance created !");
- sRemeber = ConnectEvent((IUnknown*)session, __uuidof(_IRDPSessionEvents), (IUnknown*)&sEv, &sharer_picpc, &sharer_picp);
- if ((hr = session->Open()) != S_OK) {
- Display("Error opening session, hr=0x%X GLE=%u !", hr, GetLastError());
- goto Error;
- }
- Display("Session opened!");
- if(s_nfullHeigh > 0 && s_nfullWidth > 0) {
- session->SetDesktopSharedRect(0, 0, s_nfullWidth, s_nfullHeigh);
- }
- if(dwFilterId != 0) {
- if(!FilterApplication((long)dwFilterId, NULL)) {
- Display("Error filter application id");
- goto Error;
- }
- }
- if (session->get_Invitations(&invitationManager) != S_OK) {
- Display("Get invitations error, hr=0x%X GLE=%u !", hr, GetLastError());
- goto Error;
- }
-
- Display("Get invitations ok!");
- if ((hr = invitationManager->CreateInvitation(
- RVC_RDP_AUTH,
- RVC_RDP_GROUP,
- B(""),
- MAX_ATTENDEE,
- &invitation)) != S_OK) {
- Display("Error obtaining invitation, hr=0x%X GLE=%u !", hr, GetLastError());
- goto Error;
- }
- Display("Invitation obtained!");
- FILE *invite = NULL;
- fopen_s(&invite, lpcszInviteFilePath, "w");
- if(!invite) {
- Display("fopen file(%s) failed.", lpcszInviteFilePath);
- goto Error;
- }
- BSTR inviteString;
- if ((hr = invitation->get_ConnectionString(&inviteString)) == S_OK) {
- _bstr_t b = inviteString;
- char* lpszInvite = b;
- fprintf_s(invite, "%s", lpszInvite);
- Display("Invitation written to file!");
- Display("invite_file_path: %s", lpcszInviteFilePath);
- SysFreeString(inviteString);
- }
- fclose(invite);
- if ((hr = session->get_Attendees(&attendeeManager)) == S_OK) {
- Display("Get Attendees ok, Waitting for attandee !");
- }
- return TRUE;
- Error:
- if(invitation) {
- invitation->Release();
- invitation = NULL;
- }
- if(invitationManager) {
- invitationManager->Release();
- invitationManager = NULL;
- }
- if (session){
- session->Close();
- while(session->Release() > 0) {
- ;
- }
- session = NULL;
- }
- return FALSE;
- }
- void SharerDisconnect()
- {
- EnterCriticalSection(&csSession);
- Display("Start to stop the invitation session... 0x%X", GetCurrentThreadId());
- if(invitation) {
- invitation->Release();
- invitation = NULL;
- }
- if(invitationManager) {
- invitationManager->Release();
- invitationManager = NULL;
- }
- if (session){
- DisconnectEvent(sharer_picpc, sharer_picp, sRemeber);
- session->Close();
- while(session->Release() > 0){
- Display("amazing!");
- }
- session = NULL;
- Display("Session stopped succ!");
- }
- else {
- Display("Warning at stopping: No active session!");
- }
- Display("Current attendee count: %ld", s_nAttendees);
- if(s_nAttendees > 0) {
- InterlockedExchange(&s_nAttendees, 0);
- }
- LeaveCriticalSection(&csSession);
- }
- void OnAttendeeConnected(IDispatch *pAttendee)
- {
- IRDPSRAPIAttendee *pRDPAtendee;
- pAttendee->QueryInterface(__uuidof(IRDPSRAPIAttendee), (void**)&pRDPAtendee);
- pRDPAtendee->put_ControlLevel((CTRL_LEVEL)usCtrlLevel);
- Display("An attendee connected!");
- InterlockedIncrement(&s_nAttendees);
- if(s_nAttendees == 1) {
- #ifdef MOD_FREERDPWRAP_EXPORTS
- pFsm->AttendeeConnected();
- #endif
- }
- }
- void OnAttendeeDisconnected(IDispatch *pAttendee){
- IRDPSRAPIAttendeeDisconnectInfo *info;
- ATTENDEE_DISCONNECT_REASON reason;
- pAttendee->QueryInterface(__uuidof(IRDPSRAPIAttendeeDisconnectInfo), (void**)&info);
- if (info->get_Reason(&reason) == S_OK){
- char *textReason = NULL;
- switch (reason){
- case ATTENDEE_DISCONNECT_REASON_APP:
- textReason = "Viewer terminated session!";
- break;
- case ATTENDEE_DISCONNECT_REASON_ERR:
- textReason = "Internal Error!";
- break;
- case ATTENDEE_DISCONNECT_REASON_CLI:
- textReason = "Attendee requested termination!";
- break;
- default:
- textReason = "Unknown reason!";
- }
- Display("Attendee disconnected!");
- Display("Reason: %s", textReason);
- }
- pAttendee->Release();
- //sharer_picp = 0;
- //sharer_picpc = 0;
- InterlockedDecrement(&s_nAttendees);
- if(s_nAttendees == 0) {
- //SharerDisconnect();
- PostMessage(g_hWareHwnd, WM_TERMINATE_INVITE, 0, 0);
- #ifdef MOD_FREERDPWRAP_EXPORTS
- pFsm->AttendeeDisconnect();
- #endif
- Display("After SharerDisconnect() in event");
- }
- }
- void OnControlLevelChangeRequest(IDispatch *pAttendee, CTRL_LEVEL RequestedLevel){
- IRDPSRAPIAttendee *pRDPAtendee;
- pAttendee->QueryInterface(__uuidof(IRDPSRAPIAttendee), (void**)&pRDPAtendee);
- if (pRDPAtendee->put_ControlLevel(RequestedLevel) == S_OK){
- switch (RequestedLevel){
- case CTRL_LEVEL_NONE:
- Display("Level changed to CTRL_LEVEL_NONE!");
- break;
- case CTRL_LEVEL_VIEW:
- Display("Level changed to CTRL_LEVEL_VIEW!");
- break;
- case CTRL_LEVEL_INTERACTIVE:
- Display("Level changed to CTRL_LEVEL_INTERACTIVE!");
- break;
- }
- }
- }
- HWND CreateButton(LPCSTR lpButtonName, HWND hWnd, int x, int y){
- return CreateWindow("button", lpButtonName, WS_EX_TRANSPARENT | BS_OWNERDRAW | WS_CHILD | WS_VISIBLE, x, y, 100, 30, hWnd, 0, (HINSTANCE)hWnd, 0);
- }
- int WINAPI WinMainEntry(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- WNDCLASSEX WndClass;
- MSG Msg;
- int nfw = 0;
- int nfh = 0;
- instance = hInstance;
- AXRegister();
- INITCOMMONCONTROLSEX icex = { 0 };
- InitCommonControls();
- WndClass.cbSize = sizeof(WNDCLASSEX);
- WndClass.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_PARENTDC;
- WndClass.lpfnWndProc = WareWndPro;
- WndClass.cbClsExtra = 0;
- WndClass.cbWndExtra = 0;
- WndClass.hInstance = instance;
- WndClass.hIcon = LoadIcon(hInstance, APP);
- WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
- WndClass.hbrBackground = CreateSolidBrush(RGB(245, 247, 248));
- WndClass.lpszMenuName = 0;
- WndClass.lpszClassName = className;
- WndClass.hIconSm = LoadIcon(hInstance, APPSMALL);
- RegisterClassEx(&WndClass);
- #ifndef FULL_SCREEN
- nfw = 1200;
- nfh = 600;
- #else
- nfw = GetSystemMetrics(SM_CXSCREEN);
- nfh = GetSystemMetrics(SM_CYSCREEN);
- #endif
-
- hWarerWnd = CreateWindowExA(
- WS_EX_TOPMOST,
- className,
- TITLE_NAME,
- WS_OVERLAPPEDWINDOW | WS_POPUP,
- CW_USEDEFAULT, CW_USEDEFAULT,
- nfw, nfh,
- NULL, NULL,
- instance,
- NULL);
- RECT wInfo;
- GetClientRect(hWarerWnd, &wInfo);
- int Width = wInfo.right;
- int Height = wInfo.bottom;
- #ifndef FULL_SCREEN
- hwndConnect = CreateButton("Connect", hWarerWnd, 20, 20);
- hwndDisconnect = CreateButton("Disconnect", hWarerWnd, 20, 80);
- startSharing = CreateButton("Start Sharing", hWarerWnd, 20, 140);
- stopSharing = CreateButton("Stop Sharing", hWarerWnd, 20, 200);
- view = CreateWindowA("edit", 0,
- WS_CLIPCHILDREN | WS_CHILD | WS_VISIBLE,
- 160, 5, Width - 160, Height - 5, hWarerWnd, 0, instance, 0);
- SetWindowLong(hWarerWnd, GWL_STYLE, GetWindowLong(hWarerWnd, GWL_STYLE) &~WS_MAXIMIZEBOX);
- ShowWindow(hWarerWnd, 1);
- UpdateWindow(hWarerWnd);
- SendMessage(view, EM_SETREADONLY, 1, 0);
- int hotkeyID = GlobalAddAtom("MyHotkey");
- RegisterHotKey(hWarerWnd, 99, MOD_ALT, VK_F8);
- #else
- //view = CreateWindowA("edit", 0,
- // WS_CLIPCHILDREN | WS_CHILD | WS_VISIBLE,
- // 0, 0, Width, Height, hWarerWnd, 0, instance, 0);
- SetWindowLong(hWarerWnd, GWL_STYLE, WS_POPUP);
- SetWindowPos(hWarerWnd, HWND_TOPMOST, 0, 0, nfw, nfh, SWP_HIDEWINDOW);
- #endif
- g_hWareHwnd = hWarerWnd;
- while (GetMessage(&Msg, NULL, 0, 0)) {
- TranslateMessage(&Msg);
- DispatchMessage(&Msg);
- }
- Display("Out of GetMessage.");
- return Msg.wParam;
- }
- void RDPWarer_SetReference(PVOID param)
- {
- #ifdef MOD_FREERDPWRAP_EXPORTS
- pFsm = reinterpret_cast<CFreeRDPWrapFSM*>(param);
- LOG_ASSERT(pFsm != NULL);
- #else
- UNREFERENCED_PARAMETER(param);
- #endif
- }
- void PutStreamOnWindow(HWND hh){
- // While looking at the calls made I have never seen this create window called it is always returns 0 and is recreated
- ACTIVEX_WINDOW = CreateWindowEx(0, "AX", "{32be5ed2-5c86-480f-a914-0ff8885a1b3f}", WS_CHILD | WS_VISIBLE, 0, 20, 1, 1, hh, 0, instance, 0);
- if (ACTIVEX_WINDOW){
- IUnknown*a = 0;
- SendMessage(ACTIVEX_WINDOW, AX_QUERYINTERFACE, (WPARAM)&__uuidof(IUnknown*), (LPARAM)&a);
- }
- else {
- ACTIVEX_WINDOW = CreateWindow("AX", "}32BE5ED2-5C86-480F-A914-0FF8885A1B3F}", WS_CHILD | WS_VISIBLE, 0, 0, 1, 1, hh, 0, instance, 0);
- SendMessage(ACTIVEX_WINDOW, AX_RECREATE, 0, (LPARAM)viewer);
- }
- if(ACTIVEX_WINDOW) {
- SetWindowLong(ACTIVEX_WINDOW, GWL_EXSTYLE, GetWindowLong(ACTIVEX_WINDOW, GWL_EXSTYLE) | WS_EX_TOPMOST);
- SendMessage(ACTIVEX_WINDOW, AX_INPLACE, 1, 0);
- ShowWindow(ACTIVEX_WINDOW, SW_MAXIMIZE);
- } else {
- Display("ACTIVEX_WINDOW is null");
- }
- }
- LRESULT CALLBACK WareWndPro(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
- {
- HDC hdc;
- PAINTSTRUCT ps;
- GdiplusStartupInput gdiplusStartupInput;
- ULONG_PTR gdiplusToken;
- GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
- switch (Message) {
- case WM_CREATE:
- s_nfullWidth = GetSystemMetrics(SM_CXSCREEN);
- s_nfullHeigh = GetSystemMetrics(SM_CYSCREEN);
- InitializeCriticalSection(&csSession);
- break;
- case WM_PAINT:
- COM_INIT();
- #ifndef FULL_SCREEN
- hdc = BeginPaint(hwnd, &ps);
- GDIPLUS(hdc);
- EndPaint(hwnd, &ps);
- #endif
- break;
- case WM_DRAWITEM:
- {
- #ifndef FULL_SCREEN
- LPDRAWITEMSTRUCT pDIS;
- pDIS = (LPDRAWITEMSTRUCT)lParam;
- CHAR staticText[99];
- int len = SendMessage(pDIS->hwndItem, WM_GETTEXT, ARRAYSIZE(staticText), (LPARAM)staticText);
- if (pDIS->hwndItem == hwndConnect || pDIS->hwndItem == hwndDisconnect || pDIS->hwndItem == startSharing || pDIS->hwndItem == stopSharing){
- SetBkMode(pDIS->hDC, TRANSPARENT);
- FillRect(pDIS->hDC, &pDIS->rcItem, CreateSolidBrush(RGB(0, 100, 200)));
- SetTextColor(pDIS->hDC, RGB(255, 255, 255));
- DrawTextA(pDIS->hDC, staticText, strlen(staticText), &pDIS->rcItem, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
- SetTextColor(pDIS->hDC, RGB(0, 0, 0));
- SelectObject(pDIS->hDC, GetStockObject(NULL_BRUSH));
- SelectObject(pDIS->hDC, CreatePen(PS_DOT, 1, RGB(255, 255, 255)));
- if (pDIS->itemAction & ODA_SELECT)
- SelectObject(pDIS->hDC, CreatePen(PS_DOT, 1, RGB(255, 255, 255)));
- else
- SelectObject(pDIS->hDC, CreatePen(PS_SOLID, 1, RGB(255, 255, 255)));
- Rectangle(
- pDIS->hDC,
- pDIS->rcItem.left,
- pDIS->rcItem.top,
- pDIS->rcItem.right,
- pDIS->rcItem.bottom
- );
- }
- #endif
- }
- break;
- case WM_COMMAND:
- if ((HWND)lParam == hwndConnect){
- if (ViewerConnect(INVITATION_FILE_PATH)){
- #ifndef FULL_SCREEN
- PutStreamOnWindow(view);
- #endif
-
- }
- }
- if ((HWND)lParam == hwndDisconnect){
- ViewerDisconnect();
- }
- if ((HWND)lParam == startSharing){
- USHORT us = CTRL_LEVEL_INTERACTIVE;
- if(SendMessage(hwnd, WM_SETCTRLVIEW, us, 0)) {
- if(ShareAndInvite(INVITATION_FILE_PATH, 8344)) {
- }
- }
- }
- if ((HWND)lParam == stopSharing){
- SharerDisconnect();
- }
- break;
- case WM_INVITE:
- {
- if(ShareAndInvite((LPCTSTR)wParam, (DWORD)lParam)) {
- #ifdef FULL_SCREEN
- ShowWareWindow(FALSE);
- #endif
- return TRUE;
- }
- return FALSE;
- }
- break;
- case WM_TERMINATE_INVITE:
- {
- SharerDisconnect();
- return TRUE;
- }
- break;
- case WM_CONNECT:
- {
- if(ViewerConnect(inviteFileName, (DWORD)lParam)) {
-
- #ifdef FULL_SCREEN
- PutStreamOnWindow(hwnd);
- //ShowWindow(g_hWareHwnd, 1);
- ShowWareWindow();
- #else
- PutStreamOnWindow(view);
- #endif
- return TRUE;
- }
- return FALSE;
-
- }
- break;
- case WM_DISCONNECT:
- {
- ViewerDisconnect();
- #ifdef FULL_SCREEN
- ShowWareWindow(FALSE);
- #endif
- return TRUE;
- }
- break;
- case WM_HOTKEY:
- if(wParam == 99) {
- Display("hotkey");
- HideMenuBar();
- }
- break;
- case WM_SETCTRLVIEW:
- {
- CTRL_LEVEL level = (CTRL_LEVEL)wParam;
- Display("Ctrl Level: %d", level);
- if(level <= CTRL_LEVEL_INVALID || level > CTRL_LEVEL_MAX) {
- usCtrlLevel = CTRL_LEVEL_VIEW;
- return FALSE;
- }
- usCtrlLevel = level;
- return TRUE;
- }
- break;
- case WM_CLOSE:
- Display("WM_CLOSE message received!");
- #if 1
- Display("Simulate: Connection interrupt!");
- ViewerDisconnect();
- #ifdef FULL_SCREEN
- ShowWareWindow(FALSE);
- #endif
- #else // 1
- ViewerDisconnect();
- COM_UNIN();
- //To destroy the activex inherent window
- #ifndef FULL_SCREEN
- DestroyWindow(view);
- #endif
- DestroyWindow(hwnd);
- DeleteCriticalSection(&csSession);
- #endif
- break;
- case WM_DESTROY:
- Display("WM_DESTROY message received!");
- PostQuitMessage(0);
- break;
- default:
- /*
- #ifndef MOD_FREERDPWRAP_EXPORTS
- if(Message == WM_EXIT_MSG || Message == WM_EXIT_MSG_CODE) {
- Display("Receive the quit message!");
- ViewerDisconnect();
- COM_UNIN();
- #ifndef FULL_SCREEN
- DestroyWindow(view);
- #endif
- DestroyWindow(hwnd);
- DeleteCriticalSection(&csSession);
- }
- #endif
- */
- return DefWindowProc(hwnd, Message, wParam, lParam);
- }
- return 0;
- }
- //客户端连接分享端失败的回调事件
- void OnConnectionFailed()
- {
- Display("Connection failed! Is the server active?");
- ViewerDisconnect();
- #ifdef FULL_SCREEN
- ShowWareWindow(FALSE);
- #endif
- #ifdef MOD_FREERDPWRAP_EXPORTS
- pFsm->ConnectToHostFailed();
- #endif
- }
- void OnConnectionInterrupt()
- {
- Display("Connection interrupt! maybe the server is dead");
- ViewerDisconnect();
- #ifdef FULL_SCREEN
- ShowWareWindow(FALSE);
- #endif
- #ifdef MOD_FREERDPWRAP_EXPORTS
- pFsm->ViewerDisconnected();
- #endif
- }
- //客户端成功连接上分享端的回调事件
- void OnConnectionEstablished()
- {
- Display("Connect to Sharer successful! We are live!");
- #ifdef MOD_FREERDPWRAP_EXPORTS
- pFsm->ConnectToHostSucc();
- #endif
- }
- DWORD WINAPI RDPWarer_MainWndStartAndLoop(LPVOID param)
- {
- return WinMainEntry(GetModuleHandle (NULL), NULL, NULL, SW_SHOW);
- }
- BOOL RDPWarer_ShareAndInvite(LPCTSTR lpcszInviteFilePath, DWORD dwFilterId, USHORT usCtrl)
- {
- memset(inviteFileName, 0, sizeof(inviteFileName));
- strcpy_s(inviteFileName, lpcszInviteFilePath);
- if(SendMessage(hWarerWnd, WM_SETCTRLVIEW, (WPARAM)usCtrl, 0)) {
- return SendMessage(hWarerWnd, WM_INVITE, (WPARAM)lpcszInviteFilePath, (LPARAM)dwFilterId);
- }
- return FALSE;
- }
- BOOL RDPWarer_Connect(LPCTSTR lpcszInviteFilePath)
- {
- memset(inviteFileName, 0, sizeof(inviteFileName));
- strcpy_s(inviteFileName, lpcszInviteFilePath);
- return SendMessage(hWarerWnd, WM_CONNECT, (WPARAM)lpcszInviteFilePath, NULL);
- }
- BOOL RDPWarer_CloseInvite()
- {
- return SendMessage(hWarerWnd, WM_TERMINATE_INVITE, NULL, NULL);
- }
- BOOL RDPWarer_Disconnect()
- {
- return SendMessage(hWarerWnd, WM_DISCONNECT, NULL, NULL);
- }
|