HOOK.DLL中:static AFX_EXTENSION_MODULE LogHookDLL = { NULL, NULL };HWND        g_hWnd       = NULL;  //接收窗口
//HHOOK       g_hLogHook   = NULL;  //钩子变量
HINSTANCE g_hInstance  = NULL;  //模块实例句柄
HWND        g_hLastFocus = NULL;  //记录上一次得到焦点的窗口句柄#pragma   data_seg("myData")     
static    HHOOK g_hLogHook = NULL;  
#pragma   data_seg()
#pragma   comment(linker,"/Section:SHARD_DATA,rws")extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved); g_hInstance = hInstance; if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("LOGHOOK.DLL Initializing!\n");

// Extension DLL one-time initialization
if (!AfxInitExtensionModule(LogHookDLL, hInstance))
return 0; // Insert this DLL into the resource chain
// NOTE: If this Extension DLL is being implicitly linked to by
//  an MFC Regular DLL (such as an ActiveX Control)
//  instead of an MFC application, then you will want to
//  remove this line from DllMain and put it in a separate
//  function exported from this Extension DLL.  The Regular DLL
//  that uses this Extension DLL should then explicitly call that
//  function to initialize this Extension DLL.  Otherwise,
//  the CDynLinkLibrary object will not be attached to the
//  Regular DLL's resource chain, and serious problems will
//  result. new CDynLinkLibrary(LogHookDLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("LOGHOOK.DLL Terminating!\n");
// Terminate the library before destructors are called
if (g_hLogHook != NULL) 
UnhookWindowsHookEx(g_hLogHook); AfxTermExtensionModule(LogHookDLL);
}
return 1;   // ok
}LRESULT CALLBACK JournalLogProc(int iCode,WPARAM wParam, LPARAM lParam)
{
if (iCode< 0 )
return CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
if (iCode == HC_ACTION)
{
/*EVENTMSG *pEvt=(EVENTMSG *)lParam;
HWND hFocus;            //保存当前活动窗口句柄
char szTitle[256];      //当前窗口名称
                  */ if(pEvt->message==WM_LBUTTONDOWN || pEvt->message ==WM_RBUTTONDOWN)
{
                    ...
}

TRACE("%d\n",iCode);
PCWPSTRUCT pcw=(PCWPSTRUCT)lParam;   

if(iCode > 0 )   
{   
if( pcw && pcw->hwnd && pcw->message==WM_CREATE)   
{   
CString   strApplication;   
CString   strCommand=GetCommandLine();   
int nnum = strCommand.ReverseFind('\\');
if(nnum!=-1)   
{   
strApplication=strCommand.Mid(nnum+1);   
strApplication.TrimRight();   
strApplication.TrimRight("\"");   
}   
if(strApplication==_T("notepad.exe")) 
return 0;         
}   
} return CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
}void InstallHook(HWND hWnd)
{
if(g_hLogHook==NULL)
{
g_hLogHook = SetWindowsHookEx(WH_JOURNALRECORD,
(HOOKPROC)JournalLogProc,
g_hInstance,NULL);
if( g_hLogHook)
{

}
}
g_hWnd = hWnd;
}void UnInstallHook()
{
if(g_hLogHook != NULL)
{
UnhookWindowsHookEx(g_hLogHook);
g_hLogHook=NULL;
}
}我的意思是想其他程序启动时我能捕获到启动消息,对于不想启动的程序我可以禁止启动.比如我设置了禁止打开文本,文本程序就不能启动.1,我的HOOK存在问题,对于鼠标的消息能够响应,但是其他的消息都响应不了.iCode的值一直为0.问题出在哪里? 
2,禁止程序运行,有什么好的思路和实现方法.有源码更好,谢谢.[email protected]

解决方案 »

  1.   

    可以用parental control禁止程序运行
      

  2.   

    还是不建议使用MFC DLL用做全局HOOK,除了系统API外,它还会加载很多其它的DLL。
      

  3.   

    用hook的话,建议用detours,很方便
      

  4.   

    PsSetCreateProcessNotifyRoutine can add one callback for CreateProcess notify.
      

  5.   

    to jiangsheng, 
    parental control是一个第三方软件吧?我是要自己编程实现禁止程序运行。比如我的程序设置了禁止"notepad.exe","123.exe","456.exe"运行,则打开这些程序时直接返回,不运行。to zhoujianhei,
    不用HOOK?那可以用哪些的? Shell可以吗?怎么实现?to naixian1983,
    可否发一个使用detours的小程序。to laiyiling,
    PsSetCreateProcessNotifyRoutine 是驱动级的吧?不会使用,而且还需要ntddk吧?在VC6下,加上这个DDK,容易实现禁止程序运行吗?
      

  6.   

    detours的用法可参看:http://bbs.pediy.com/showthread.php?t=38759
      

  7.   

    parental control是Windows Vista的一个功能
      

  8.   

    1. 勾住CreateProcessA和CreateProcessW也许可以实现禁止程序运行的功能。
    2. Shell钩子可能不能实现楼主的要求,Shell钩子似乎只对有窗口的程序有效
      

  9.   

    http://waxb.blog.com.cn/archives/2007/APIHook2.shtml这里有几个很好的例子教你怎样一步一步用HOOK和怎样用detours。而且还有源码下载,好的很
      

  10.   

    这个问题不适合用SetWindowsHookEx。先提个简单点的方法,在注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows下面的AppInit_DLLs值中加入你的DLL路径及文件名,每个进程启动时都会加在你的DLL,你可以在DllMain中加入判断,当不想让改程序执行时可以ExitProcess。
      

  11.   

    另外,全局Hook之类的DLL,应该用Win32 DLL。
      

  12.   

    谢谢LS,我用修改注册表,加载DLL试试。
      

  13.   

    我的DLL:
    #include "stdafx.h"BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam )
    {
    CHAR szName[ 256 ];
    CHAR szClass[ 256 ];
    ::GetClassName( hwnd,szClass,255);
    ::GetWindowText( hwnd,szName, 255 );

    if(::GetWindowLong(hwnd,GWL_STYLE)& WS_VISIBLE
    && GetParent( hwnd ) == NULL
    && strcmp( szName,"")!=0 
    && strcmp( szClass,"Progman")!=0 )
    {
    if (strcmp(szClass,"Notepad") == 0)
    {
    ExitProcess(0);
    return FALSE;
    }
    }
    return TRUE;
    }BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
     )
    {
    EnumWindows(EnumWindowsProc,NULL);    return TRUE;
    }修改了注册表,加载我的DLL,现在是要禁止运行文本程序.而文本程序仍然可以正常打开,是不是应该用GetCmdLine,而不是用我现在这个EnumWindows的方法.
    还有一个问题,就是打开一个允许运行的程序,加载了我的DLL,再打开其他的程序,则没有反应,怎么让这个DLL可以同时让多个程序同时加载?另:监控一个程序的运行情况,就是记录这个程序何时启动,何时关闭,要怎么做?谢谢!
      

  14.   

    9>Logging the Shell Activity
    http://www.cnblogs.com/henryzc/articles/297063.html但是这种方法只能禁止从SHELL中启动的进程
      

  15.   

    利用注册表AppInit_DLLs值自动插入DLL是在进程刚启动时,这时进程还没有创建窗口,所以EnumWindows是找不到的(其实也不该用Enum,因为目的是取当前进程),你可以用GetModuleFileName、GetProcessImageFileName、GetCommandLine等函数来得到进程的Exe文件名。
      

  16.   

    感谢LS,我用LS的方法已实现了禁用。关于如何去建全局HOOK和HOOK API,希望大家提供个链接或是最好有源码,VC6.0,简单一些的,越简明越好。[email protected],谢谢!如果觉得分数太少,我可加分或另开贴,来者不拒,下班前结贴。