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
| HRESULT RegisterShellExtDll(QString lpszPath, bool bRegister)
{
// first try - load dll and register it manually.
HRESULT hResult = S_OK;
// if failed - try by loading extension manually (would fail on vista when running as user)
hResult = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if(SUCCEEDED(hResult))
{
HRESULT (STDAPICALLTYPE *pfn)(void);
LPCTSTR temp[lpszPath.length()+1];
lpszPath.toWCharArray(temp);
HINSTANCE hMod = LoadLibrary(temp);// load the dll
if(hMod == NULL)
hResult = HRESULT_FROM_WIN32(GetLastError());
if(SUCCEEDED(hResult) && !hMod)
hResult = E_FAIL;
if(SUCCEEDED(hResult))
{
(FARPROC&)pfn = GetProcAddress(hMod, (bRegister ? "DllRegisterServer" : "DllUnregisterServer"));
if(pfn == NULL)
hResult = E_FAIL;
if(SUCCEEDED(hResult))
hResult = (*pfn)();
CoFreeLibrary(hMod);
}
CoUninitialize();
}
// if previous operation failed (ie. vista system) - try running regsvr32 with elevated privileges
if(SCODE_CODE(hResult) == ERROR_ACCESS_DENIED)
{
hResult = S_FALSE;
// try with regsvr32
SHELLEXECUTEINFO sei;
memset(&sei, 0, sizeof(sei));
sei.cbSize = sizeof(sei);
sei.fMask = SEE_MASK_UNICODE;
sei.lpVerb = TEXT("runas");
sei.lpFile = TEXT("regsvr32.exe");
QString strParams;
if(bRegister)
strParams = " \""+lpszPath+"\"";
else
strParams = "/u \""+lpszPath+"\"";
LPCTSTR temp[strParams.length()+1];
strParams.toWCharArray(temp);
sei.lpParameters = temp;
sei.nShow = SW_SHOW;
if(!ShellExecuteEx(&sei))
hResult = E_FAIL;
}
return hResult;
} |
Partager