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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| /***************************************************************************
* Copyright (C) 2001-2008 by Józef Starosczyk *
* ixen@copyhandler.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License *
* (version 2) as published by the Free Software Foundation; *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/***************************************************************************
register.cpp
-------------------
Begin : Mon Nov 10 2008 13:20 alpha_one_x86
Project : Ultracopier
Email : alpha.super-one@laposte.net
Note : See README for copyright and developer
Target : For interact with windows and the dll
****************************************************************************/
#include "register.h"
#include "objbase.h"
#include "env.h"
#include <QString.h>
HRESULT RegisterShellExtDll(QString lpszPath, bool bRegister)
{
DEBUGCONSOLE(70,"RegisterShellExtDll","start");
// 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))
{
DEBUGCONSOLE(70,"RegisterShellExtDll","CoInitializeEx: ok");
HRESULT (STDAPICALLTYPE *pfn)(void);
WCHAR temp[lpszPath.length()+1];
lpszPath.toWCharArray(temp);
HINSTANCE hMod = LoadLibrary((WCHAR*)temp);// load the dll
if(hMod == NULL)
{
hResult = HRESULT_FROM_WIN32(GetLastError());
DEBUGCONSOLE(70,"RegisterShellExtDll","hMod: NULL");
}
if(SUCCEEDED(hResult) && !hMod)
{
DEBUGCONSOLE(70,"RegisterShellExtDll","SUCCEEDED(hResult): true && hMod: false");
hResult = E_FAIL;
}
if(SUCCEEDED(hResult))
{
DEBUGCONSOLE(70,"RegisterShellExtDll","SUCCEEDED(hResult): true");
(FARPROC&)pfn = GetProcAddress(hMod, (bRegister ? "DllRegisterServer" : "DllUnregisterServer"));
if(pfn == NULL)
{
DEBUGCONSOLE(70,"RegisterShellExtDll","pfn: NULL");
hResult = E_FAIL;
}
if(SUCCEEDED(hResult))
{
DEBUGCONSOLE(70,"RegisterShellExtDll","SUCCEEDED(hResult): true");
hResult = (*pfn)();
}
CoFreeLibrary(hMod);
}
CoUninitialize();
}
else
{
DEBUGCONSOLE(70,"RegisterShellExtDll","false");
}
// if previous operation failed (ie. vista system) - try running regsvr32 with elevated privileges
if(SCODE_CODE(hResult) == ERROR_ACCESS_DENIED)
{
DEBUGCONSOLE(70,"RegisterShellExtDll","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+"\"";
WCHAR temp[strParams.length()*2+1];
strParams.toWCharArray((WCHAR*)temp);
sei.lpParameters = temp;
strParams.fromStdWString(temp);
DEBUGCONSOLE(70,"RegisterShellExtDll","strParams: "+strParams);
sei.nShow = SW_SHOW;
if(!ShellExecuteEx(&sei))
{
DEBUGCONSOLE(70,"RegisterShellExtDll","ShellExecuteEx(&sei): false");
hResult = E_FAIL;
}
}
else
{
DEBUGCONSOLE(70,"RegisterShellExtDll","SCODE_CODE(hResult): "+QString::number(SCODE_CODE(hResult)));
}
return hResult;
} |
Partager