static int ShellExecAsUser_HandlerProc(IShellDispatch2 *const dispatch, const void *const data) { int iSuccess = SHELL_EXEC_AS_USER_FAILED; const shell_exec_as_user_param_t *const param = (const shell_exec_as_user_param_t*) data; DispatchPendingMessages(125); variant_t vEmpty; variant_t verb(param->pcOperation); variant_t file(param->pcFileName); variant_t para(param->pcParameters); variant_t show(SW_SHOWNORMAL); HRESULT hr = dispatch->ShellExecute(file, para, vEmpty, verb, show); if(SUCCEEDED(hr)) { iSuccess = SHELL_EXEC_AS_USER_SUCCESS; } return iSuccess; }
static unsigned __stdcall ShellExecAsUser_ThreadHelperProc(void* pArguments) { HRESULT hr = CoInitialize(NULL); if((hr == S_OK) || (hr == S_FALSE)) { if(threadParam_t *params = (threadParam_t*) pArguments) { params->returnValue = ShellExecAsUser(params->pcOperation, params->pcFileName, params->pcParameters, params->parentHwnd, false); } DispatchPendingMessages(1000); //Required to avoid potential deadlock or crash on CoUninitialize() !!! CoUninitialize(); } else { if(threadParam_t *params = (threadParam_t*) pArguments) { params->returnValue = SHELLEXECASUSER_ERROR_FAILED; } } return EXIT_SUCCESS; }
static unsigned __stdcall MyInvokeShellVerb_ThreadHelperProc(void* pArguments) { HRESULT hr = CoInitialize(NULL); if((hr == S_OK) || (hr == S_FALSE)) { if(threadParam_t *params = (threadParam_t*) pArguments) { params->returnValue = MyInvokeShellVerb(params->pcDirectoryName, params->pcFileName, params->uiVerbId, false); } DispatchPendingMessages(1000); //Required to avoid potential deadlock or crash on CoUninitialize() !!! CoUninitialize(); } else { if(threadParam_t *params = (threadParam_t*) pArguments) { params->returnValue = -3; } } return EXIT_SUCCESS; }
static unsigned __stdcall ShellTest_ThreadHelperProc(void* pArguments) { HRESULT hr = CoInitialize(NULL); if((hr == S_OK) || (hr == S_FALSE)) { if(threadParam_t *params = (threadParam_t*) pArguments) { params->returnValue = MyShellDispatch(params->handler, params->data, false); } DispatchPendingMessages(1000); //Required to avoid potential deadlock or crash on CoUninitialize() !!! CoUninitialize(); } else { if(threadParam_t *params = (threadParam_t*) pArguments) { params->returnValue = SHELL_DISPATCH_FAILED; } } return EXIT_SUCCESS; }
static int ShellExecAsUser_ShellDispatchProc(const TCHAR *pcOperation, const TCHAR *pcFileName, const TCHAR *pcParameters, const HWND parentHwnd) { int iSuccess = SHELLEXECASUSER_ERROR_FAILED; IShellWindows *psw = NULL; HRESULT hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&psw)); if(SUCCEEDED(hr)) { HWND hwnd = 0; IDispatch* pdisp = NULL; variant_t vEmpty; if(S_OK == psw->FindWindowSW(vEmpty, vEmpty, SWC_DESKTOP, (long*)&hwnd, SWFO_NEEDDISPATCH, &pdisp)) { if((hwnd != NULL) && (hwnd != INVALID_HANDLE_VALUE)) { IShellBrowser *psb; hr = IUnknown_QueryService(pdisp, SID_STopLevelBrowser, IID_PPV_ARGS(&psb)); if(SUCCEEDED(hr)) { IShellView *psv = NULL; hr = psb->QueryActiveShellView(&psv); if(SUCCEEDED(hr)) { IDispatch *pdispBackground = NULL; HRESULT hr = psv->GetItemObject(SVGIO_BACKGROUND, IID_PPV_ARGS(&pdispBackground)); if (SUCCEEDED(hr)) { IShellFolderViewDual *psfvd = NULL; hr = pdispBackground->QueryInterface(IID_PPV_ARGS(&psfvd)); if (SUCCEEDED(hr)) { IDispatch *pdisp = NULL; hr = psfvd->get_Application(&pdisp); if (SUCCEEDED(hr)) { IShellDispatch2 *psd; hr = pdisp->QueryInterface(IID_PPV_ARGS(&psd)); if(SUCCEEDED(hr)) { DispatchPendingMessages(125); variant_t verb(pcOperation); variant_t file(pcFileName); variant_t para(pcParameters); variant_t show(SW_SHOWNORMAL); hr = psd->ShellExecute(file, para, vEmpty, verb, show); if(SUCCEEDED(hr)) { iSuccess = SHELLEXECASUSER_ERROR_SUCCESS; } psd->Release(); psd = NULL; } pdisp->Release(); pdisp = NULL; } } pdispBackground->Release(); pdispBackground = NULL; } psv->Release(); psv = NULL; } psb->Release(); psb = NULL; } } pdisp->Release(); pdisp = NULL; } psw->Release(); psw = NULL; } return iSuccess; }
static int MyInvokeShellVerb_ShellDispatchProc(const TCHAR *pcDirectoryName, const TCHAR *pcFileName, const DWORD uiVerbId) { int iSuccess = 0; bool bUnloadDll = false; HMODULE hShellDll = GetModuleHandleW(shell32); if(hShellDll == NULL) { bUnloadDll = true; hShellDll = LoadLibraryW(shell32); if(hShellDll == NULL) { iSuccess = -3; return iSuccess; } } WCHAR pcVerbName[128]; memset(pcVerbName, 0, sizeof(WCHAR) * 128); if(LoadStringW(hShellDll, uiVerbId, pcVerbName, 128) < 1) { if(bUnloadDll) { FreeLibrary(hShellDll); hShellDll = NULL; } iSuccess = -3; return iSuccess; } if(bUnloadDll) { FreeLibrary(hShellDll); hShellDll = NULL; } // ----------------------------------- // IShellDispatch *pShellDispatch = NULL; Folder *pFolder = NULL; FolderItem *pItem = NULL; HRESULT hr = CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void**)&pShellDispatch); if(FAILED(hr) || (pShellDispatch == NULL)) { iSuccess = -3; return iSuccess; } variant_t vaDirectory(pcDirectoryName); hr = pShellDispatch->NameSpace(vaDirectory, &pFolder); if(FAILED(hr) || (pFolder == NULL)) { iSuccess = -3; pShellDispatch->Release(); return iSuccess; } pShellDispatch->Release(); pShellDispatch = NULL; variant_t vaFileName(pcFileName); hr = pFolder->ParseName(vaFileName, &pItem); if(FAILED(hr) || (pItem == NULL)) { iSuccess = -3; pFolder->Release(); return iSuccess; } pFolder->Release(); pFolder = NULL; // ----------------------------------- // long iVerbCount = 0; FolderItemVerbs *pVerbs = NULL; hr = pItem->Verbs(&pVerbs); if(FAILED(hr) || (pVerbs == NULL)) { iSuccess = -3; pItem->Release(); return iSuccess; } pItem->Release(); pItem = NULL; hr = pVerbs->get_Count(&iVerbCount); if(FAILED(hr) || (iVerbCount < 1)) { iSuccess = -3; pVerbs->Release(); return iSuccess; } DispatchPendingMessages(125); // ----------------------------------- // for(int i = 0; i < iVerbCount; i++) { variant_t vaVariantIndex(i); FolderItemVerb *pCurrentVerb = NULL; BSTR pcCurrentVerbName = NULL; hr = pVerbs->Item(vaVariantIndex, &pCurrentVerb); if (FAILED(hr) || (pCurrentVerb == NULL)) { continue; } hr = pCurrentVerb->get_Name(&pcCurrentVerbName); if(FAILED(hr) || (pcCurrentVerbName == NULL)) { pCurrentVerb->Release(); continue; } if(_wcsicmp(pcCurrentVerbName, pcVerbName) == 0) { hr = pCurrentVerb->DoIt(); if(!FAILED(hr)) { iSuccess = 1; } } SysFreeString(pcCurrentVerbName); pCurrentVerb->Release(); } pVerbs->Release(); pVerbs = NULL; // ----------------------------------- // return iSuccess; }