- 注册时间
- 2011-3-6
- 最后登录
- 1970-1-1
该用户从未签到
|
网上代码大多数都是ATL的,我比较喜欢SDK,因此用SDK实现了个,大牛飘过,希望对新手有帮助
反正代码不多,就直接贴出来算了,有不对的大家请指出
/********************CopyHook.def********************/
代码:
LIBRARY "CopyHook.dll"
EXPORTS
DllGetClassObject PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE
DllCanUnloadNow PRIVATE
/********************CopyHook.h********************/
代码:
//{24c7f2bb-3f48-42eb-9d6f-81b756ea15ba}
DEFINE_GUID(CLSID_MyCopyHook, 0x24c7f2bb, 0x3f48, 0x42eb, 0x9d, 0x6f, 0x81, 0xb7, 0x56, 0xea, 0x15, 0xba);
class CMyCopyHook : public ICopyHookW, public ICopyHookA
{
public:
CMyCopyHook();
~CMyCopyHook();
HRESULT STDMETHODCALLTYPE QueryInterface (REFIID iid, void **ppvObject);
ULONG STDMETHODCALLTYPE AddRef ();
ULONG STDMETHODCALLTYPE Release ();
UINT STDMETHODCALLTYPE CopyCallback (HWND hwnd, UINT wFunc, UINT wFlags, LPCWSTR pszSrcFile, DWORD dwSrcAttribs,
LPCWSTR pszDestFile, DWORD dwDestAttribs);
UINT STDMETHODCALLTYPE CopyCallback (HWND hwnd, UINT wFunc, UINT wFlags, LPCSTR pszSrcFile, DWORD dwSrcAttribs,
LPCSTR pszDestFile, DWORD dwDestAttribs);
private:
ULONG m_lRefCount;
};
/********************CopyHook.cpp********************/
代码:
#include <Windows.h>
#include <Stdio.h>
#include <Tchar.h>
#include <Shlobj.h>
#include <InitGuid.h>
#include "CopyHook.h"
#include "CopyHookFactory.h"
HMODULE g_hinst;
ULONG g_lLockNumber;
ULONG g_lRefThisDll;
CMyCopyHook::CMyCopyHook() : m_lRefCount(0)
{
g_lRefThisDll++;
}
CMyCopyHook::~CMyCopyHook()
{
g_lRefThisDll--;
}
HRESULT STDMETHODCALLTYPE CMyCopyHook::QueryInterface (REFIID iid, void **ppvObject)
{
if(iid == IID_IUnknown)
{
m_lRefCount++;
*ppvObject = this;
}
else if(iid == IID_ICopyHookW)
{
m_lRefCount++;
*ppvObject = (ICopyHookW *)this;
}
else if(iid == IID_ICopyHookA)
{
m_lRefCount++;
*ppvObject = (ICopyHookA *)this;
}
else
{
*ppvObject = NULL;
return E_NOINTERFACE;
}
return S_OK;
}
ULONG STDMETHODCALLTYPE CMyCopyHook::AddRef ()
{
m_lRefCount++;
return m_lRefCount;
}
ULONG STDMETHODCALLTYPE CMyCopyHook::Release ()
{
m_lRefCount--;
if(m_lRefCount == 0)
delete this;
return m_lRefCount;
}
UINT STDMETHODCALLTYPE CMyCopyHook::CopyCallback (HWND hwnd, UINT wFunc, UINT wFlags, LPCSTR pszSrcFile, DWORD dwSrcAttribs,
LPCSTR pszDestFile, DWORD dwDestAttribs)
{
CHAR szMsg[1024];
wsprintfA(szMsg, "是否允许将目录从 %s 复制到 %s ?", pszSrcFile, pszDestFile);
return MessageBoxA(hwnd, szMsg, "提示", MB_YESNO | MB_ICONQUESTION);
}
UINT STDMETHODCALLTYPE CMyCopyHook::CopyCallback (HWND hwnd, UINT wFunc, UINT wFlags, LPCWSTR pszSrcFile, DWORD dwSrcAttribs,
LPCWSTR pszDestFile, DWORD dwDestAttribs)
{
WCHAR szMsg[1024];
wsprintfW(szMsg, L"是否允许将目录从 %s 复制文件到 %s ?", pszSrcFile, pszDestFile);
return MessageBoxW(hwnd, szMsg, L"提示", MB_YESNO | MB_ICONQUESTION);
}
STDAPI DllGetClassObject (const CLSID &rclsid, const IID &riid, void **ppv)
{
if ( rclsid == CLSID_MyCopyHook )
{
CMyCopyHookFactory *pFactory = new CMyCopyHookFactory;
if (pFactory == NULL)
return E_OUTOFMEMORY ;
return pFactory->QueryInterface(riid, ppv);
}
return CLASS_E_CLASSNOTAVAILABLE;
}
STDAPI DllCanUnloadNow(void)
{
if (g_lRefThisDll == 0 && g_lLockNumber == 0)
return S_OK;
else
return S_FALSE;
}
HRESULT ClsidToString(const CLSID &clsid, LPWSTR lpszClsid)
{
HRESULT result;
LPWSTR lpszStr;
if(!lpszClsid)
return E_FAIL;
if((result = StringFromCLSID(clsid, &lpszStr)) != S_OK)
return result;
lstrcpyW(lpszClsid, lpszStr);
CoTaskMemFree(lpszStr);
lpszStr = NULL;
return S_OK;
}
HRESULT RegisterClsid(LPWSTR lpszClsid, LPWSTR lpszModule)
{
LSTATUS status;
HKEY hKey;
HKEY hSubKey;
WCHAR szModel[] = L"Apartment";
if((status = RegOpenKeyW(HKEY_CLASSES_ROOT, L"CLSID", &hKey)) != ERROR_SUCCESS)
return HRESULT_FROM_WIN32(status);
if((status = RegCreateKeyW(hKey, lpszClsid, &hSubKey)) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return HRESULT_FROM_WIN32(status);;
}
RegCloseKey(hKey);
hKey = hSubKey;
hSubKey = NULL;
if((status = RegCreateKeyW(hKey, L"InProcServer32", &hSubKey)) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return HRESULT_FROM_WIN32(status);;
}
RegCloseKey(hKey);
hKey = hSubKey;
hSubKey = NULL;
if((status = RegSetValueW(hKey, NULL, REG_SZ, lpszModule, MAX_PATH)) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return HRESULT_FROM_WIN32(status);;
}
if((status = RegSetValueExW(hKey, L"ThreadingModel", 0, REG_SZ, (BYTE*)szModel, sizeof(szModel))) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return HRESULT_FROM_WIN32(status);;
}
RegCloseKey(hKey);
return S_OK;
}
HRESULT UnRegisterClsid(LPWSTR lpszClsid)
{
LSTATUS status;
WCHAR szSubKey[1024];
lstrcpyW(szSubKey, L"CLSID\\");
lstrcatW(szSubKey, lpszClsid);
lstrcatW(szSubKey, L"\\InProcServer32");
if((status = RegDeleteKeyW(HKEY_CLASSES_ROOT, szSubKey)) != ERROR_SUCCESS)
return HRESULT_FROM_WIN32(status);
lstrcpyW(szSubKey, L"CLSID\\");
lstrcatW(szSubKey, lpszClsid);
if((status = RegDeleteKeyW(HKEY_CLASSES_ROOT, szSubKey)) != ERROR_SUCCESS)
return HRESULT_FROM_WIN32(status);
return S_OK;
}
STDAPI DllRegisterServer()
{
HRESULT result;
WCHAR szClsid[64];
LSTATUS status;
HKEY hKey;
WCHAR szModule[MAX_PATH];
if((result = ClsidToString(CLSID_MyCopyHook, szClsid)) != S_OK)
return result;
if(!GetModuleFileNameW(g_hinst, szModule, MAX_PATH))
return E_FAIL;
if((status = RegCreateKeyW(HKEY_CLASSES_ROOT, L"Directory\\shellex\\CopyHookHandlers\\WertCopyHook", &hKey)) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return HRESULT_FROM_WIN32(status);
}
RegCloseKey(hKey);
if((status = RegSetValueW(HKEY_CLASSES_ROOT, L"Directory\\shellex\\CopyHookHandlers\\WertCopyHook", REG_SZ, szClsid, sizeof(szClsid) / sizeof(WCHAR))) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return HRESULT_FROM_WIN32(status);
}
RegCloseKey(hKey);
return RegisterClsid(szClsid, szModule);
}
STDAPI DllUnregisterServer()
{
HRESULT result;
WCHAR szClsid[64];
LSTATUS status;
if((result = ClsidToString(CLSID_MyCopyHook, szClsid)) != S_OK)
return result;
if((status = RegDeleteKeyW(HKEY_CLASSES_ROOT, L"Directory\\shellex\\CopyHookHandlers\\WertCopyHook")) != ERROR_SUCCESS)
return HRESULT_FROM_WIN32(status);
return UnRegisterClsid(szClsid);
}
BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH://加载dll;
{
g_hinst = hinstDLL;
}
break;
case DLL_PROCESS_DETACH://释放dll
{
}
break;
case DLL_THREAD_ATTACH://新建线程
{
}
break;
case DLL_THREAD_DETACH://线程退出
{
}
break;
}
return TRUE;
}
/********************CopyHookFactory.h********************/
代码:
class CMyCopyHookFactory : public IClassFactory
{
public:
CMyCopyHookFactory();
~CMyCopyHookFactory();
HRESULT STDMETHODCALLTYPE QueryInterface (REFIID iid, void **ppvObject);
ULONG STDMETHODCALLTYPE AddRef ();
ULONG STDMETHODCALLTYPE Release ();
HRESULT STDMETHODCALLTYPE CreateInstance (IUnknown *pUnkOuter, REFIID riid, void **ppvObject);
HRESULT STDMETHODCALLTYPE LockServer (BOOL fLock);
private:
ULONG m_lRefCount;
};
/********************CopyHookFactory.cpp********************/
代码:
#include <Windows.h>
#include <Stdio.h>
#include <Tchar.h>
#include <Shlobj.h>
#include <InitGuid.h>
#include "CopyHook.h"
#include "CopyHookFactory.h"
extern ULONG g_lLockNumber;
extern ULONG g_lRefThisDll;
CMyCopyHookFactory::CMyCopyHookFactory() : m_lRefCount(0)
{
}
CMyCopyHookFactory::~CMyCopyHookFactory()
{
}
HRESULT STDMETHODCALLTYPE CMyCopyHookFactory::QueryInterface (REFIID iid, void **ppvObject)
{
if(iid == IID_IUnknown)
{
m_lRefCount++;
*ppvObject = this;
}
else if(iid == IID_IClassFactory)
{
m_lRefCount++;
*ppvObject = this;
}
else
{
*ppvObject = NULL;
return E_NOINTERFACE;
}
return S_OK;
}
ULONG STDMETHODCALLTYPE CMyCopyHookFactory::AddRef ()
{
m_lRefCount++;
return m_lRefCount;
}
ULONG STDMETHODCALLTYPE CMyCopyHookFactory::Release ()
{
m_lRefCount--;
if(m_lRefCount == 0)
delete this;
return m_lRefCount;
}
HRESULT STDMETHODCALLTYPE CMyCopyHookFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppvObject)
{
CMyCopyHook *pObj;
*ppvObject = NULL;
if(pUnkOuter)
return CLASS_E_NOAGGREGATION;
pObj = new CMyCopyHook();
if (pObj == NULL)
return E_OUTOFMEMORY ;
return pObj->QueryInterface(IID_ICopyHookW, ppvObject);
}
HRESULT STDMETHODCALLTYPE CMyCopyHookFactory::LockServer(BOOL fLock)
{
if(fLock)
g_lLockNumber++;
else
g_lLockNumber--;
return NOERROR;
} |
|