- 我们要实现限制程序不允许发送文件,可以通过限制“文件选择对话框”实现。对于文件选择对话框的限制,基础点的就是Hook:GetOpenFileName、SHBrowseForFolder,对这两个API的Hook,可以限制市面上80%的文件发送,但对于QQ之类的在XP系统以上就不能成功限制了。具体是因为QQ在选择文件发送的对话框,采用COM接口来实现,所以要限制QQ发送文件,还需要额外Hook COM接口。COM Hook放在下一篇进行概述,这一篇简单的实现下GetOpenFileName、SHBrowseForFolder的Hook。
InitHook("comdlg32.dll", "GetOpenFileNameA", (DWORD)hook_OpenFileNameA);
InitHook("comdlg32.dll", "GetOpenFileNameW", (DWORD)hook_OpenFileNameW);
BOOL _stdcall hook_OpenFileNameA(LPOPENFILENAME lpofn)
{
BOOL bRet;
PAPIHOOK32_ENTRY phk= g_HookEntry[3];
RESTORE_OLDADDRESS( phk);
bRet = ::GetOpenFileNameA( lpofn );
SET_NEWADDRESS( phk);
if (CheckEnabled(g_iAppIndex))
{
if (( NULL == lpofn->lpstrFilter)
|| ( NULL != strstr(lpofn->lpstrFilter, "所有文件" ))
|| ( NULL != strstr(lpofn->lpstrFilter, "*.*"))
|| ( NULL != strstr(lpofn->lpstrFilter, "All Files")))
{
return FALSE;
}
}
return bRet;
}
BOOL _stdcall hook_OpenFileNameW(LPOPENFILENAMEW lpofn)
{
BOOL bRet;
PAPIHOOK32_ENTRY phk= g_HookEntry[4];
RESTORE_OLDADDRESS(phk);
bRet = ::GetOpenFileNameW(lpofn);
SET_NEWADDRESS(phk);
CErrModule::ShowMsgW(lpofn->lpstrFilter);
if (CheckEnabled(g_iAppIndex))
{
if (( NULL == lpofn->lpstrFilter)
|| ( NULL != wcsstr(lpofn->lpstrFilter, L"所有文件"))
|| ( NULL != wcsstr(lpofn->lpstrFilter, L"*.*"))
|| ( NULL != wcsstr(lpofn->lpstrFilter, L"All Files")))
{
return FALSE;
}
}
return bRet;
}
InitHook("shell32.dll", "SHBrowseForFolderW", (DWORD)hook_SHBrowseForFolderW);
LPITEMIDLIST WINAPI hook_SHBrowseForFolderW(LPBROWSEINFOW lpbi)
{
LPITEMIDLIST pList;
PAPIHOOK32_ENTRY phk = g_HookEntry[9];
RESTORE_OLDADDRESS(phk);
pList = SHBrowseForFolderW(lpbi);
SET_NEWADDRESS(phk);
if (CheckEnabled(g_iAppIndex))
return NULL;
return pList;
}