使用soui开发的mbc,只支持windows版本
w1146869587
2022-01-24 4905e2e7537d507f218e8e9595485e09d9f3a2b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// MBShellExt.cpp : Implementation of CMBShellExt
 
#include "stdafx.h"
#include "MBShellExt.h"
 
#pragma comment(lib,"shlwapi.lib") 
#pragma message("Automatically linking with Shlwapi.dll") 
 
// CMBShellExt
 
#define        REG_SUBKEY                    _T( "Software\\Activesoft\\MBClient" )
 
#define    PSW_EMKEY                        ( _T("P512D790") )    // ÊÇAPM\EMÉϵÄÃÜÔ¿
 
// CMBShellExt
#define    AMCADDIN_FLAG_SHELLEX_MENU        ( 16384 )            // ÔÊÐí¼ÓÈë×ÊÔ´¹ÜÀíÆ÷ÓÒ¼ü²Ëµ¥ÖÐ
 
HRESULT CMBShellExt::Initialize( LPCITEMIDLIST pidlFolder, LPDATAOBJECT pDataObj, HKEY hProgID )
{
    FORMATETC    fmt    = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
    STGMEDIUM    stg    = { TYMED_HGLOBAL };
    HDROP        hDrop;
 
    // Look for CF_HDROP data in the data object.
    if ( FAILED( pDataObj->GetData( &fmt, &stg ) ) )
    {
        // Nope! Return an "invalid argument" error back to Explorer.
        return    E_INVALIDARG;
    }
 
    // Get a pointer to the actual data.
    hDrop    = (HDROP)GlobalLock( stg.hGlobal );
    if ( NULL == hDrop )
        return    E_INVALIDARG;
 
    // Note that it's vitally important to error-check everything, especially pointers. Since our extension runs in Explorer's process space, if we crash we take down Explorer too. On 9x, such a crash might necessitate rebooting the computer.
 
    // So, now that we have an HDROP handle, we can get the filename we need.
 
    // Sanity check ¨C make sure there is at least one filename.
    UINT    uNumFiles    = DragQueryFile( hDrop, 0xFFFFFFFF, NULL, 0 );
    if ( 0 == uNumFiles )
    {
        GlobalUnlock( stg.hGlobal );
        ReleaseStgMedium( &stg );
        return    E_INVALIDARG;
    }
 
    HRESULT        hr    = S_OK;
    TCHAR        szFile[MAX_PATH];
    UINT        nLen;
 
    // get multi files
    for ( UINT uFile = 0; uFile < uNumFiles; uFile++ )
    {
        // Get the next filename.
        nLen    = DragQueryFile( hDrop, uFile, szFile, MAX_PATH );
        if ( nLen == 0 )
            continue;
 
        if ( PathIsDirectory ( szFile ) )
        {
            szFile[nLen]    = '\\';
            szFile[nLen+1]    = '\0';
        }
 
        m_lsFiles.AddTail( szFile );
    }   // end for
 
       GlobalUnlock( stg.hGlobal );
    ReleaseStgMedium( &stg );
    return    hr;
}
 
HRESULT CMBShellExt::QueryContextMenu( HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd, UINT uidLastCmd, UINT uFlags )
{
    // If the flags include CMF_DEFAULTONLY then we shouldn't do anything.
    if ( uFlags & CMF_DEFAULTONLY )
        return    MAKE_HRESULT( SEVERITY_SUCCESS, FACILITY_NULL, 0 );
 
    int                nNum = 0;
    POSITION        pos;
    CString            strDispName;
    CString            strMenuName;
    
 
    strMenuName = _T("MBCÉÏ´«");//µ½Îĵµ¹ñ
    InsertMenu( hmenu, uMenuIndex,  MF_BYPOSITION, uidFirstCmd + nNum, strMenuName );
    ++nNum;
    HBITMAP hBitmap= GetSTKBitmap(1);
    if(hBitmap)
    {
        SetMenuItemBitmaps(hmenu,
        uMenuIndex,
        MF_BYPOSITION,
        hBitmap,
        hBitmap);
    }
    /*strMenuName = _T("MBCÉÏ´«µ½¸öÈËÍøÅÌ");
    InsertMenu( hmenu, uMenuIndex,  MF_BYPOSITION, uidFirstCmd + nNum, strMenuName );
    ++nNum;
    hBitmap= GetSTKBitmap(1);
    if(hBitmap)
    {
        SetMenuItemBitmaps(hmenu,
        uMenuIndex,
        MF_BYPOSITION,
        hBitmap,
        hBitmap);
    }*/
    for ( pos = m_lsAddInDispName.GetHeadPosition(); pos; )
    {
        strDispName = m_lsAddInDispName.GetNext( pos );
        InsertMenu( hmenu, uMenuIndex,  MF_BYPOSITION, uidFirstCmd + nNum, strDispName );
        ++nNum;
    }
 
    strMenuName = _T("MBCÒ×·¢");    
    InsertMenu( hmenu, uMenuIndex,  MF_BYPOSITION, uidFirstCmd + nNum, strMenuName );
    hBitmap= GetSTKBitmap();
    if(hBitmap)
    {
        SetMenuItemBitmaps(hmenu,
        uMenuIndex,
        MF_BYPOSITION,
        hBitmap,
        hBitmap);
    }
    return    MAKE_HRESULT( SEVERITY_SUCCESS, FACILITY_NULL, nNum + 1 );
}
 
#ifdef _WIN64
HRESULT CMBShellExt::GetCommandString(UINT_PTR idCmd, UINT uFlags, UINT* pwReserved, LPSTR pszName, UINT cchMax)
{
     USES_CONVERSION;
 
    // Check idCmd, it must be 0 since we have only one menu item.
    if ( idCmd < 0 && idCmd > (UINT)m_lsAddInDispName.GetCount() )
        return    E_INVALIDARG;
 
    // If Explorer is asking for a help string, copy our string into the
    // supplied buffer.
    if ( uFlags & GCS_HELPTEXT )
    {
        CString  strText;
        LPCTSTR    szText = _T("");
        if(idCmd == 0)
        {
            szText =  _T("MBCÒ×·¢");
        }
        else 
        {
            strText.Format( _T("This is the MBC shell extension's help %d"), idCmd);
            szText = strText;
        }
        if ( uFlags & GCS_UNICODE )
        {
            // We need to cast pszName to a Unicode string, and then use the
            // Unicode string copy API.
            lstrcpynW( (LPWSTR) pszName, T2CW(szText), cchMax );
        }
        else
        {
            // Use the ANSI string copy API to return the help string.
            lstrcpynA( pszName, T2CA(szText), cchMax );
        }
 
        return S_OK;
    }
 
    return E_INVALIDARG;
}
 
#else
HRESULT CMBShellExt::GetCommandString( UINT idCmd, UINT uFlags, UINT* pwReserved, LPSTR pszName, UINT cchMax )
{
    USES_CONVERSION;
 
    // Check idCmd, it must be 0 since we have only one menu item.
    if ( idCmd < 0 && idCmd > (UINT)m_lsAddInDispName.GetCount() )
        return    E_INVALIDARG;
 
    // If Explorer is asking for a help string, copy our string into the
    // supplied buffer.
    if ( uFlags & GCS_HELPTEXT )
    {
        LPCTSTR    szText = _T("This is the MBC shell extension's help");
 
        if ( uFlags & GCS_UNICODE )
        {
            // We need to cast pszName to a Unicode string, and then use the
            // Unicode string copy API.
            lstrcpynW( (LPWSTR) pszName, T2CW(szText), cchMax );
        }
        else
        {
            // Use the ANSI string copy API to return the help string.
            lstrcpynA( pszName, T2CA(szText), cchMax );
        }
 
        return S_OK;
    }
 
    return E_INVALIDARG;
}
#endif
 
HRESULT CMBShellExt::InvokeCommand( LPCMINVOKECOMMANDINFO pCmdInfo )
    // If lpVerb really points to a string, ignore this function call and bail out.
    int        nMenuID = 0;
 
    // Get the command index - the only valid one is 0.
    nMenuID = LOWORD( pCmdInfo->lpVerb );
 
 
    if ( 0 != HIWORD( pCmdInfo->lpVerb ) )
    {
        return    E_INVALIDARG;
    }
    
    if ( nMenuID >= 0 && nMenuID <= m_lsAddInDispName.GetCount( ) + 2 )
    {
#ifdef _WIN64
        CString            strAMViewPath;
 
        if ( DoSTKCom( nMenuID, pCmdInfo->hwnd ) )
            return S_OK;
        else
            return S_FALSE;
#else
        if ( DoSTKCom( nMenuID, pCmdInfo->hwnd ) )
            return S_OK;
        else
            return S_FALSE;
#endif
 
        return S_OK;
    }
 
   return E_INVALIDARG;
}
 
bool CMBShellExt::DoSTKCom( long nMenuID, HWND hWnd )
{
    HRESULT            hr;
    CString            strMBExe;
    strMBExe.Format( _T("%s\\mbcU.exe"), GetSTKWorkDir());
    HANDLE hEventShareMemory  =   OpenEvent(EVENT_ALL_ACCESS , FALSE, _T("mbcSharedMemoryEvent")); 
    if (NULL == hEventShareMemory)
    {
        long nErrorId = GetLastError();
        CString  strMessage;
        strMessage.Format(_T("OpenEvent mbcSharedMemoryEvent ErrorID:%d"),nErrorId);
        WriteLog(strMessage);
        ShellExecute( NULL, _T("open"), strMBExe, _T(""), _T(""), SW_SHOW );
        //MessageBox(hWnd, strMessage, _T("mbEasySend"), MB_OK | MB_ICONINFORMATION );
        return    false;
    }
    HANDLE hShareMemory = OpenFileMapping( PAGE_READONLY,false,_T("mbcSharedMemory") );
    if (NULL == hShareMemory)
    {
        /*m_hShareMemory = CreateFileMapping(HANDLE(0xFFFFFFFF), NULL, PAGE_READWRITE,0, 2560, _T("mbcSharedMemory"));
        if (NULL == m_hShareMemory)
        {*/
            long nErrorId = GetLastError();
            CString  strMessage;
            strMessage.Format(_T("OpenFileMapping mbcSharedMemory ErrorID:%d"),nErrorId);
            //MessageBox(hWnd, strMessage, _T("mbEasySend"), MB_OK | MB_ICONINFORMATION );
            ShellExecute( NULL, _T("open"), strMBExe, _T(""), _T(""), SW_SHOW );
            WriteLog(strMessage);
            return    false;
        //}
    }
    TCHAR* lpBuffer = (TCHAR*)MapViewOfFile(hShareMemory, FILE_MAP_WRITE, 0, 0, 2560);
    if (NULL == lpBuffer)
    {
        //MessageBox(hWnd, _T("MapViewOfFile mbcSharedMemoryʧ°Ü"), _T("mbEasySend"), MB_OK | MB_ICONINFORMATION );
        long nErrorId = GetLastError();
        CString  strMessage;
        strMessage.Format(_T("MapViewOfFile mbcSharedMemory ErrorID:%d"),nErrorId);
        WriteLog(strMessage);
        return    false;
    }
    POSITION    pos;
    CString  strBuf;
    CString  strXmlBuf;
    CString  strAddInName = _T("");
    int            nType = 0;
    if(nMenuID == 0 )//|| nMenuID == 1
        nType = 1;
    if ( nMenuID -1 > m_lsAddInName.GetCount() )
    {
        pos = m_lsAddInName.FindIndex( nMenuID - 1 );
        if ( pos )
        {
            strAddInName = m_lsAddInName.GetAt( pos );
        }
    }
    strXmlBuf.Format(_T("<body type='%d' addin='%s'>"),nType,strAddInName);
    CString        strFile;
    for ( pos = m_lsFiles.GetHeadPosition(); pos; )
    {
        strFile = m_lsFiles.GetNext( pos );
        strBuf.Format( _T("<file>%s</file>"),strFile  );
        if(strXmlBuf.GetLength() + strBuf.GetLength()> 2550)
        {
            break;
        }
        strXmlBuf += strBuf;
    }
    strXmlBuf += _T("</body>");
    _stprintf(lpBuffer,_T("%s"), strXmlBuf );
    UnmapViewOfFile(lpBuffer);
    SetEvent(hEventShareMemory);
    CloseHandle(hEventShareMemory);
    CloseHandle(hShareMemory);
    return true;
}
 
void CMBShellExt::GetAddInMenu( )
{
    HKEY            hKey;
    DWORD            dwBuf    = MAX_PATH;
    DWORD            dwIndex = 0;
    CRegKey            myKey;    
    CString            strSubPath, strDispName;
    CString            strName, strValue;
    TCHAR            szValue[256]= { 0 };
    TCHAR            szName[80]    = { 0 };
    DWORD            dwReaded    = 0;
    DWORD            dwValue        = 0;
    int                nFlag        = 0;
 
    RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\ActiveSoft\\MBClient\\AMCAddIn"), 0, KEY_ENUMERATE_SUB_KEYS, &hKey);
 
    m_lsAddInName.RemoveAll();
    m_lsAddInDispName.RemoveAll();
    while (RegEnumKey(hKey, dwIndex, szName, dwBuf) == 0) 
    { 
        dwIndex++;
 
        szName[dwBuf]    = '\0';
        strName            = szName;        
        strDispName        = _T("");
        strSubPath.Format( _T("SOFTWARE\\ActiveSoft\\MBClient\\AMCAddIn\\%s"), strName );
        myKey.Close( );
        if ( myKey.Open( HKEY_LOCAL_MACHINE, strSubPath, KEY_READ ) == ERROR_SUCCESS )
        {
            dwReaded    = 255;
            nFlag        = 0;            
 
            if ( ERROR_SUCCESS == myKey.QueryValue( dwValue, _T( "Flag" ) ) )
                nFlag = dwValue;
        
            if ( nFlag & AMCADDIN_FLAG_SHELLEX_MENU )
            {
    #ifdef AFX_TARG_CHS
                if ( ERROR_SUCCESS == myKey.QueryValue( szValue, _T( "DisplayName" ), &dwReaded ) && dwReaded > 0 )
                {
                    szValue[dwReaded] = '\0';
                    strDispName = szValue;
                }
    #else
                if ( ERROR_SUCCESS == myKey.QueryValue( szValue, _T( "DisplayName_ENU" ), &dwReaded ) && dwReaded > 0 )
                {
                    szValue[dwReaded] = '\0';
                    strDispName = szValue;
                }
    #endif
                if ( strDispName.IsEmpty() )
                    strDispName = strName;
                m_lsAddInDispName.AddTail( strDispName );
                m_lsAddInName.AddTail( strName );
            }
 
            myKey.Close( );
        }
        else
        {
            continue;
        }
    }
 
    RegCloseKey(hKey);
}
 
bool CMBShellExt::CStringToAnsi( LPCTSTR pszT,  LPSTR *ppszA  )
{
    ULONG        cbAnsi, cCharacters;
 
    if ( pszT == NULL )
    {
        *ppszA= NULL;
        return false;
    }
#ifdef _UNICODE  
    cCharacters = wcslen(pszT);
    cbAnsi = ::WideCharToMultiByte( CP_ACP, 0, pszT, -1, NULL, 0, NULL, NULL );
    *ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi+1);
    if ( NULL == *ppszA )
        return false;
    ZeroMemory(*ppszA,cbAnsi+1);
    // Convert to ANSI.
    if (0 == WideCharToMultiByte(CP_ACP, 0, pszT, cCharacters, *ppszA,
                  cbAnsi, NULL, NULL))
    {
        CoTaskMemFree(*ppszA);
        *ppszA = NULL;
        return false;
    }
#else
    cCharacters = strlen(pszT);
    *ppszA = (LPSTR) CoTaskMemAlloc(cCharacters+1);
    if ( NULL == *ppszA )
        return false;
    ZeroMemory(*ppszA,cCharacters+1);
    memcpy(*ppszA,pszT,cCharacters);
#endif
     return true;
}
 
CString CMBShellExt::GetSTKWorkDir()
{
    CString        strWorkDir;
    CRegKey        reg;
    CString        strSubKey;
    TCHAR        szValue[256]    = { 0 };
    DWORD        dwReaded;
 
    strSubKey.Format( _T("%s"), REG_SUBKEY );
    if ( ERROR_SUCCESS == reg.Open( HKEY_LOCAL_MACHINE, strSubKey, KEY_READ ) )
    {
        dwReaded    = 255;
        if ( ERROR_SUCCESS == reg.QueryStringValue( _T( "WorkDir" ), szValue, &dwReaded ) && dwReaded > 0 )
        {
            strWorkDir     = szValue;
            strWorkDir.TrimRight();
            strWorkDir.TrimRight( '\\' );
        }
 
        reg.Close();
    }
    if ( strWorkDir.IsEmpty() )
        strWorkDir =GetModulePath();
    return    strWorkDir;
}
 
 
 
// Ð´ÈÕÖ¾
void CMBShellExt::WriteLog( CString strLog )
{
    CTime        tmCurrent    = CTime::GetCurrentTime();
    CString        strTime        = tmCurrent.Format( _T(" %Y-%m-%d %H:%M:%S = ") );
    CString        strEnter    = _T(" \r\n ");
    CString        strLogPath    = _T("  ");
    CString        strBuff;
    LPSTR        pDest ;
    CFile        file;
 
    strLogPath.Format( _T("%s\\mbEasySend.log"),GetModulePath());
    if ( !file.Open(strLogPath, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate) )
    {
        return ;
    }
    file.Seek( 0, CFile::end );
 
    strBuff.Format( _T("%s%s%s"),strTime, strLog,strEnter );
    if ( CStringToAnsi(strBuff, &pDest))
    {
        file.Write( (void*)pDest, strlen(pDest));
        CoTaskMemFree(pDest);
    }
    file.Close();
}
HBITMAP CMBShellExt::GetSTKBitmap(int nMenuType)
{
     HINSTANCE    hInstance = NULL;
    HBITMAP        hBitmap = NULL;
#ifdef _WIN64
    hInstance = ::GetModuleHandle( _T("mbEasySendW.dll") );
#else
    hInstance = ::GetModuleHandle( _T("mbEasySend.dll") );
#endif
    if(nMenuType == 0)
        hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_AM) );//IDB_USERSTAT
    else
        hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_MBC) );//IDB_USERSTAT
    HIMAGELIST hImageList1= ImageList_Create(16,16,ILC_COLORDDB|ILC_MASK ,1,2); //
    ImageList_SetBkColor(hImageList1, GetSysColor( COLOR_MENU ) );
    ImageList_AddMasked(hImageList1, hBitmap, RGB(255,0,255));//ÉèÖð×ɫΪ͸Ã÷É«
    //ImageList_Add( hImageList1, hBitmap,NULL); //AfxGetInstanceHandle()
    CWnd *pWnd = AfxGetMainWnd();            // Get main window
    if (pWnd == NULL) 
        pWnd = CWnd::GetDesktopWindow();
    if(!pWnd)
        return NULL;
    CDC *pDC = pWnd->GetDC();              // Get device context
    if(!pDC)
        return NULL;
    HBITMAP  hBmp;
    HICON hIcon = ImageList_GetIcon(hImageList1,0,0);
    HDC hTmpDc;
    hTmpDc = CreateCompatibleDC(pDC->GetSafeHdc());
    hBmp = CreateCompatibleBitmap(pDC->GetSafeHdc(),16,16);
    HBITMAP hOldBmp = (HBITMAP)::SelectObject(hTmpDc,hBmp);
    HBRUSH hBrush ;
    COLORREF newclrBack;
    newclrBack=GetSysColor(COLOR_3DFACE);
    hBrush =CreateSolidBrush(newclrBack);
    ::DrawIconEx(
        hTmpDc,
        0,
        0,
        hIcon,
        16,
        16,
        0,
        hBrush,
        DI_NORMAL
        );    
    (HBITMAP)::SelectObject(hTmpDc, hOldBmp );
    DeleteDC(hTmpDc);
    ::DestroyIcon(hIcon);
    pWnd->ReleaseDC(pDC);
    return hBmp;
}
// »ñȡִÐгÌÐòËùÔÚµÄĿ¼
CString CMBShellExt::GetModulePath()
{
    CString        strFileName;
     HINSTANCE    hInstance = NULL;
    #ifdef _WIN64
        hInstance = ::GetModuleHandle( _T("mbEasySendW.dll") );
    #else
        hInstance = ::GetModuleHandle( _T("mbEasySend.dll") );
    #endif
    if (m_strBinPath.IsEmpty( ))
    {
        int        nPos;
 
        GetModuleFileName( hInstance, strFileName.GetBuffer( MAX_PATH ), MAX_PATH );
        strFileName.ReleaseBuffer();
        strFileName = GetPathName( strFileName );
        return    strFileName;
    }
    else
        return m_strBinPath;
}
// È¡È«Â·¾¶ÎļþÃûÖеÄ·¾¶
CString CMBShellExt::GetPathName( CString strFileName )
{
    int            nPos;
    CString        strPathName;
 
    if ( strFileName.IsEmpty() )
        return    _T( "" );
 
    nPos = strFileName.ReverseFind( '\\' );
    if ( nPos < 0 )
        return    _T( "" );
 
    strPathName    = strFileName.Left( nPos );
    return    strPathName;
}