Advertisement
Guest User

Pin to Taskbar 2

a guest
Apr 5th, 2011
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. #include <windows.h>
  2. #include <shellapi.h>
  3. #include <shlwapi.h>
  4. #include <shlobj.h>
  5. #include <shobjidl.h>
  6. void PinToTaskbar(LPCWSTR path, BOOL reg){
  7.   WCHAR dir[MAX_PATH];
  8.   WCHAR file[MAX_PATH];
  9.   LPWSTR lpFilePart;
  10.   IShellFolder *pdf;
  11.   LPITEMIDLIST pidl;
  12.   HWND hwnd = GetDesktopWindow();
  13.   GetFullPathName(path, MAX_PATH,dir, &lpFilePart);
  14.   StrCpyW(file, lpFilePart);
  15.   PathRemoveFileSpec(dir);
  16.   if (FAILED(SHGetDesktopFolder(&pdf))) return;
  17.   if (SUCCEEDED(pdf->lpVtbl->ParseDisplayName(pdf, NULL, NULL, dir, NULL, &pidl,  NULL))){
  18.     IShellFolder *psf;
  19.     if (SUCCEEDED(pdf->lpVtbl->BindToObject(pdf, pidl, NULL, &IID_IShellFolder, &psf))){
  20.       LPENUMIDLIST pei;
  21.       if (SUCCEEDED(psf->lpVtbl->EnumObjects(psf, hwnd, SHCONTF_NONFOLDERS, &pei))){
  22.         LPITEMIDLIST pitm;
  23.         while (pei->lpVtbl->Next(pei, 1, &pitm, NULL) == NO_ERROR){
  24.           STRRET sr = { STRRET_WSTR, } ;
  25.           psf->lpVtbl->GetDisplayNameOf(psf, pitm, SHGDN_INFOLDER | SHGDN_FORPARSING, &sr);
  26.           if (sr.uType == STRRET_WSTR && StrCmpIW(sr.pOleStr, file) == 0){
  27.             LPCONTEXTMENU pcm;
  28.             if (SUCCEEDED(psf->lpVtbl->GetUIObjectOf(psf, hwnd, 1, &pitm, &IID_IContextMenu, NULL, &pcm))){
  29.               HMENU hmenu = CreatePopupMenu();
  30.               if (SUCCEEDED(pcm->lpVtbl->QueryContextMenu(pcm, hmenu, 0, 1, 0x7fff, CMF_EXPLORE))){
  31.                 CMINVOKECOMMANDINFO ici;
  32.                 ici.cbSize = sizeof(ici);
  33.                 ici.fMask = 0;
  34.                 ici.hwnd = hwnd;
  35.                 ici.lpVerb       = reg ? "taskbarpin" : "taskbarunpin";
  36.                 ici.lpParameters = NULL;
  37.                 ici.lpDirectory  = NULL;
  38.                 ici.nShow        = SW_SHOW;
  39.                 pcm->lpVtbl->InvokeCommand(pcm, &ici);
  40.               }
  41.               DestroyMenu(hmenu);
  42.               pcm->lpVtbl->Release(pcm);
  43.             }
  44.           }
  45.           CoTaskMemFree(pitm);
  46.         }
  47.         pei->lpVtbl->Release(pei);
  48.       }
  49.       psf->lpVtbl->Release(psf);
  50.     }
  51.     CoTaskMemFree(pidl);
  52.   }
  53.   pdf->lpVtbl->Release(pdf);
  54. }
  55. int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int nCmdShow){
  56.   int i, argc;
  57.   LPCWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc);
  58.   CoInitialize(NULL);
  59.   for (i = 1; i < argc; i++) PinToTaskbar(argv[i], TRUE);
  60.   CoUninitialize();
  61.   GlobalFree((HGLOBAL)argv);
  62.   ExitProcess(0);
  63.   return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement