diff --git a/sendto+.c b/sendto+.c index 36cc0d1..bb076f2 100644 --- a/sendto+.c +++ b/sendto+.c @@ -29,37 +29,36 @@ lnk file with parameters need 64-bit version! #include // tcc, any other compiler support wWinMain() and '__argc/__targv' -//shlguid.h +// shlguid.h DEFINE_GUID(IID_IDropTarget, 0x00000122, 0x0000, 0x0000, 0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46); DEFINE_GUID(IID_IDataObject, 0x0000010e, 0x0000, 0x0000, 0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46); -// #define WINGDIPAPI __stdcall #define GDIPCONST const typedef enum GpStatus { - Ok = 0, - GenericError = 1, - InvalidParameter = 2, - OutOfMemory = 3, - ObjectBusy = 4, - InsufficientBuffer = 5, - NotImplemented = 6, - Win32Error = 7, - WrongState = 8, - Aborted = 9, - FileNotFound = 10, - ValueOverflow = 11, - AccessDenied = 12, - UnknownImageFormat = 13, - FontFamilyNotFound = 14, - FontStyleNotFound = 15, - NotTrueTypeFont = 16, - UnsupportedGdiplusVersion = 17, - GdiplusNotInitialized = 18, - PropertyNotFound = 19, - PropertyNotSupported = 20, - ProfileNotFound = 21 + Ok = 0, + GenericError = 1, + InvalidParameter = 2, + OutOfMemory = 3, + ObjectBusy = 4, + InsufficientBuffer = 5, + NotImplemented = 6, + Win32Error = 7, + WrongState = 8, + Aborted = 9, + FileNotFound = 10, + ValueOverflow = 11, + AccessDenied = 12, + UnknownImageFormat = 13, + FontFamilyNotFound = 14, + FontStyleNotFound = 15, + NotTrueTypeFont = 16, + UnsupportedGdiplusVersion = 17, + GdiplusNotInitialized = 18, + PropertyNotFound = 19, + PropertyNotSupported = 20, + ProfileNotFound = 21 } GpStatus; typedef DWORD ARGB; @@ -69,41 +68,36 @@ typedef GpStatus (WINGDIPAPI *NotificationHookProc)(ULONG_PTR *token); typedef VOID (WINGDIPAPI *NotificationUnhookProc)(ULONG_PTR token); typedef struct GdiplusStartupInput { - UINT32 GdiplusVersion; - DebugEventProc DebugEventCallback; - BOOL SuppressBackgroundThread; - BOOL SuppressExternalCodecs; + UINT32 GdiplusVersion; + DebugEventProc DebugEventCallback; + BOOL SuppressBackgroundThread; + BOOL SuppressExternalCodecs; } GdiplusStartupInput; typedef struct GdiplusStartupOutput { - NotificationHookProc NotificationHook; - NotificationUnhookProc NotificationUnhook; + NotificationHookProc NotificationHook; + NotificationUnhookProc NotificationUnhook; } GdiplusStartupOutput; -GpStatus WINGDIPAPI GdiplusStartup(ULONG_PTR*,GDIPCONST GdiplusStartupInput*,GdiplusStartupOutput*); +GpStatus WINGDIPAPI GdiplusStartup(ULONG_PTR *, GDIPCONST GdiplusStartupInput *, GdiplusStartupOutput *); VOID WINGDIPAPI GdiplusShutdown(ULONG_PTR); - -GpStatus WINGDIPAPI -GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap); - -GpStatus WINGDIPAPI -GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap, HBITMAP* hbmReturn, ARGB background); - -VOID WINGDIPAPI GdipFree(VOID*); +GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap **bitmap); +GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap *bitmap, HBITMAP *hbmReturn, ARGB background); +VOID WINGDIPAPI GdipFree(VOID *); #define IDM_SENDTOFIRST 0 -TCHAR *FOLDER_SENDTO; -UINT idm_g = IDM_SENDTOFIRST; -TCHAR **PSENDTO; /* store the shourtcuts full path */ -// -HBITMAP *hBmpImageA; /* MenuItemBitmap */ +TCHAR *g_FOLDER_SENDTO; +TCHAR **g_PSENDTO; /* Store the shourtcuts full path */ +UINT g_idm = IDM_SENDTOFIRST; + +HBITMAP *g_hBmpImageA; /* MenuItemBitmap */ HINSTANCE g_hinst; /* My hinstance */ -HMENU g_hmenuSendTo; /* Our SendTo popup */ +HMENU g_hmenuSendTo; /* My SendTo popup */ LPSHELLFOLDER g_psfDesktop; /* The desktop folder */ -UINT FORKING = 0; /* compatible with UAC focus changes */ +UINT g_FORKING = 0; /* compatible with UAC focus changes */ LPSHELLFOLDER PIDL2PSF(LPITEMIDLIST pidl) { @@ -111,7 +105,6 @@ LPSHELLFOLDER PIDL2PSF(LPITEMIDLIST pidl) if (pidl) { g_psfDesktop->lpVtbl->BindToObject(g_psfDesktop, pidl, NULL, &IID_IShellFolder, (LPVOID *)&psf); - // failed: got NULL } return psf; } @@ -123,10 +116,10 @@ LPITEMIDLIST PidlFromPath(HWND hwnd, LPCTSTR pszPath) DWORD dwAttributes; HRESULT hres; WCHAR *wszName; - // + wszName = calloc(T_MAX_PATH + 1, sizeof(WCHAR)); #ifdef UNICODE - if (wcslen(pszPath) >= T_MAX_PATH) { return NULL; } + if (wcslen(pszPath) >= T_MAX_PATH) {return NULL;} wcscpy(wszName, pszPath); #else if (!MultiByteToWideChar(CP_ACP, 0, pszPath, -1, wszName, T_MAX_PATH)) { @@ -138,7 +131,6 @@ LPITEMIDLIST PidlFromPath(HWND hwnd, LPCTSTR pszPath) if (FAILED(hres)) { return NULL; } - return pidl; } @@ -147,15 +139,9 @@ LPSHELLFOLDER GetFolder(HWND hwnd, LPCTSTR pszPath) LPITEMIDLIST pidl; pidl = PidlFromPath(hwnd, pszPath); - return PIDL2PSF(pidl); } -/***************************************************************************** - * GetUIObjectOfAbsPidl - * Given an absolute (desktop-relative) LPITEMIDLIST, get the - * specified UI object. - *****************************************************************************/ HRESULT GetUIObjectOfAbsPidls(HWND hwnd, LPITEMIDLIST *pidls, INT NumOfpidls, REFIID riid, LPVOID *ppvOut) { LPITEMIDLIST *pidlLasts; @@ -163,28 +149,18 @@ HRESULT GetUIObjectOfAbsPidls(HWND hwnd, LPITEMIDLIST *pidls, INT NumOfpidls, RE HRESULT hres; INT i; - /* Just for safety's sake. */ *ppvOut = NULL; - pidlLasts = malloc(sizeof(LPITEMIDLIST) * NumOfpidls); if (pidlLasts == NULL) return E_FAIL; for (i = 0; i < NumOfpidls; i++) { - hres = SHBindToParent(pidls[i], &IID_IShellFolder, (LPVOID *)&psf, (LPCITEMIDLIST*)&pidlLasts[i]); + hres = SHBindToParent(pidls[i], &IID_IShellFolder, (LPVOID *)&psf, (LPCITEMIDLIST *)&pidlLasts[i]); if (FAILED(hres)) goto Fail; if (i < NumOfpidls - 1) psf->lpVtbl->Release(psf); } - - /* Now ask the parent for the the UI object of the child. */ hres = psf->lpVtbl->GetUIObjectOf(psf, hwnd, NumOfpidls, pidlLasts, riid, NULL, ppvOut); - - /* - * Regardless of whether or not the GetUIObjectOf succeeded, - * we have no further use for the parent folder. - */ Fail: psf->lpVtbl->Release(psf); - return hres; } @@ -194,32 +170,23 @@ HRESULT GetUIObjectOfPaths(HWND hwnd, LPCTSTR *pszPaths, INT NumOfPaths, REFIID HRESULT hres; INT i; - /* Just for safety's sake. */ *ppvOut = NULL; - - pidls = calloc(NumOfPaths, sizeof(LPITEMIDLIST)); + pidls = malloc(sizeof(LPITEMIDLIST) * NumOfPaths); if (pidls == NULL) return E_FAIL; for (i = 0; i < NumOfPaths; i++) { pidls[i] = PidlFromPath(hwnd, pszPaths[i]); if (pidls[i] == NULL) goto Fail; } - hres = GetUIObjectOfAbsPidls(hwnd, pidls, NumOfPaths, riid, ppvOut); - Fail: for (i = 0; i < NumOfPaths; i++) { CoTaskMemFree(pidls[i]); } free(pidls); - return hres; } -/***************************************************************************** - * DoDrop - * Drop a data object on a drop target. - *****************************************************************************/ void DoDrop(LPDATAOBJECT pdto, LPDROPTARGET pdt) { POINTL pt = { 0, 0 }; @@ -230,8 +197,8 @@ void DoDrop(LPDATAOBJECT pdto, LPDROPTARGET pdt) hres = pdt->lpVtbl->DragEnter(pdt, pdto, MK_LBUTTON, pt, &dwEffect); if (SUCCEEDED(hres) && dwEffect) { hres = pdt->lpVtbl->Drop(pdt, pdto, MK_LBUTTON, pt, &dwEffect); - - } else { + } + else { hres = pdt->lpVtbl->DragLeave(pdt); } } @@ -240,7 +207,7 @@ LPTSTR pidl_to_name(LPSHELLFOLDER psf, LPITEMIDLIST pidl, SHGDNF uFlags) { HRESULT hres; STRRET str; LPTSTR pszName = NULL; - // + hres = psf->lpVtbl->GetDisplayNameOf(psf, pidl, uFlags, &str); if (hres == S_OK) { hres = StrRetToStr(&str, pidl, &pszName); @@ -251,16 +218,15 @@ LPTSTR pidl_to_name(LPSHELLFOLDER psf, LPITEMIDLIST pidl, SHGDNF uFlags) { BOOL GethBitMapByPath(LPTSTR pszPath, HBITMAP *phbmp) { SHFILEINFO ShFI = {0}; BOOL hasBmpImage = FALSE; - // - if (SUCCEEDED( SHGetFileInfo(pszPath, FILE_ATTRIBUTE_NORMAL, &ShFI, sizeof(SHFILEINFO), SHGFI_ICON|SHGFI_SMALLICON|SHGFI_USEFILEATTRIBUTES) )) { - GpBitmap* bitmap = NULL; + + if (SUCCEEDED(SHGetFileInfo(pszPath, FILE_ATTRIBUTE_NORMAL, &ShFI, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES))) { + GpBitmap *bitmap = NULL; if (GdipCreateBitmapFromHICON(ShFI.hIcon, &bitmap) == Ok) { hasBmpImage = !(GdipCreateHBITMAPFromBitmap(bitmap, phbmp, 0)); GdipFree(bitmap); } DestroyIcon(ShFI.hIcon); } - // return hasBmpImage; } @@ -272,16 +238,14 @@ void FolderToMenu(HWND hwnd, HMENU hmenu, LPCTSTR pszFolder) /* OS_WOW6432 */ if ((PROC)(GetProcAddress(GetModuleHandle(_T("Shlwapi")), (LPCSTR)437))(30)) { - AppendMenu(hmenu, MF_GRAYED | MF_DISABLED | MF_STRING, idm_g, TEXT("64-bit OS needs 64-bit version :p")); + AppendMenu(hmenu, MF_GRAYED | MF_DISABLED | MF_STRING, g_idm, TEXT("64-bit OS needs 64-bit version :p")); return; } psf = GetFolder(hwnd, pszFolder); if (psf) { LPENUMIDLIST peidl; - hres = psf->lpVtbl->EnumObjects(psf, hwnd, - SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, - &peidl); + hres = psf->lpVtbl->EnumObjects(psf, hwnd, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &peidl); if (SUCCEEDED(hres)) { LPITEMIDLIST pidl; MENUITEMINFO mii; @@ -291,43 +255,41 @@ void FolderToMenu(HWND hwnd, HMENU hmenu, LPCTSTR pszFolder) LPTSTR pszPath, pszName; // pszPath = pidl_to_name(psf, pidl, SHGDN_FORPARSING); - if (pszPath == NULL) {continue;} pszName = pidl_to_name(psf, pidl, SHGDN_NORMAL); - if (pszName == NULL) {continue;} // - // path should be enough + // Path should be enough. CoTaskMemFree(pidl); + if (pszPath == NULL || pszName == NULL) {continue;} // - // store path rather than retrial, as we check if it is dir. - PSENDTO = (TCHAR**)realloc(PSENDTO, sizeof(TCHAR*) * (idm_g - IDM_SENDTOFIRST + 1)); - if (PSENDTO == NULL) {continue;} - PSENDTO[idm_g] = _tcsdup(pszPath); + // Store the path to retrieve, as we check if it is a dir and launch it later. + g_PSENDTO = (TCHAR **)realloc(g_PSENDTO, sizeof(TCHAR *) * (g_idm - IDM_SENDTOFIRST + 1)); + if (g_PSENDTO == NULL) {continue;} + g_PSENDTO[g_idm] = _tcsdup(pszPath); // - hBmpImageA = (HBITMAP*)realloc(hBmpImageA, sizeof(HBITMAP*) * (idm_g - IDM_SENDTOFIRST + 1)); - hasBmpImage = GethBitMapByPath(pszPath, &hBmpImageA[idm_g]); + // Icon + g_hBmpImageA = (HBITMAP *)realloc(g_hBmpImageA, sizeof(HBITMAP *) * (g_idm - IDM_SENDTOFIRST + 1)); + hasBmpImage = GethBitMapByPath(pszPath, &g_hBmpImageA[g_idm]); // if (PathIsDirectory(pszPath)) { HMENU hSubMenu = CreatePopupMenu(); if (AppendMenu(hmenu, MF_ENABLED | MF_POPUP | MF_STRING, (UINT)hSubMenu, pszName)) { mi.cbSize = sizeof(mi); mi.fMask = MIM_HELPID; - mi.dwContextHelpID = idm_g; + mi.dwContextHelpID = g_idm; SetMenuInfo(hSubMenu, &mi); - idm_g++; - //FolderToMenu(hwnd, hSubMenu, pszPath); + g_idm++; } } else { - if (AppendMenu(hmenu, MF_ENABLED | MF_STRING, idm_g, pszName)) { + if (AppendMenu(hmenu, MF_ENABLED | MF_STRING, g_idm, pszName)) { mii.cbSize = sizeof(mii); mii.fMask = MIIM_DATA; - //mii.dwItemData = (ULONG_PTR)pidl; if (hasBmpImage) { mii.fMask |= MIIM_BITMAP; - mii.hbmpItem = hBmpImageA[idm_g]; + mii.hbmpItem = g_hBmpImageA[g_idm]; } - SetMenuItemInfo(hmenu, idm_g, FALSE, &mii); - idm_g++; + SetMenuItemInfo(hmenu, g_idm, FALSE, &mii); + g_idm++; } } // @@ -339,24 +301,23 @@ void FolderToMenu(HWND hwnd, HMENU hmenu, LPCTSTR pszFolder) psf->lpVtbl->Release(psf); } - if (idm_g == IDM_SENDTOFIRST) { - AppendMenu(hmenu, MF_GRAYED | MF_DISABLED | MF_STRING, idm_g, TEXT("Send what sent to me to my sendto ^_^")); + if (g_idm == IDM_SENDTOFIRST) { + AppendMenu(hmenu, MF_GRAYED | MF_DISABLED | MF_STRING, g_idm, TEXT("Send what sent to me to my sendto ^_^")); } } void SendTo_OnInitMenuPopup(HWND hwnd, HMENU hmenu, UINT item, BOOL fSystemMenu) { if (GetMenuItemCount(hmenu) > 0) {return;} - // if (hmenu == g_hmenuSendTo) { /* :p only top level */ - FolderToMenu(hwnd, hmenu, FOLDER_SENDTO); + FolderToMenu(hwnd, hmenu, g_FOLDER_SENDTO); } else { MENUINFO mi; mi.cbSize = sizeof(mi); mi.fMask = MIM_HELPID; if (GetMenuInfo(hmenu, &mi)) { - FolderToMenu(hwnd, hmenu, PSENDTO[mi.dwContextHelpID]); + FolderToMenu(hwnd, hmenu, g_PSENDTO[mi.dwContextHelpID]); } } } @@ -365,15 +326,14 @@ void SendTo_SendToItem(HWND hwnd, int idm) { HRESULT hres; - FORKING = 1; - // + g_FORKING = 1; if (__argc == 1) { - ShellExecute(NULL, NULL, PSENDTO[idm], NULL, NULL, SW_SHOWDEFAULT); + ShellExecute(NULL, NULL, g_PSENDTO[idm], NULL, NULL, SW_SHOWDEFAULT); } else { LPDATAOBJECT pdto; LPDROPTARGET pdt; - hres = GetUIObjectOfPaths(hwnd, &PSENDTO[idm], 1, &IID_IDropTarget, (LPVOID *)&pdt); + hres = GetUIObjectOfPaths(hwnd, &g_PSENDTO[idm], 1, &IID_IDropTarget, (LPVOID *)&pdt); if (SUCCEEDED(hres)) { /* First convert all filenames to a data object. */ hres = GetUIObjectOfPaths(hwnd, __targv + 1, __argc - 1, &IID_IDataObject, (LPVOID *)&pdto); @@ -386,7 +346,7 @@ void SendTo_SendToItem(HWND hwnd, int idm) pdto->lpVtbl->Release(pdto); } // Exit as done! - FORKING = 0; + g_FORKING = 0; PostQuitMessage(0); } @@ -404,21 +364,16 @@ BOOL SendTo_OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct) /* UAC focus changes!!! */ void SendTo_OnKillFocus(HWND hwnd, HWND hwndOldFocus) { - if (FORKING == 0) PostQuitMessage(0); + if (g_FORKING == 0) PostQuitMessage(0); } LRESULT CALLBACK SendTo_WndProc(HWND hwnd, UINT uiMsg, WPARAM wParam, LPARAM lParam) { switch (uiMsg) { - HANDLE_MSG(hwnd, WM_CREATE, SendTo_OnCreate); - HANDLE_MSG(hwnd, WM_INITMENUPOPUP, SendTo_OnInitMenuPopup); - HANDLE_MSG(hwnd, WM_COMMAND, SendTo_OnCommand); - HANDLE_MSG(hwnd, WM_KILLFOCUS, SendTo_OnKillFocus); - } return DefWindowProc(hwnd, uiMsg, wParam, lParam); @@ -447,8 +402,8 @@ BOOL InitApp(void) return FALSE; } - FOLDER_SENDTO = calloc(T_MAX_PATH + 1, sizeof(TCHAR)); - if (FOLDER_SENDTO == NULL) {return FALSE;} + g_FOLDER_SENDTO = calloc(T_MAX_PATH + 1, sizeof(TCHAR)); + if (g_FOLDER_SENDTO == NULL) {return FALSE;} return TRUE; } @@ -456,21 +411,19 @@ BOOL InitApp(void) void TermApp(void) { int i, n; - // + if (g_psfDesktop) { g_psfDesktop->lpVtbl->Release(g_psfDesktop); g_psfDesktop = NULL; } - n = idm_g - IDM_SENDTOFIRST; + n = g_idm - IDM_SENDTOFIRST; for (i = 0; i < n; i++) { - free(PSENDTO[i]); - DeleteObject(&hBmpImageA[i]); + free(g_PSENDTO[i]); + DeleteObject(&g_hBmpImageA[i]); } - free(PSENDTO); - // - free(FOLDER_SENDTO); - // - free((void**)hBmpImageA); + free(g_PSENDTO); + free(g_FOLDER_SENDTO); + free((void **)g_hBmpImageA); } int WINAPI _tWinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPTSTR lpCmdLine, int nCmdShow) @@ -479,18 +432,14 @@ int WINAPI _tWinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPTSTR lpCmdLine, int HWND hwnd; HRESULT hrInit; POINT pt = {0, 0}; - // ULONG_PTR gdiplusToken; - GdiplusStartupInput gdiplusStartupInput = {1, NULL, FALSE, TRUE}; //MUST! + GdiplusStartupInput gdiplusStartupInput = {1, NULL, FALSE, TRUE}; // MUST! g_hinst = hinst; if (!InitApp()) return 1; - - if (GetFullPathName(TEXT("sendto"), T_MAX_PATH, FOLDER_SENDTO, NULL) == 0) {return 2;} - // + if (GetFullPathName(TEXT("sendto"), T_MAX_PATH, g_FOLDER_SENDTO, NULL) == 0) {return 2;} GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); - // hrInit = CoInitialize(NULL); hwnd = CreateWindow( @@ -507,17 +456,15 @@ int WINAPI _tWinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPTSTR lpCmdLine, int SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_TOOLWINDOW); ShowWindow(hwnd, nCmdShow); - // run once! GetCursorPos(&pt); TrackPopupMenu(g_hmenuSendTo, TPM_LEFTALIGN, pt.x, pt.y, 0, hwnd, NULL); - // + while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } TermApp(); - // GdiplusShutdown(gdiplusToken); if (SUCCEEDED(hrInit)) {