5
5
#include < vector>
6
6
using std::vector;
7
7
8
+ #ifdef _DEBUG
9
+ #define new DEBUG_NEW
10
+ #endif
11
+
8
12
const int max_nick_length = 20 ;
9
13
14
+
15
+ void CHtmlScriptData::BeginThread ()
16
+ {
17
+ CoMarshalInterThreadInterfaceInStream (IID_IDispatchEx, pScriptEx, &pScStream);
18
+ hThread = (HANDLE)_beginthreadex (NULL , 0 , ThdProc, this , 0 , NULL );
19
+ }
20
+
21
+ unsigned int CHtmlScriptData::ThdProc ( void * lpParameter )
22
+ {
23
+ if (lpParameter == NULL )
24
+ {
25
+ return 0 ;
26
+ }
27
+
28
+ CHtmlScriptData* pScData = (CHtmlScriptData*)lpParameter;
29
+ pScData->DoProc ();
30
+
31
+ return 1 ;
32
+ }
33
+
34
+ void CHtmlScriptData::DoProc ()
35
+ {
36
+ CoInitialize (NULL );
37
+
38
+ // ·´Áм¯µÃµ½IDisPatchÖ¸Õë
39
+ CComPtr<IDispatchEx> pScEx;
40
+ CoGetInterfaceAndReleaseStream (pScStream, IID_IDispatchEx, (LPVOID *)&pScEx);
41
+ pScStream = NULL ;
42
+
43
+ while (bRunning)
44
+ {
45
+ iSema.Wait ();
46
+ CString nick;
47
+ iLock.Lock ();
48
+ if (ltRename.size () > 0 )
49
+ {
50
+ nick = ltRename.back ();
51
+ ltRename.clear ();
52
+ }
53
+ iLock.Unlock ();
54
+ if (nick.GetLength () == 0 )
55
+ {
56
+ continue ;
57
+ }
58
+
59
+ VARIANT arg;
60
+ arg.vt = VT_BSTR;
61
+ arg.bstrVal = nick.AllocSysString ();
62
+ DISPPARAMS dpArgs = {&arg, NULL , 1 , 0 };
63
+ HRESULT hr = pScEx->InvokeEx (lSetName, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
64
+ &dpArgs, NULL , NULL , NULL );
65
+ ::SysFreeString ( arg.bstrVal );
66
+ if (FAILED (hr))
67
+ {
68
+ // return FALSE;
69
+ }
70
+ }
71
+
72
+ ::CoUninitialize ();
73
+
74
+ iDelSema.Signal ();
75
+ }
76
+
77
+
10
78
static CHtmlWndMgr* s_pYYApiMgr = NULL ;
11
79
12
80
CHtmlWndMgr* CHtmlWndMgr::GetInstance ()
@@ -58,6 +126,14 @@ bool CHtmlWndMgr::Init(CyyListDlg* pDlg)
58
126
59
127
void CHtmlWndMgr::Release ()
60
128
{
129
+ for (map<unsigned int , CHtmlScriptData*>::iterator itr = m_mapYYid2ptr.begin ();
130
+ itr != m_mapYYid2ptr.end (); ++itr)
131
+ {
132
+ if (itr->second != NULL )
133
+ {
134
+ delete itr->second ;
135
+ }
136
+ }
61
137
m_mapYYid2ptr.clear ();
62
138
63
139
if (m_hinst != NULL )
@@ -69,7 +145,7 @@ void CHtmlWndMgr::Release()
69
145
70
146
void CHtmlWndMgr::Refresh ()
71
147
{
72
- map<unsigned int , SHtmlScriptData > mapHtmlWnd;
148
+ map<unsigned int , CHtmlScriptData* > mapHtmlWnd;
73
149
CString winClass = _T (" QWidget" );
74
150
CString ieClass = _T (" Internet Explorer_Server" );
75
151
HWND hWndQW = NULL ;
@@ -103,10 +179,10 @@ void CHtmlWndMgr::Refresh()
103
179
CString strClass = buf;
104
180
if (strClass == ieClass)
105
181
{
106
- SHtmlScriptData stData ;
107
- if (GetHtmlWnd (hWndQW, hWndIE, stData ))
182
+ CHtmlScriptData* pData = NULL ;
183
+ if (GetHtmlWnd (hWndQW, hWndIE, &pData ))
108
184
{
109
- mapHtmlWnd[stData. imId ] = stData ;
185
+ mapHtmlWnd[pData-> imId ] = pData ;
110
186
}
111
187
break ;
112
188
}
@@ -117,7 +193,7 @@ void CHtmlWndMgr::Refresh()
117
193
} while (TRUE );
118
194
} while (hWndQW != NULL );
119
195
120
- for (map<unsigned int , SHtmlScriptData >::iterator itr = mapHtmlWnd.begin ();
196
+ for (map<unsigned int , CHtmlScriptData* >::iterator itr = mapHtmlWnd.begin ();
121
197
itr != mapHtmlWnd.end (); ++itr)
122
198
{
123
199
// if (m_mapYYid2ptr.find(itr->second.imId) == m_mapYYid2ptr.end())
@@ -127,7 +203,7 @@ void CHtmlWndMgr::Refresh()
127
203
// }
128
204
}
129
205
vector<unsigned int > vtNeedDel;
130
- for (map<unsigned int , SHtmlScriptData >::iterator itr = m_mapYYid2ptr.begin ();
206
+ for (map<unsigned int , CHtmlScriptData* >::iterator itr = m_mapYYid2ptr.begin ();
131
207
itr != m_mapYYid2ptr.end (); ++itr)
132
208
{
133
209
if (mapHtmlWnd.find (itr->first ) == mapHtmlWnd.end ())
@@ -147,76 +223,61 @@ BOOL CHtmlWndMgr::ToggleYYAppVisible()
147
223
{
148
224
m_bShowYYApp = !m_bShowYYApp;
149
225
int nCmdShow = m_bShowYYApp ? SW_SHOW : SW_HIDE;
150
- for (map<unsigned int , SHtmlScriptData >::iterator itr = m_mapYYid2ptr.begin ();
226
+ for (map<unsigned int , CHtmlScriptData* >::iterator itr = m_mapYYid2ptr.begin ();
151
227
itr != m_mapYYid2ptr.end (); ++itr)
152
228
{
153
- ::ShowWindow (itr->second. hWnd, nCmdShow);
229
+ ::ShowWindow (itr->second-> hWnd, nCmdShow);
154
230
}
155
231
return m_bShowYYApp;
156
232
}
157
233
158
234
void CHtmlWndMgr::OnDelYY (unsigned int imId)
159
235
{
160
- m_mapYYid2ptr.erase (imId);
236
+ map<unsigned int , CHtmlScriptData*>::iterator _find = m_mapYYid2ptr.find (imId);
237
+ if (_find != m_mapYYid2ptr.end ())
238
+ {
239
+ if (_find->second != NULL )
240
+ {
241
+ delete _find->second ;
242
+ }
243
+ m_mapYYid2ptr.erase (_find);
244
+ }
161
245
}
162
246
163
247
BOOL CHtmlWndMgr::GetUserInfo (unsigned int imId, map<CString, VARIANT>& userInfo)
164
248
{
165
- SHtmlScriptData * pScData = GetPtrByID (imId);
249
+ CHtmlScriptData * pScData = GetPtrByID (imId);
166
250
if (pScData == NULL )
167
251
{
168
252
return FALSE ;
169
253
}
170
254
171
255
userInfo = pScData->userInfo ;
172
- // BOOL br = exeWithNoArg(pScData->pScriptEx, pScData->lGetName, userInfo);
173
- // if (!br)
174
- // {
175
- // DelYY(imId);
176
- // return FALSE;
177
- // }
178
256
179
257
return TRUE ;
180
258
}
181
259
182
260
BOOL CHtmlWndMgr::Rename (unsigned int imId, CString nick)
183
261
{
184
- SHtmlScriptData * pScData = GetPtrByID (imId);
262
+ CHtmlScriptData * pScData = GetPtrByID (imId);
185
263
if (pScData == NULL )
186
264
{
187
265
return FALSE ;
188
266
}
189
267
190
- try
268
+ if (nick. GetLength () > max_nick_length)
191
269
{
192
- VARIANT arg;
193
- arg.vt = VT_BSTR;
194
- arg.bstrVal = nick.AllocSysString ();
195
- DISPPARAMS dpArgs = {&arg, NULL , 1 , 0 };
196
- HRESULT hr = pScData->pScriptEx ->InvokeEx (pScData->lSetName , LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
197
- &dpArgs, NULL , NULL , NULL );
198
- ::SysFreeString ( arg.bstrVal );
199
- if (FAILED (hr))
200
- {
201
- return FALSE ;
202
- }
203
- }
204
- // catch (CMemoryException* e)
205
- // {
206
- //
207
- // }
208
- catch (CException* /* e*/ )
209
- {
210
- // MessageBox
211
- // bad
212
- DelYY (imId);
213
- return FALSE ;
270
+ nick.Delete (max_nick_length, nick.GetLength ()-max_nick_length);
214
271
}
272
+ pScData->iLock .Lock ();
273
+ pScData->ltRename .push_back (nick);
274
+ pScData->iLock .Unlock ();
275
+ pScData->iSema .Signal ();
215
276
216
277
return TRUE ;
217
278
}
218
279
219
- BOOL CHtmlWndMgr::GetHtmlWnd (HWND hWndQW, HWND hwndIE, SHtmlScriptData& stData )
280
+ BOOL CHtmlWndMgr::GetHtmlWnd (HWND hWndQW, HWND hwndIE, CHtmlScriptData** pData )
220
281
{
221
282
if (m_pfObjectFromLresult == NULL )
222
283
{
@@ -328,53 +389,67 @@ BOOL CHtmlWndMgr::GetHtmlWnd(HWND hWndQW, HWND hwndIE, SHtmlScriptData& stData)
328
389
return FALSE ;
329
390
}
330
391
331
- stData.hWnd = hWndQW;
332
- stData.pScriptEx = pScUserEx;
333
- stData.lSetName = dispidSet;
334
- stData.lGetName = dispidGet;
335
- stData.imId = userInfo[_T (" imId" )].uintVal ;
336
- stData.userInfo = userInfo;
392
+ *pData = new CHtmlScriptData ();
393
+ (*pData)->hWnd = hWndQW;
394
+ (*pData)->pScriptEx = pScUserEx;
395
+ (*pData)->lSetName = dispidSet;
396
+ (*pData)->lGetName = dispidGet;
397
+ (*pData)->imId = userInfo[_T (" imId" )].uintVal ;
398
+ (*pData)->userInfo = userInfo;
337
399
338
400
return TRUE ;
339
401
}
340
402
341
- unsigned int CHtmlWndMgr::AddYY (SHtmlScriptData& stData )
403
+ unsigned int CHtmlWndMgr::AddYY (CHtmlScriptData* pData )
342
404
{
343
- if (stData. pScriptEx == NULL )
405
+ if (pData == NULL || pData-> pScriptEx == NULL )
344
406
{
345
407
return 0 ;
346
408
}
347
409
348
- if (m_mapYYid2ptr.find (stData. imId ) == m_mapYYid2ptr.end ())
410
+ if (m_mapYYid2ptr.find (pData-> imId ) == m_mapYYid2ptr.end ())
349
411
{
350
- m_mapYYid2ptr[stData.imId ] = stData;
351
- m_pDlg->AddYY (stData.imId );
412
+ m_mapYYid2ptr[pData->imId ] = pData;
413
+ m_pDlg->AddYY (pData->imId );
414
+
415
+ // thread
416
+ m_mapYYid2ptr[pData->imId ]->BeginThread ();
352
417
}
353
418
else
354
419
{
355
- m_mapYYid2ptr[stData.imId ].pScriptEx = stData.pScriptEx ;
356
- m_mapYYid2ptr[stData.imId ].lGetName = stData.lGetName ;
357
- m_mapYYid2ptr[stData.imId ].lSetName = stData.lSetName ;
420
+ m_mapYYid2ptr[pData->imId ]->pScriptEx = pData->pScriptEx ;
421
+ m_mapYYid2ptr[pData->imId ]->lGetName = pData->lGetName ;
422
+ m_mapYYid2ptr[pData->imId ]->lSetName = pData->lSetName ;
423
+ delete pData;
358
424
}
359
425
360
- return stData. imId ;
426
+ return pData-> imId ;
361
427
}
362
428
363
429
BOOL CHtmlWndMgr::DelYY (unsigned int imId)
364
430
{
365
- m_mapYYid2ptr.erase (imId);
431
+ map<unsigned int , CHtmlScriptData*>::iterator _find = m_mapYYid2ptr.find (imId);
432
+ if (_find == m_mapYYid2ptr.end ())
433
+ {
434
+ return FALSE ;
435
+ }
436
+
437
+ delete _find->second ;
438
+
439
+ m_mapYYid2ptr.erase (_find);
366
440
m_pDlg->DelYY (imId);
441
+
367
442
return TRUE ;
368
443
}
369
444
370
- CHtmlWndMgr::SHtmlScriptData * CHtmlWndMgr::GetPtrByID (unsigned int imId)
445
+ CHtmlScriptData * CHtmlWndMgr::GetPtrByID (unsigned int imId)
371
446
{
372
- map<unsigned int , SHtmlScriptData >::iterator _find = m_mapYYid2ptr.find (imId);
447
+ map<unsigned int , CHtmlScriptData* >::iterator _find = m_mapYYid2ptr.find (imId);
373
448
if (_find == m_mapYYid2ptr.end ())
374
449
{
375
450
return NULL ;
376
451
}
377
- return & _find->second ;
452
+ return _find->second ;
378
453
}
379
454
380
455
BOOL CHtmlWndMgr::exeWithNoArg (CComPtr<IDispatchEx> pScEx, DISPID dispid, map<CString, VARIANT>& result)
0 commit comments