列宁大哥,小弟是个新手,以前一直用d7,最近刚搞vc++ 。 用的是2010 。 我想hook print 的一组api 
CreateDCA,CreateDCW,StartDocA,StartDocW,StartDocPrinterW,EndDoc,StartPage,
StartPagePrinter,WritePrinter,DeleteEnhMetaFile,EndPage,EndPagePrinter,EndDocPrinter不知道为什么没有成功,望不吝赐教。下面是部分代码:
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
   DisableThreadLibraryCalls(hModule);  
DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)pStartDocA, StartDocA);
DetourAttach(&(PVOID&)pStartDocW, StartDocW);
DetourAttach(&(PVOID&)pStartDocPrinterW, StartDocPrinterW);
DetourAttach(&(PVOID&)pEndDoc, EndDoc);
DetourAttach(&(PVOID&)pStartPage, StartPage);
DetourAttach(&(PVOID&)pStartPagePrinter, StartPagePrinter);
DetourAttach(&(PVOID&)pWritePrinter, WritePrinter);
DetourAttach(&(PVOID&)pDeleteEnhMetaFile, DeleteEnhMetaFile);
DetourAttach(&(PVOID&)pEndPage, EndPage);
DetourAttach(&(PVOID&)pEndPagePrinter, EndPagePrinter);
DetourAttach(&(PVOID&)pEndDocPrinter, EndDocPrinter);
DetourTransactionCommit();
break; case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
     
DetourTransactionBegin(); 
        DetourUpdateThread(GetCurrentThread()); 
DetourDetach(&(PVOID&)pStartDocA, MyStartDocA); 
DetourDetach(&(PVOID&)pStartDocW, MyStartDocW); 
DetourDetach(&(PVOID&)pStartDocPrinterW, MyStartDocPrinterW); 
DetourDetach(&(PVOID&)pEndDoc, MyEndDoc); 
DetourDetach(&(PVOID&)pStartPage, MyStartPage); 
DetourDetach(&(PVOID&)pStartPagePrinter, MyStartPagePrinter); 
DetourDetach(&(PVOID&)pWritePrinter, MyWritePrinter); 
DetourDetach(&(PVOID&)pDeleteEnhMetaFile, MyDeleteEnhMetaFile); 
DetourDetach(&(PVOID&)pEndPage, MyEndPage); 
DetourDetach(&(PVOID&)pEndPagePrinter, MyEndPagePrinter); 
DetourDetach(&(PVOID&)pEndDocPrinter, MyEndDocPrinter); 
        DetourTransactionCommit();           break; 
}
LRESULT __declspec(dllexport)  CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
 return CallNextHookEx(m_ghHook, nCode, wParam, lParam);
}
BOOL WINAPI StartHook(void)
{
m_ghHook = SetWindowsHookEx(WH_CBT, CBTProc, m_ghInstance, NULL);
if(NULL != m_ghHook)
{
return TRUE;
}
else
return FALSE;
}BOOL WINAPI StopHook(void)
{
return UnhookWindowsHookEx(m_ghHook);
}//
int WINAPI MyStartDocA(HDC hdc, CONST DOCINFOA* lpdi){  int nJobID = pStartDocA(hdc, (DOCINFOA*)lpdi);
 
return nJobID;
}

解决方案 »

  1.   

    之前我用的是detour 2.1 现在我该成了3.0 还是不行。DetourAttach,DetourDetach 这组不能用来hook print api.
      

  2.   

    先看目标进程是否加载你的dll
      

  3.   

    好的,我查了下,在win7 下面必须用3.0 。 然后,我要收消息,必须使用注入的方式。 那么我这样写
    DetourAttach,DetourDetach, 好像不是注入,是用的改引入表方式。 是这样的吗?
      

  4.   

    列宁老大哥好像是没有加载dll. 那怎样要才能注入进其他进程呢?
    在StartHook启动后,不会强制影射到其他进程中去吗?
      

  5.   

    用WH_CALLWNDPROC这个影响范围更大
      

  6.   

    我用tasklist /M  命令可以看到。Hook.dll 已经注入进去,那么为什么我在打印的时候没有调用重写的函数呢?
      

  7.   

    打印 api有很多,你用调试器在api放断点看那个程序是否调用了这些api
      

  8.   

    老大哥,能搞得这些api,我都重写了。 除了CreateDCA,CreateDCW.
     StartDocA StartDocW StartDocPrinterW  EndDoc StartPage  StartPagePrinter  WritePrinter DeleteEnhMetaFile  EndPage  EndPagePrinter EndDocPrinter 当我把dll注入到比如notepad++中,我启动打印。应该vs2010 能调试到我的函数。 是不是我本身的dll,没有消息循环。要用notepad++ 的消息循环, 然后我用钩子,勾住它。
    这样写钩子对吗?LRESULT  CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
     return CallNextHookEx(m_ghHook, nCode, wParam, lParam);
    }BOOL WINAPI StartHook(void)
    {
    m_ghHook = SetWindowsHookEx(WH_CALLWNDPROC, CBTProc, m_ghInstance, NULL);
    if(NULL != m_ghHook)
    {
    return TRUE;
    }
    else
    return FALSE;
    }
      

  9.   

    你先用procexp查看它是否装载了dll
    然后去msdn看有哪些打印api没有处理