SpWare.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. #include "stdafx.h"
  2. #include "stdafx2.h"
  3. #include "SpWare.h"
  4. #include <gdiplus.h>
  5. #include "associated.h"
  6. #include <comutil.h>
  7. #include <commctrl.h>
  8. #pragma comment(lib, "Gdiplus.lib")
  9. #pragma comment(lib, "comsuppwd.lib")
  10. #pragma comment(lib, "Comctl32.lib")
  11. #ifdef MOD_FREERDPWRAP_EXPORTS
  12. #include "..\..\Module\mod_FreeRDPWrap\FreeRDPWrapFSM.h"
  13. CFreeRDPWrapFSM* pFsm = NULL;
  14. #define TITLE_NAME "FreeRDPWrap Host"
  15. #ifdef TWINKLE_LOCAL_DEBUG
  16. #define Display(fmt, ...) \
  17. Dbg("< SpWare> :" fmt, ##__VA_ARGS__)/*; \
  18. printf("< SpWare> :" fmt "\n", ##__VA_ARGS__)*/
  19. #else
  20. #define Display(fmt, ...) \
  21. Dbg("< SpWare> :" fmt, ##__VA_ARGS__)
  22. #endif
  23. #else
  24. #include "..\..\Module\mod_FreeRDPWrap\Logger.h"
  25. #define TITLE_NAME "SpDeskShare Host"
  26. #ifdef TWINKLE_LOCAL_DEBUG
  27. #define Display(fmt, ...) \
  28. printf("<RDPWarer> :" fmt "\n", ##__VA_ARGS__); \
  29. WRITE_DEBUG_PARAM(fmt, ##__VA_ARGS__)
  30. #else
  31. #define Display(fmt, ...) \
  32. WRITE_DEBUG_PARAM(fmt, ##__VA_ARGS__)
  33. #endif
  34. #endif
  35. #define FULL_SCREEN
  36. #define REG_MSG_DESKSHARE_FLAG "{5D709B21-6BBF-468B-8FC1-6E88E9F6A571}"
  37. UINT WM_EXIT_MSG = ::RegisterWindowMessageA(REG_MSG_DESKSHARE_FLAG);
  38. #define DESKSHARE_FLAG_WIN 1 //我的同屏窗口状态wParam
  39. #define FLAG_WIN_SHOW 0 //显示窗口 <!--lparam/>
  40. #define FLAG_WIN_HIDE 1 //隐藏窗口 <!--lparam/>
  41. static int s_nfullWidth = GetSystemMetrics(SM_CXSCREEN);
  42. static int s_nfullHeigh = GetSystemMetrics(SM_CYSCREEN);
  43. /******
  44. Remote Desktop Sharing
  45. Athenian & wap2k - Rohitab forums
  46. http://www.rohitab.com
  47. ******/
  48. /*THE CLIENT (VIEWER)*/
  49. #define _CRT_SECURE_NO_WARNINGS
  50. //this is to prevent noobs and total idiots like you from using this masterpiece!
  51. using namespace Gdiplus;
  52. /*GLOBALS*/
  53. #define APP MAKEINTRESOURCE(101)
  54. #define APPSMALL MAKEINTRESOURCE(102)
  55. #define MAX_ATTENDEE 1
  56. #define override
  57. HWND g_hWareHwnd = NULL;
  58. char inviteFileName[MAX_PATH] = {0};
  59. USHORT usCtrlLevel = CTRL_LEVEL_VIEW;
  60. HWND hWarerWnd = NULL;
  61. HWND startSharing, stopSharing;
  62. HWND hwndConnect, hwndDisconnect, view, ACTIVEX_WINDOW, levelList;
  63. LRESULT CALLBACK WareWndPro(HWND, UINT, WPARAM, LPARAM);
  64. LRESULT CALLBACK scrollProc(HWND, UINT, WPARAM, LPARAM);
  65. static char className[] = "SPWARE-RDP";
  66. static HINSTANCE instance = NULL;
  67. static LONG s_nAttendees = 0;
  68. IRDPSRAPIViewer *viewer = NULL;
  69. IRDPSRAPISharingSession *session = NULL;
  70. IRDPSRAPIApplicationFilter *filter = NULL;
  71. IRDPSRAPIApplicationList *appList = NULL;
  72. IRDPSRAPIInvitationManager *invitationManager = NULL;
  73. IRDPSRAPIInvitation *invitation = NULL;
  74. IRDPSRAPIAttendeeManager *attendeeManager = NULL;
  75. IRDPSRAPIAttendee *attendee = NULL;
  76. IConnectionPointContainer* viewer_picpc = NULL;
  77. IConnectionPoint* viewer_picp = NULL;
  78. IConnectionPointContainer* sharer_picpc = NULL;
  79. IConnectionPoint* sharer_picp = NULL;
  80. SCROLLINFO si;
  81. int vs = 0;
  82. int vRemeber = NULL;
  83. int sRemeber = NULL;
  84. CRITICAL_SECTION csSession;
  85. int ConnectEvent(IUnknown* Container, REFIID riid, IUnknown* Advisor, IConnectionPointContainer** picpc, IConnectionPoint** picp)
  86. {
  87. HRESULT hr = 0;
  88. unsigned long tid = 0;
  89. IConnectionPointContainer* icpc = 0;
  90. IConnectionPoint* icp = 0;
  91. *picpc = 0;
  92. *picp = 0;
  93. Container->QueryInterface(IID_IConnectionPointContainer, (void **)&icpc);
  94. if (icpc)
  95. {
  96. *picpc = icpc;
  97. icpc->FindConnectionPoint(riid, &icp);
  98. if (icp)
  99. {
  100. *picp = icp;
  101. hr = icp->Advise(Advisor, &tid);
  102. //icp->Release();
  103. }
  104. //icpc->Release();
  105. }
  106. //Container->Release();
  107. return tid;
  108. }
  109. void DisconnectEvent(IConnectionPointContainer* icpc, IConnectionPoint* icp, unsigned int Cookie)
  110. {
  111. Display("Enter DisconnectEvent");
  112. unsigned long hr = 0;
  113. if(icp != nullptr) {
  114. icp->Unadvise(Cookie);
  115. icp->Release();
  116. }
  117. if(icpc != nullptr)
  118. icpc->Release();
  119. Display("Leave DisconnectEvent");
  120. }
  121. /*Event function prototypes*/
  122. /*viewer section*/
  123. void OnConnectionFailed();
  124. void OnConnectionInterrupt();
  125. void OnConnectionEstablished();
  126. /*sharer section*/
  127. void OnAttendeeConnected(IDispatch *pAttendee);
  128. void OnAttendeeDisconnected(IDispatch *pAttendee);
  129. void OnControlLevelChangeRequest(IDispatch *pAttendee, CTRL_LEVEL RequestedLevel);
  130. #pragma region [4/12/2018 10:34 Gifur]
  131. /*
  132. class EventSink : public _IRDPSessionEvents {
  133. public:
  134. EventSink(){
  135. }
  136. ~EventSink(){
  137. }
  138. // IUnknown
  139. virtual HRESULT STDMETHODCALLTYPE override QueryInterface(
  140. REFIID iid, void**ppvObject){
  141. *ppvObject = 0;
  142. if (iid == IID_IUnknown || iid == IID_IDispatch || iid == __uuidof(_IRDPSessionEvents))
  143. *ppvObject = this;
  144. if (*ppvObject)
  145. {
  146. ((IUnknown*)(*ppvObject))->AddRef();
  147. return S_OK;
  148. }
  149. return E_NOINTERFACE;
  150. }
  151. virtual ULONG STDMETHODCALLTYPE override AddRef(void){
  152. return 0;
  153. }
  154. virtual ULONG STDMETHODCALLTYPE override Release(void){
  155. return 0;
  156. }
  157. // IDispatch
  158. virtual HRESULT STDMETHODCALLTYPE override GetTypeInfoCount(
  159. __RPC__out UINT *pctinfo){
  160. return E_NOTIMPL;
  161. }
  162. virtual HRESULT STDMETHODCALLTYPE override GetTypeInfo(
  163. UINT iTInfo,
  164. LCID lcid,
  165. __RPC__deref_out_opt ITypeInfo **ppTInfo){
  166. return E_NOTIMPL;
  167. }
  168. virtual HRESULT STDMETHODCALLTYPE override GetIDsOfNames(
  169. __RPC__in REFIID riid,
  170. __RPC__in_ecount_full(cNames) LPOLESTR *rgszNames,
  171. UINT cNames,
  172. LCID lcid,
  173. __RPC__out_ecount_full(cNames) DISPID *rgDispId){
  174. return E_NOTIMPL;
  175. }
  176. virtual HRESULT STDMETHODCALLTYPE override EventSink::Invoke(
  177. DISPID dispIdMember,
  178. REFIID riid,
  179. LCID lcid,
  180. WORD wFlags,
  181. DISPPARAMS FAR* pDispParams,
  182. VARIANT FAR* pVarResult,
  183. EXCEPINFO FAR* pExcepInfo,
  184. unsigned int FAR* puArgErr){
  185. switch (dispIdMember){
  186. case DISPID_RDPSRAPI_EVENT_ON_VIEWER_CONNECTFAILED:
  187. OnConnectionFailed();
  188. break;
  189. case DISPID_RDPSRAPI_EVENT_ON_VIEWER_CONNECTED:
  190. OnConnectionEstablished();
  191. break;
  192. case DISPID_RDPSRAPI_EVENT_ON_ATTENDEE_CONNECTED:
  193. OnAttendeeConnected(pDispParams->rgvarg[0].pdispVal);
  194. break;
  195. case DISPID_RDPSRAPI_EVENT_ON_ATTENDEE_DISCONNECTED:
  196. OnAttendeeDisconnected(pDispParams->rgvarg[0].pdispVal);
  197. break;
  198. case DISPID_RDPSRAPI_EVENT_ON_CTRLLEVEL_CHANGE_REQUEST:
  199. OnControlLevelChangeRequest(pDispParams->rgvarg[1].pdispVal, (CTRL_LEVEL)pDispParams->rgvarg[0].intVal);
  200. break;
  201. }
  202. return S_OK;
  203. }
  204. };
  205. EventSink ev;
  206. */
  207. #pragma endregion
  208. class SharerEvtSink : public _IRDPSessionEvents {
  209. public:
  210. SharerEvtSink(){
  211. }
  212. ~SharerEvtSink(){
  213. }
  214. // IUnknown
  215. virtual HRESULT STDMETHODCALLTYPE override QueryInterface(
  216. REFIID iid, void**ppvObject){
  217. *ppvObject = 0;
  218. if (iid == IID_IUnknown || iid == IID_IDispatch || iid == __uuidof(_IRDPSessionEvents))
  219. *ppvObject = this;
  220. if (*ppvObject)
  221. {
  222. ((IUnknown*)(*ppvObject))->AddRef();
  223. return S_OK;
  224. }
  225. return E_NOINTERFACE;
  226. }
  227. virtual ULONG STDMETHODCALLTYPE override AddRef(void){
  228. return 0;
  229. }
  230. virtual ULONG STDMETHODCALLTYPE override Release(void){
  231. return 0;
  232. }
  233. // IDispatch
  234. virtual HRESULT STDMETHODCALLTYPE override GetTypeInfoCount(
  235. __RPC__out UINT *pctinfo){
  236. return E_NOTIMPL;
  237. }
  238. virtual HRESULT STDMETHODCALLTYPE override GetTypeInfo(
  239. UINT iTInfo,
  240. LCID lcid,
  241. __RPC__deref_out_opt ITypeInfo **ppTInfo){
  242. return E_NOTIMPL;
  243. }
  244. virtual HRESULT STDMETHODCALLTYPE override GetIDsOfNames(
  245. __RPC__in REFIID riid,
  246. __RPC__in_ecount_full(cNames) LPOLESTR *rgszNames,
  247. UINT cNames,
  248. LCID lcid,
  249. __RPC__out_ecount_full(cNames) DISPID *rgDispId){
  250. return E_NOTIMPL;
  251. }
  252. virtual HRESULT STDMETHODCALLTYPE override SharerEvtSink::Invoke(
  253. DISPID dispIdMember,
  254. REFIID riid,
  255. LCID lcid,
  256. WORD wFlags,
  257. DISPPARAMS FAR* pDispParams,
  258. VARIANT FAR* pVarResult,
  259. EXCEPINFO FAR* pExcepInfo,
  260. unsigned int FAR* puArgErr){
  261. switch (dispIdMember){
  262. case DISPID_RDPSRAPI_EVENT_ON_ATTENDEE_CONNECTED:
  263. OnAttendeeConnected(pDispParams->rgvarg[0].pdispVal);
  264. break;
  265. case DISPID_RDPSRAPI_EVENT_ON_ATTENDEE_DISCONNECTED:
  266. OnAttendeeDisconnected(pDispParams->rgvarg[0].pdispVal);
  267. break;
  268. case DISPID_RDPSRAPI_EVENT_ON_CTRLLEVEL_CHANGE_REQUEST:
  269. OnControlLevelChangeRequest(pDispParams->rgvarg[1].pdispVal, (CTRL_LEVEL)pDispParams->rgvarg[0].intVal);
  270. break;
  271. default:
  272. Display("SharerEvtSink: %d", dispIdMember);
  273. break;
  274. }
  275. return S_OK;
  276. }
  277. };
  278. SharerEvtSink sEv;
  279. class ViewerEvtSink : public _IRDPSessionEvents {
  280. public:
  281. ViewerEvtSink(){
  282. }
  283. ~ViewerEvtSink(){
  284. }
  285. // IUnknown
  286. virtual HRESULT STDMETHODCALLTYPE override QueryInterface(
  287. REFIID iid, void**ppvObject){
  288. *ppvObject = 0;
  289. if (iid == IID_IUnknown || iid == IID_IDispatch || iid == __uuidof(_IRDPSessionEvents))
  290. *ppvObject = this;
  291. if (*ppvObject)
  292. {
  293. ((IUnknown*)(*ppvObject))->AddRef();
  294. return S_OK;
  295. }
  296. return E_NOINTERFACE;
  297. }
  298. virtual ULONG STDMETHODCALLTYPE override AddRef(void){
  299. return 0;
  300. }
  301. virtual ULONG STDMETHODCALLTYPE override Release(void){
  302. return 0;
  303. }
  304. // IDispatch
  305. virtual HRESULT STDMETHODCALLTYPE override GetTypeInfoCount(
  306. __RPC__out UINT *pctinfo){
  307. return E_NOTIMPL;
  308. }
  309. virtual HRESULT STDMETHODCALLTYPE override GetTypeInfo(
  310. UINT iTInfo,
  311. LCID lcid,
  312. __RPC__deref_out_opt ITypeInfo **ppTInfo){
  313. return E_NOTIMPL;
  314. }
  315. virtual HRESULT STDMETHODCALLTYPE override GetIDsOfNames(
  316. __RPC__in REFIID riid,
  317. __RPC__in_ecount_full(cNames) LPOLESTR *rgszNames,
  318. UINT cNames,
  319. LCID lcid,
  320. __RPC__out_ecount_full(cNames) DISPID *rgDispId){
  321. return E_NOTIMPL;
  322. }
  323. virtual HRESULT STDMETHODCALLTYPE override ViewerEvtSink::Invoke(
  324. DISPID dispIdMember,
  325. REFIID riid,
  326. LCID lcid,
  327. WORD wFlags,
  328. DISPPARAMS FAR* pDispParams,
  329. VARIANT FAR* pVarResult,
  330. EXCEPINFO FAR* pExcepInfo,
  331. unsigned int FAR* puArgErr){
  332. switch (dispIdMember){
  333. case DISPID_RDPSRAPI_EVENT_ON_VIEWER_CONNECTFAILED:
  334. OnConnectionFailed();
  335. break;
  336. case DISPID_RDPSRAPI_EVENT_ON_VIEWER_CONNECTED:
  337. OnConnectionEstablished();
  338. break;
  339. case DISPID_RDPSRAPI_EVENT_ON_VIEWER_DISCONNECTED:
  340. OnConnectionInterrupt();
  341. break;
  342. default:
  343. Display("ViewerEvtSink: %d", dispIdMember);
  344. break;
  345. }
  346. return S_OK;
  347. }
  348. };
  349. ViewerEvtSink vEv;
  350. void GDIPLUS(HDC hdc){
  351. Graphics graphics(hdc);
  352. FontFamily family(L"Verdana");
  353. Font font(&family, 15, FontStyleRegular, UnitPixel);
  354. SolidBrush bBrush(Color(255, 0, 100, 200));
  355. // Fill the rectangle.
  356. graphics.FillRectangle(&bBrush, 0, 0, 150, 260);
  357. }
  358. BOOL bIsZoomed, bFullScreen = FALSE;
  359. DWORD ghwndStyle = (WS_OVERLAPPEDWINDOW | WS_SYSMENU | WS_VISIBLE);
  360. RECT rcPrevious;
  361. WINDOWPLACEMENT gOldPlacement;
  362. WINDOWPLACEMENT gNewPlacement;
  363. void HideMenuBar()
  364. {
  365. #ifndef FULL_SCREEN
  366. if (GetWindowLong(g_hWareHwnd, GWL_STYLE) & WS_OVERLAPPEDWINDOW)
  367. {
  368. Display("HideMenuBar@1");
  369. GetWindowPlacement(view, &gOldPlacement);
  370. GetWindowRect(g_hWareHwnd, &rcPrevious);
  371. int nfw = GetSystemMetrics(SM_CXSCREEN);
  372. int nfh = GetSystemMetrics(SM_CYSCREEN);
  373. // save window state
  374. //Judge whether the dialog is in maxshow status
  375. bIsZoomed = IsZoomed(g_hWareHwnd);
  376. SetMenu(g_hWareHwnd, NULL);
  377. //restore the dialog to its original size and position.
  378. if (bIsZoomed) ShowWindow(g_hWareHwnd, SW_RESTORE);
  379. SetWindowLong(g_hWareHwnd, GWL_STYLE, WS_POPUP);
  380. //ShowCursor(FALSE);
  381. SetThreadExecutionState(ES_CONTINUOUS //informs the system that the state should be remained
  382. | ES_SYSTEM_REQUIRED // Forces the system to be reseting the display idle timer.
  383. | ES_DISPLAY_REQUIRED // Forces the display to be reseting the display idle timer.
  384. );
  385. Display("w:%d, h:%d", nfw, nfh);
  386. MoveWindow(g_hWareHwnd, 0, 0, nfw, nfh, TRUE);
  387. ShowWindow(g_hWareHwnd, SW_RESTORE);
  388. MoveWindow(view, 0, 0, nfw, nfh, TRUE);
  389. bFullScreen = TRUE;
  390. }
  391. else
  392. {
  393. Display("HideMenuBar@2");
  394. ShowWindow(g_hWareHwnd, SW_RESTORE);
  395. SetWindowLong(g_hWareHwnd, GWL_STYLE, ghwndStyle);
  396. if (bIsZoomed) ShowWindow(g_hWareHwnd, SW_MAXIMIZE);
  397. //ShowCursor(TRUE);
  398. SetThreadExecutionState(ES_CONTINUOUS);
  399. // Trigger WM_SIZE message
  400. RECT& Rc = rcPrevious;
  401. //GetWindowRect(g_hWareHwnd, &Rc);
  402. SetWindowPos(g_hWareHwnd, HWND_TOPMOST, Rc.left, Rc.top,
  403. (Rc.right-Rc.left),(Rc.bottom-Rc.top), SWP_FRAMECHANGED);
  404. SetWindowPlacement(view, &gOldPlacement);
  405. bFullScreen = FALSE;
  406. }
  407. #endif
  408. }
  409. void ShowWareWindow(BOOL bShow = TRUE) {
  410. DWORD dwREcipents = BSM_APPLICATIONS | BSM_ALLDESKTOPS;
  411. if(bShow) {
  412. Display("topmostex %d, %d", s_nfullWidth, s_nfullHeigh);
  413. //SetWindowPos(g_hWareHwnd, HWND_TOPMOST, 0, 0, s_nfullWidth, s_nfullHeigh, TRUE);
  414. SetWindowPos(g_hWareHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
  415. //ShowWindow(g_hWareHwnd, 1);
  416. Display("TopMost or not: %d", !!(GetWindowLong(g_hWareHwnd, GWL_EXSTYLE) & WS_EX_TOPMOST));
  417. #ifndef MOD_FREERDPWRAP_EXPORTS
  418. if(0 >= BroadcastSystemMessageA(BSF_POSTMESSAGE | BSF_FORCEIFHUNG, &dwREcipents, WM_EXIT_MSG, DESKSHARE_FLAG_WIN, FLAG_WIN_SHOW)) {
  419. Display("BroadcastSystemMessageA failed %d", GetLastError());
  420. }
  421. #endif
  422. }
  423. else {
  424. Display("Set not topmost!");
  425. SetWindowPos(g_hWareHwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE);
  426. #ifndef MOD_FREERDPWRAP_EXPORTS
  427. if(0 >= BroadcastSystemMessageA(BSF_POSTMESSAGE | BSF_FORCEIFHUNG, &dwREcipents, WM_EXIT_MSG, DESKSHARE_FLAG_WIN, FLAG_WIN_HIDE)) {
  428. Display("BroadcastSystemMessageA failed %d", GetLastError());
  429. }
  430. #endif
  431. }
  432. Display("Leave ShowWareWindow(%d)", bShow);
  433. }
  434. bool CheckFilePath(const char *path){
  435. FILE *check = NULL;
  436. fopen_s(&check, path, "r");
  437. if (check){
  438. //check for size 0
  439. fseek(check, 0, SEEK_END);
  440. int size = ftell(check);
  441. rewind(check);
  442. if (size > 0){
  443. fclose(check);
  444. return true;
  445. }
  446. else{
  447. fclose(check);
  448. return false;
  449. }
  450. }
  451. return false;
  452. }
  453. bool ViewerConnect(const char* invite_file_path, DWORD select_id = 0)
  454. {
  455. HRESULT hr = S_OK;
  456. Display("Invitation file path: %s", invite_file_path);
  457. if (invite_file_path){
  458. if (CheckFilePath(invite_file_path)) {
  459. Display("Invitation file's integrity checked!");
  460. //__ComInvokeEventHandler()
  461. //To prevent another start procedure
  462. if (viewer == NULL){
  463. if ((hr = CoCreateInstance(__uuidof(RDPViewer),
  464. NULL, CLSCTX_INPROC_SERVER,
  465. __uuidof(IRDPSRAPIViewer),
  466. (void**)&viewer)) == S_OK) {
  467. Display("Instance created!");
  468. vRemeber = ConnectEvent((IUnknown*)viewer, __uuidof(_IRDPSessionEvents), (IUnknown*)&vEv, &viewer_picpc, &viewer_picp);
  469. Display("Start to read invitation file!");
  470. FILE *read = NULL;
  471. fopen_s(&read, invite_file_path, "r");
  472. if (read) {
  473. fseek(read, 0, SEEK_END);
  474. int flen = ftell(read);
  475. if (flen <= 0) {
  476. fclose(read);
  477. Display("Invalid inviation file !");
  478. return false;
  479. }
  480. char* inviteString = new char[flen + 1];
  481. if (inviteString == NULL) {
  482. Display("allocate buffer for file content failed !");
  483. fclose(read);
  484. return false;
  485. }
  486. ZeroMemory(inviteString, sizeof(char)*(flen + 1));
  487. fseek(read, 0, SEEK_SET);
  488. fgets(inviteString, flen, read);
  489. inviteString[flen] = '\0';
  490. fclose(read);
  491. if ((hr = viewer->Connect(B(inviteString), RVC_RDP_AUTH, B(""))) == S_OK) {
  492. Display("Connection line active!");
  493. delete[] inviteString;
  494. inviteString = NULL;
  495. return true;
  496. }
  497. else{
  498. delete[] inviteString;
  499. inviteString = NULL;
  500. Display("Connection line error, hr=0x%X GLE=%u !", hr, GetLastError());
  501. return false;
  502. }
  503. }
  504. else{
  505. Display("Error reading invitation file !");
  506. return false;
  507. }
  508. }
  509. else{
  510. Display("Error creating instance, hr=0x%X GLE=%u !", hr, GetLastError());
  511. return false;
  512. }
  513. }
  514. else{
  515. Display("Error starting: viewer already exists!");
  516. return false;
  517. }
  518. }
  519. else{
  520. Display("Invalid invitation file!");
  521. return false;
  522. }
  523. }
  524. else{
  525. Display("Error: An invitation file must be selected!");
  526. return false;
  527. }
  528. }
  529. void ViewerDisconnect()
  530. {
  531. Display("Disconnecting...0x%X", GetCurrentThreadId());
  532. if (viewer){
  533. DisconnectEvent(viewer_picpc, viewer_picp, vRemeber);
  534. viewer->Disconnect();
  535. viewer->Release();
  536. viewer = NULL;
  537. Display("Disconnected!");
  538. }
  539. else {
  540. Display("High warning at disconnecting: No active connection!");
  541. }
  542. }
  543. BOOL FilterApplication(long filterid, const char* filtr_name)
  544. {
  545. BOOL bFound = FALSE;
  546. HRESULT hr = S_OK;
  547. IRDPSRAPIApplicationList* list = NULL;
  548. if (session) {
  549. if ((hr = session->get_ApplicationFilter(&filter)) == S_OK) {
  550. hr = filter->put_Enabled(TRUE);
  551. if (hr != S_OK) {
  552. Display("Set enable for application filter failed, 0x%X", hr);
  553. return FALSE;
  554. }
  555. //list of objects in the session that can be shared.
  556. hr = filter->get_Applications(&list);
  557. if (hr != S_OK) {
  558. Display("get applicaion list from application filter failed, hr = 0x%X", hr);
  559. return FALSE;
  560. }
  561. for (int i = 0; i < 102400; i++) {
  562. IRDPSRAPIApplication* app = NULL;
  563. hr = list->get_Item(i, &app);
  564. if (hr != S_OK) {
  565. //WRITE_WARN_PARAM("get applicaion item(%d) from application list failed, hr = 0x%X", i, hr);
  566. continue;
  567. }
  568. if (app != NULL) {
  569. long id;
  570. BSTR bstrAppName;
  571. app->get_Id(&id);
  572. app->get_Name(&bstrAppName);
  573. if (filterid != 0 && id == filterid) {
  574. if(app->put_Shared(VARIANT_TRUE) == S_OK) {
  575. bFound = TRUE;
  576. }
  577. break;
  578. }
  579. _bstr_t b = bstrAppName;
  580. if(filtr_name != NULL && !_stricmp(filtr_name, (LPCTSTR)b)) {
  581. if(app->put_Shared(VARIANT_TRUE) == S_OK) {
  582. bFound = TRUE;
  583. }
  584. break;
  585. }
  586. Display("%d -- %s", id, (LPCTSTR)b);
  587. }
  588. }
  589. }
  590. else {
  591. Display("fetch application filter failed, hr = 0x%X !", hr);
  592. return FALSE;
  593. }
  594. }
  595. return bFound;
  596. }
  597. BOOL ShareAndInvite(LPCTSTR lpcszInviteFilePath, DWORD dwFilterId)
  598. {
  599. HRESULT hr = S_OK;
  600. if(session != NULL) {
  601. Display("Error starting: Session already exists!");
  602. //prevent from reporting error result in extreme situation. [4/16/2018 9:59 @Gifur]
  603. //Display("Destroy the previous one.");
  604. //SharerDisconnect();
  605. return FALSE;
  606. }
  607. if ((hr = CoCreateInstance(__uuidof(RDPSession),
  608. NULL, CLSCTX_INPROC_SERVER,
  609. __uuidof(IRDPSRAPISharingSession),
  610. (void**)&session)) != S_OK) {
  611. Display("Error creating instance, hr=0x%X GLE=%u !", hr, GetLastError());
  612. goto Error;
  613. }
  614. Display("Instance created !");
  615. sRemeber = ConnectEvent((IUnknown*)session, __uuidof(_IRDPSessionEvents), (IUnknown*)&sEv, &sharer_picpc, &sharer_picp);
  616. if ((hr = session->Open()) != S_OK) {
  617. Display("Error opening session, hr=0x%X GLE=%u !", hr, GetLastError());
  618. goto Error;
  619. }
  620. Display("Session opened!");
  621. if(s_nfullHeigh > 0 && s_nfullWidth > 0) {
  622. session->SetDesktopSharedRect(0, 0, s_nfullWidth, s_nfullHeigh);
  623. }
  624. if(dwFilterId != 0) {
  625. if(!FilterApplication((long)dwFilterId, NULL)) {
  626. Display("Error filter application id");
  627. goto Error;
  628. }
  629. }
  630. if (session->get_Invitations(&invitationManager) != S_OK) {
  631. Display("Get invitations error, hr=0x%X GLE=%u !", hr, GetLastError());
  632. goto Error;
  633. }
  634. Display("Get invitations ok!");
  635. if ((hr = invitationManager->CreateInvitation(
  636. RVC_RDP_AUTH,
  637. RVC_RDP_GROUP,
  638. B(""),
  639. MAX_ATTENDEE,
  640. &invitation)) != S_OK) {
  641. Display("Error obtaining invitation, hr=0x%X GLE=%u !", hr, GetLastError());
  642. goto Error;
  643. }
  644. Display("Invitation obtained!");
  645. FILE *invite = NULL;
  646. fopen_s(&invite, lpcszInviteFilePath, "w");
  647. if(!invite) {
  648. Display("fopen file(%s) failed.", lpcszInviteFilePath);
  649. goto Error;
  650. }
  651. BSTR inviteString;
  652. if ((hr = invitation->get_ConnectionString(&inviteString)) == S_OK) {
  653. _bstr_t b = inviteString;
  654. char* lpszInvite = b;
  655. fprintf_s(invite, "%s", lpszInvite);
  656. Display("Invitation written to file!");
  657. Display("invite_file_path: %s", lpcszInviteFilePath);
  658. SysFreeString(inviteString);
  659. }
  660. fclose(invite);
  661. if ((hr = session->get_Attendees(&attendeeManager)) == S_OK) {
  662. Display("Get Attendees ok, Waitting for attandee !");
  663. }
  664. return TRUE;
  665. Error:
  666. if(invitation) {
  667. invitation->Release();
  668. invitation = NULL;
  669. }
  670. if(invitationManager) {
  671. invitationManager->Release();
  672. invitationManager = NULL;
  673. }
  674. if (session){
  675. session->Close();
  676. while(session->Release() > 0) {
  677. ;
  678. }
  679. session = NULL;
  680. }
  681. return FALSE;
  682. }
  683. void SharerDisconnect()
  684. {
  685. EnterCriticalSection(&csSession);
  686. Display("Start to stop the invitation session... 0x%X", GetCurrentThreadId());
  687. if(invitation) {
  688. invitation->Release();
  689. invitation = NULL;
  690. }
  691. if(invitationManager) {
  692. invitationManager->Release();
  693. invitationManager = NULL;
  694. }
  695. if (session){
  696. DisconnectEvent(sharer_picpc, sharer_picp, sRemeber);
  697. session->Close();
  698. while(session->Release() > 0){
  699. Display("amazing!");
  700. }
  701. session = NULL;
  702. Display("Session stopped succ!");
  703. }
  704. else {
  705. Display("Warning at stopping: No active session!");
  706. }
  707. Display("Current attendee count: %ld", s_nAttendees);
  708. if(s_nAttendees > 0) {
  709. InterlockedExchange(&s_nAttendees, 0);
  710. }
  711. LeaveCriticalSection(&csSession);
  712. }
  713. void OnAttendeeConnected(IDispatch *pAttendee)
  714. {
  715. IRDPSRAPIAttendee *pRDPAtendee;
  716. pAttendee->QueryInterface(__uuidof(IRDPSRAPIAttendee), (void**)&pRDPAtendee);
  717. pRDPAtendee->put_ControlLevel((CTRL_LEVEL)usCtrlLevel);
  718. Display("An attendee connected!");
  719. InterlockedIncrement(&s_nAttendees);
  720. if(s_nAttendees == 1) {
  721. #ifdef MOD_FREERDPWRAP_EXPORTS
  722. pFsm->AttendeeConnected();
  723. #endif
  724. }
  725. }
  726. void OnAttendeeDisconnected(IDispatch *pAttendee){
  727. IRDPSRAPIAttendeeDisconnectInfo *info;
  728. ATTENDEE_DISCONNECT_REASON reason;
  729. pAttendee->QueryInterface(__uuidof(IRDPSRAPIAttendeeDisconnectInfo), (void**)&info);
  730. if (info->get_Reason(&reason) == S_OK){
  731. char *textReason = NULL;
  732. switch (reason){
  733. case ATTENDEE_DISCONNECT_REASON_APP:
  734. textReason = "Viewer terminated session!";
  735. break;
  736. case ATTENDEE_DISCONNECT_REASON_ERR:
  737. textReason = "Internal Error!";
  738. break;
  739. case ATTENDEE_DISCONNECT_REASON_CLI:
  740. textReason = "Attendee requested termination!";
  741. break;
  742. default:
  743. textReason = "Unknown reason!";
  744. }
  745. Display("Attendee disconnected!");
  746. Display("Reason: %s", textReason);
  747. }
  748. pAttendee->Release();
  749. //sharer_picp = 0;
  750. //sharer_picpc = 0;
  751. InterlockedDecrement(&s_nAttendees);
  752. if(s_nAttendees == 0) {
  753. //SharerDisconnect();
  754. PostMessage(g_hWareHwnd, WM_TERMINATE_INVITE, 0, 0);
  755. #ifdef MOD_FREERDPWRAP_EXPORTS
  756. pFsm->AttendeeDisconnect();
  757. #endif
  758. Display("After SharerDisconnect() in event");
  759. }
  760. }
  761. void OnControlLevelChangeRequest(IDispatch *pAttendee, CTRL_LEVEL RequestedLevel){
  762. IRDPSRAPIAttendee *pRDPAtendee;
  763. pAttendee->QueryInterface(__uuidof(IRDPSRAPIAttendee), (void**)&pRDPAtendee);
  764. if (pRDPAtendee->put_ControlLevel(RequestedLevel) == S_OK){
  765. switch (RequestedLevel){
  766. case CTRL_LEVEL_NONE:
  767. Display("Level changed to CTRL_LEVEL_NONE!");
  768. break;
  769. case CTRL_LEVEL_VIEW:
  770. Display("Level changed to CTRL_LEVEL_VIEW!");
  771. break;
  772. case CTRL_LEVEL_INTERACTIVE:
  773. Display("Level changed to CTRL_LEVEL_INTERACTIVE!");
  774. break;
  775. }
  776. }
  777. }
  778. HWND CreateButton(LPCSTR lpButtonName, HWND hWnd, int x, int y){
  779. return CreateWindow("button", lpButtonName, WS_EX_TRANSPARENT | BS_OWNERDRAW | WS_CHILD | WS_VISIBLE, x, y, 100, 30, hWnd, 0, (HINSTANCE)hWnd, 0);
  780. }
  781. int WINAPI WinMainEntry(HINSTANCE hInstance,
  782. HINSTANCE hPrevInstance,
  783. LPSTR lpCmdLine,
  784. int nCmdShow)
  785. {
  786. WNDCLASSEX WndClass;
  787. MSG Msg;
  788. int nfw = 0;
  789. int nfh = 0;
  790. instance = hInstance;
  791. AXRegister();
  792. INITCOMMONCONTROLSEX icex = { 0 };
  793. InitCommonControls();
  794. WndClass.cbSize = sizeof(WNDCLASSEX);
  795. WndClass.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_PARENTDC;
  796. WndClass.lpfnWndProc = WareWndPro;
  797. WndClass.cbClsExtra = 0;
  798. WndClass.cbWndExtra = 0;
  799. WndClass.hInstance = instance;
  800. WndClass.hIcon = LoadIcon(hInstance, APP);
  801. WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  802. WndClass.hbrBackground = CreateSolidBrush(RGB(245, 247, 248));
  803. WndClass.lpszMenuName = 0;
  804. WndClass.lpszClassName = className;
  805. WndClass.hIconSm = LoadIcon(hInstance, APPSMALL);
  806. RegisterClassEx(&WndClass);
  807. #ifndef FULL_SCREEN
  808. nfw = 1200;
  809. nfh = 600;
  810. #else
  811. nfw = GetSystemMetrics(SM_CXSCREEN);
  812. nfh = GetSystemMetrics(SM_CYSCREEN);
  813. #endif
  814. hWarerWnd = CreateWindowExA(
  815. WS_EX_TOPMOST,
  816. className,
  817. TITLE_NAME,
  818. WS_OVERLAPPEDWINDOW | WS_POPUP,
  819. CW_USEDEFAULT, CW_USEDEFAULT,
  820. nfw, nfh,
  821. NULL, NULL,
  822. instance,
  823. NULL);
  824. RECT wInfo;
  825. GetClientRect(hWarerWnd, &wInfo);
  826. int Width = wInfo.right;
  827. int Height = wInfo.bottom;
  828. #ifndef FULL_SCREEN
  829. hwndConnect = CreateButton("Connect", hWarerWnd, 20, 20);
  830. hwndDisconnect = CreateButton("Disconnect", hWarerWnd, 20, 80);
  831. startSharing = CreateButton("Start Sharing", hWarerWnd, 20, 140);
  832. stopSharing = CreateButton("Stop Sharing", hWarerWnd, 20, 200);
  833. view = CreateWindowA("edit", 0,
  834. WS_CLIPCHILDREN | WS_CHILD | WS_VISIBLE,
  835. 160, 5, Width - 160, Height - 5, hWarerWnd, 0, instance, 0);
  836. SetWindowLong(hWarerWnd, GWL_STYLE, GetWindowLong(hWarerWnd, GWL_STYLE) &~WS_MAXIMIZEBOX);
  837. ShowWindow(hWarerWnd, 1);
  838. UpdateWindow(hWarerWnd);
  839. SendMessage(view, EM_SETREADONLY, 1, 0);
  840. int hotkeyID = GlobalAddAtom("MyHotkey");
  841. RegisterHotKey(hWarerWnd, 99, MOD_ALT, VK_F8);
  842. #else
  843. //view = CreateWindowA("edit", 0,
  844. // WS_CLIPCHILDREN | WS_CHILD | WS_VISIBLE,
  845. // 0, 0, Width, Height, hWarerWnd, 0, instance, 0);
  846. SetWindowLong(hWarerWnd, GWL_STYLE, WS_POPUP);
  847. SetWindowPos(hWarerWnd, HWND_TOPMOST, 0, 0, nfw, nfh, SWP_HIDEWINDOW);
  848. #endif
  849. g_hWareHwnd = hWarerWnd;
  850. while (GetMessage(&Msg, NULL, 0, 0)) {
  851. TranslateMessage(&Msg);
  852. DispatchMessage(&Msg);
  853. }
  854. Display("Out of GetMessage.");
  855. return Msg.wParam;
  856. }
  857. void RDPWarer_SetReference(PVOID param)
  858. {
  859. #ifdef MOD_FREERDPWRAP_EXPORTS
  860. pFsm = reinterpret_cast<CFreeRDPWrapFSM*>(param);
  861. LOG_ASSERT(pFsm != NULL);
  862. #else
  863. UNREFERENCED_PARAMETER(param);
  864. #endif
  865. }
  866. void PutStreamOnWindow(HWND hh){
  867. // While looking at the calls made I have never seen this create window called it is always returns 0 and is recreated
  868. ACTIVEX_WINDOW = CreateWindowEx(0, "AX", "{32be5ed2-5c86-480f-a914-0ff8885a1b3f}", WS_CHILD | WS_VISIBLE, 0, 20, 1, 1, hh, 0, instance, 0);
  869. if (ACTIVEX_WINDOW){
  870. IUnknown*a = 0;
  871. SendMessage(ACTIVEX_WINDOW, AX_QUERYINTERFACE, (WPARAM)&__uuidof(IUnknown*), (LPARAM)&a);
  872. }
  873. else {
  874. ACTIVEX_WINDOW = CreateWindow("AX", "}32BE5ED2-5C86-480F-A914-0FF8885A1B3F}", WS_CHILD | WS_VISIBLE, 0, 0, 1, 1, hh, 0, instance, 0);
  875. SendMessage(ACTIVEX_WINDOW, AX_RECREATE, 0, (LPARAM)viewer);
  876. }
  877. if(ACTIVEX_WINDOW) {
  878. SetWindowLong(ACTIVEX_WINDOW, GWL_EXSTYLE, GetWindowLong(ACTIVEX_WINDOW, GWL_EXSTYLE) | WS_EX_TOPMOST);
  879. SendMessage(ACTIVEX_WINDOW, AX_INPLACE, 1, 0);
  880. ShowWindow(ACTIVEX_WINDOW, SW_MAXIMIZE);
  881. } else {
  882. Display("ACTIVEX_WINDOW is null");
  883. }
  884. }
  885. LRESULT CALLBACK WareWndPro(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  886. {
  887. HDC hdc;
  888. PAINTSTRUCT ps;
  889. GdiplusStartupInput gdiplusStartupInput;
  890. ULONG_PTR gdiplusToken;
  891. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  892. switch (Message) {
  893. case WM_CREATE:
  894. s_nfullWidth = GetSystemMetrics(SM_CXSCREEN);
  895. s_nfullHeigh = GetSystemMetrics(SM_CYSCREEN);
  896. InitializeCriticalSection(&csSession);
  897. break;
  898. case WM_PAINT:
  899. COM_INIT();
  900. #ifndef FULL_SCREEN
  901. hdc = BeginPaint(hwnd, &ps);
  902. GDIPLUS(hdc);
  903. EndPaint(hwnd, &ps);
  904. #endif
  905. break;
  906. case WM_DRAWITEM:
  907. {
  908. #ifndef FULL_SCREEN
  909. LPDRAWITEMSTRUCT pDIS;
  910. pDIS = (LPDRAWITEMSTRUCT)lParam;
  911. CHAR staticText[99];
  912. int len = SendMessage(pDIS->hwndItem, WM_GETTEXT, ARRAYSIZE(staticText), (LPARAM)staticText);
  913. if (pDIS->hwndItem == hwndConnect || pDIS->hwndItem == hwndDisconnect || pDIS->hwndItem == startSharing || pDIS->hwndItem == stopSharing){
  914. SetBkMode(pDIS->hDC, TRANSPARENT);
  915. FillRect(pDIS->hDC, &pDIS->rcItem, CreateSolidBrush(RGB(0, 100, 200)));
  916. SetTextColor(pDIS->hDC, RGB(255, 255, 255));
  917. DrawTextA(pDIS->hDC, staticText, strlen(staticText), &pDIS->rcItem, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
  918. SetTextColor(pDIS->hDC, RGB(0, 0, 0));
  919. SelectObject(pDIS->hDC, GetStockObject(NULL_BRUSH));
  920. SelectObject(pDIS->hDC, CreatePen(PS_DOT, 1, RGB(255, 255, 255)));
  921. if (pDIS->itemAction & ODA_SELECT)
  922. SelectObject(pDIS->hDC, CreatePen(PS_DOT, 1, RGB(255, 255, 255)));
  923. else
  924. SelectObject(pDIS->hDC, CreatePen(PS_SOLID, 1, RGB(255, 255, 255)));
  925. Rectangle(
  926. pDIS->hDC,
  927. pDIS->rcItem.left,
  928. pDIS->rcItem.top,
  929. pDIS->rcItem.right,
  930. pDIS->rcItem.bottom
  931. );
  932. }
  933. #endif
  934. }
  935. break;
  936. case WM_COMMAND:
  937. if ((HWND)lParam == hwndConnect){
  938. if (ViewerConnect(INVITATION_FILE_PATH)){
  939. #ifndef FULL_SCREEN
  940. PutStreamOnWindow(view);
  941. #endif
  942. }
  943. }
  944. if ((HWND)lParam == hwndDisconnect){
  945. ViewerDisconnect();
  946. }
  947. if ((HWND)lParam == startSharing){
  948. USHORT us = CTRL_LEVEL_INTERACTIVE;
  949. if(SendMessage(hwnd, WM_SETCTRLVIEW, us, 0)) {
  950. if(ShareAndInvite(INVITATION_FILE_PATH, 8344)) {
  951. }
  952. }
  953. }
  954. if ((HWND)lParam == stopSharing){
  955. SharerDisconnect();
  956. }
  957. break;
  958. case WM_INVITE:
  959. {
  960. if(ShareAndInvite((LPCTSTR)wParam, (DWORD)lParam)) {
  961. #ifdef FULL_SCREEN
  962. ShowWareWindow(FALSE);
  963. #endif
  964. return TRUE;
  965. }
  966. return FALSE;
  967. }
  968. break;
  969. case WM_TERMINATE_INVITE:
  970. {
  971. SharerDisconnect();
  972. return TRUE;
  973. }
  974. break;
  975. case WM_CONNECT:
  976. {
  977. if(ViewerConnect(inviteFileName, (DWORD)lParam)) {
  978. #ifdef FULL_SCREEN
  979. PutStreamOnWindow(hwnd);
  980. //ShowWindow(g_hWareHwnd, 1);
  981. ShowWareWindow();
  982. #else
  983. PutStreamOnWindow(view);
  984. #endif
  985. return TRUE;
  986. }
  987. return FALSE;
  988. }
  989. break;
  990. case WM_DISCONNECT:
  991. {
  992. ViewerDisconnect();
  993. #ifdef FULL_SCREEN
  994. ShowWareWindow(FALSE);
  995. #endif
  996. return TRUE;
  997. }
  998. break;
  999. case WM_HOTKEY:
  1000. if(wParam == 99) {
  1001. Display("hotkey");
  1002. HideMenuBar();
  1003. }
  1004. break;
  1005. case WM_SETCTRLVIEW:
  1006. {
  1007. CTRL_LEVEL level = (CTRL_LEVEL)wParam;
  1008. Display("Ctrl Level: %d", level);
  1009. if(level <= CTRL_LEVEL_INVALID || level > CTRL_LEVEL_MAX) {
  1010. usCtrlLevel = CTRL_LEVEL_VIEW;
  1011. return FALSE;
  1012. }
  1013. usCtrlLevel = level;
  1014. return TRUE;
  1015. }
  1016. break;
  1017. case WM_CLOSE:
  1018. Display("WM_CLOSE message received!");
  1019. #if 1
  1020. Display("Simulate: Connection interrupt!");
  1021. ViewerDisconnect();
  1022. #ifdef FULL_SCREEN
  1023. ShowWareWindow(FALSE);
  1024. #endif
  1025. #else // 1
  1026. ViewerDisconnect();
  1027. COM_UNIN();
  1028. //To destroy the activex inherent window
  1029. #ifndef FULL_SCREEN
  1030. DestroyWindow(view);
  1031. #endif
  1032. DestroyWindow(hwnd);
  1033. DeleteCriticalSection(&csSession);
  1034. #endif
  1035. break;
  1036. case WM_DESTROY:
  1037. Display("WM_DESTROY message received!");
  1038. PostQuitMessage(0);
  1039. break;
  1040. default:
  1041. /*
  1042. #ifndef MOD_FREERDPWRAP_EXPORTS
  1043. if(Message == WM_EXIT_MSG || Message == WM_EXIT_MSG_CODE) {
  1044. Display("Receive the quit message!");
  1045. ViewerDisconnect();
  1046. COM_UNIN();
  1047. #ifndef FULL_SCREEN
  1048. DestroyWindow(view);
  1049. #endif
  1050. DestroyWindow(hwnd);
  1051. DeleteCriticalSection(&csSession);
  1052. }
  1053. #endif
  1054. */
  1055. return DefWindowProc(hwnd, Message, wParam, lParam);
  1056. }
  1057. return 0;
  1058. }
  1059. //客户端连接分享端失败的回调事件
  1060. void OnConnectionFailed()
  1061. {
  1062. Display("Connection failed! Is the server active?");
  1063. ViewerDisconnect();
  1064. #ifdef FULL_SCREEN
  1065. ShowWareWindow(FALSE);
  1066. #endif
  1067. #ifdef MOD_FREERDPWRAP_EXPORTS
  1068. pFsm->ConnectToHostFailed();
  1069. #endif
  1070. }
  1071. void OnConnectionInterrupt()
  1072. {
  1073. Display("Connection interrupt! maybe the server is dead");
  1074. ViewerDisconnect();
  1075. #ifdef FULL_SCREEN
  1076. ShowWareWindow(FALSE);
  1077. #endif
  1078. #ifdef MOD_FREERDPWRAP_EXPORTS
  1079. pFsm->ViewerDisconnected();
  1080. #endif
  1081. }
  1082. //客户端成功连接上分享端的回调事件
  1083. void OnConnectionEstablished()
  1084. {
  1085. Display("Connect to Sharer successful! We are live!");
  1086. #ifdef MOD_FREERDPWRAP_EXPORTS
  1087. pFsm->ConnectToHostSucc();
  1088. #endif
  1089. }
  1090. DWORD WINAPI RDPWarer_MainWndStartAndLoop(LPVOID param)
  1091. {
  1092. return WinMainEntry(GetModuleHandle (NULL), NULL, NULL, SW_SHOW);
  1093. }
  1094. BOOL RDPWarer_ShareAndInvite(LPCTSTR lpcszInviteFilePath, DWORD dwFilterId, USHORT usCtrl)
  1095. {
  1096. memset(inviteFileName, 0, sizeof(inviteFileName));
  1097. strcpy_s(inviteFileName, lpcszInviteFilePath);
  1098. if(SendMessage(hWarerWnd, WM_SETCTRLVIEW, (WPARAM)usCtrl, 0)) {
  1099. return SendMessage(hWarerWnd, WM_INVITE, (WPARAM)lpcszInviteFilePath, (LPARAM)dwFilterId);
  1100. }
  1101. return FALSE;
  1102. }
  1103. BOOL RDPWarer_Connect(LPCTSTR lpcszInviteFilePath)
  1104. {
  1105. memset(inviteFileName, 0, sizeof(inviteFileName));
  1106. strcpy_s(inviteFileName, lpcszInviteFilePath);
  1107. return SendMessage(hWarerWnd, WM_CONNECT, (WPARAM)lpcszInviteFilePath, NULL);
  1108. }
  1109. BOOL RDPWarer_CloseInvite()
  1110. {
  1111. return SendMessage(hWarerWnd, WM_TERMINATE_INVITE, NULL, NULL);
  1112. }
  1113. BOOL RDPWarer_Disconnect()
  1114. {
  1115. return SendMessage(hWarerWnd, WM_DISCONNECT, NULL, NULL);
  1116. }