Guest User

Untitled

a guest
Dec 18th, 2011
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. // DDShellExt.cpp : Implementation of CDDShellExt
  2.  
  3. #include "stdafx.h"
  4. #include "DDShellExt.h"
  5. #include "ClientCatchcopy.h"
  6.  
  7. #ifndef _M_X64
  8. const CLSID CLSID_DDShellExt = {0x68D44A27,0xFFB6,0x4B89,{0xA3,0xE5,0x7B,0x0E,0x50,0xA7,0xAB,0x33}};
  9. #else
  10. const CLSID CLSID_DDShellExt = {0x68ff37c4,0x51bc,0x4c2a,{0xa9,0x92,0x7e,0x39,0xbc,0xe,0x70,0x6f}};
  11. #endif
  12.  
  13. STDMETHODIMP CDDShellExt::Initialize(LPCITEMIDLIST pidlFolder,LPDATAOBJECT pDO,HKEY hProgID)
  14. {
  15.     if(!connected)
  16.     {
  17.         bool b = m_ac.connectToServer();
  18.  
  19.         if (b==true)
  20.         {
  21.             connected=true;
  22.             bool f = m_ac.sendProtocol();
  23.             if(f!=true)
  24.                 return E_FAIL;
  25.             #if defined(_M_X64)
  26.                 m_ac.setClientName(L"Windows Explorer 64Bits");
  27.             #else
  28.                 m_ac.setClientName(L"Windows Explorer 32Bits");
  29.             #endif
  30.         }
  31.         else
  32.             return E_FAIL;
  33.     }
  34.  
  35.     FORMATETC fmt={CF_HDROP,NULL,DVASPECT_CONTENT,-1,TYMED_HGLOBAL};
  36.     STGMEDIUM stg={TYMED_HGLOBAL};
  37.     HDROP hDrop;
  38.  
  39.     fDestDir[0]=0;
  40.     if (!SHGetPathFromIDList(pidlFolder,fDestDir))
  41.     {
  42.         MessageBox ( NULL,L"E_FAIL 1", L"E_FAIL 1", MB_OK);
  43.         return E_FAIL;
  44.     }
  45.  
  46.     // Detect if it's explorer that started the operation by enumerating available
  47.     // clipboard formats and searching for one that only explorer uses
  48.     IEnumFORMATETC *en;
  49.     FORMATETC fmt2;
  50.     WCHAR fmtName[256]=L"\0";
  51.     fFromExplorer=false;
  52.     pDO->EnumFormatEtc(DATADIR_GET,&en);
  53.     while(en->Next(1,&fmt2,NULL)==S_OK){
  54.         GetClipboardFormatName(fmt2.cfFormat,fmtName,256);
  55.         if (!wcscmp(fmtName,CFSTR_SHELLIDLIST)) fFromExplorer=true;
  56.     }
  57.     en->Release();
  58.  
  59.     // Look for CF_HDROP data in the data object. If there
  60.     // is no such data, return an error back to Explorer.
  61.     if (FAILED(pDO->GetData(&fmt,&stg)))
  62.     {
  63.         MessageBox ( NULL,L"E_INVALIDARG 2", L"E_INVALIDARG 2", MB_OK);
  64.         return E_INVALIDARG;
  65.     }
  66.  
  67.     // Get a pointer to the actual data.
  68.     hDrop=(HDROP)GlobalLock(stg.hGlobal);
  69.  
  70.     // Make sure it worked.
  71.     if (hDrop==NULL)
  72.     {
  73.         MessageBox ( NULL,L"E_INVALIDARG 1", L"E_INVALIDARG 1", MB_OK);
  74.         return E_INVALIDARG;
  75.     }
  76.  
  77.     UINT numFiles,i;
  78.     WCHAR fn[MAX_PATH]=L"";
  79.  
  80.     numFiles=DragQueryFile(hDrop,0xFFFFFFFF,NULL,0);
  81.  
  82.     if (numFiles)
  83.     {
  84.         for(i=0;i<numFiles;++i)
  85.         {
  86.             if(DragQueryFile(hDrop,i,fn,MAX_PATH))
  87.                 sources.push_back(fn);
  88.         }
  89.     }
  90.  
  91.     GlobalUnlock(stg.hGlobal);
  92.     ReleaseStgMedium(&stg);
  93.  
  94.     return S_OK;
  95. }
  96.  
  97. STDMETHODIMP CDDShellExt::QueryContextMenu(HMENU hmenu,UINT uMenuIndex,UINT uidFirstCmd,UINT uidLastCmd,UINT uFlags)
  98. {
  99.     if(!m_ac.isConnected())
  100.     {
  101.         if(!m_ac.connectToServer())
  102.             return E_FAIL;
  103.     }
  104.     if (uFlags&CMF_DEFAULTONLY)
  105.         return MAKE_HRESULT(SEVERITY_SUCCESS,FACILITY_NULL,0);
  106.  
  107.     int x=uidFirstCmd;
  108.  
  109.     InsertMenu(hmenu,uMenuIndex++,MF_STRING|MF_BYPOSITION,x++,_T("Copy"));
  110.     InsertMenu(hmenu,uMenuIndex++,MF_STRING|MF_BYPOSITION,x++,_T("Move"));
  111.  
  112.     int defItem=GetMenuDefaultItem(hmenu,false,0);
  113.     if (defItem==1) // 1: Copy
  114.     {
  115.         if (fFromExplorer)
  116.             SetMenuDefaultItem(hmenu,uidFirstCmd+defItem-1,false);
  117.     }
  118.     else if (defItem==2)
  119.     {
  120.         SetMenuDefaultItem(hmenu,uidFirstCmd+defItem-1,false);
  121.     }
  122.     return MAKE_HRESULT(SEVERITY_SUCCESS,FACILITY_NULL,2);
  123. }
  124.  
  125.  
  126. STDMETHODIMP CDDShellExt::InvokeCommand ( LPCMINVOKECOMMANDINFO pInfo )
  127. {
  128.     if(HIWORD(pInfo->lpVerb))
  129.         return E_INVALIDARG;
  130.     switch(LOWORD(pInfo->lpVerb))
  131.     {
  132.         case 0:// copy
  133.             if(!m_ac.addCopyWithDestination(sources,fDestDir))
  134.                 return E_FAIL;
  135.         break;
  136.         case 1:// move
  137.             if(!m_ac.addMoveWithDestination(sources,fDestDir))
  138.                 return E_FAIL;
  139.         break;
  140.         default :
  141.             return S_OK;
  142.     }
  143.     return S_OK;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment