rt

解决方案 »

  1.   

    HOOK到CreateFile()的串口句柄难道不行吗?如果是已经打开的串口句柄就不太好获得了,我是这么觉得,不知道有没有别的方法
      

  2.   

    已经开了话:(没法了不过,不用HOOK CreateFile,hook 对DCB操作的函数^_^
      

  3.   

    我的目的,就是portmon。
    第一:我没搞清楚:为什么我的串口程序先起动后,portmon还是可以跟踪串口?我发现portmon里没有.sys,.vxd,所以我觉得它就是用的HOOK API。
    第二:HOOK CreateFile()的难度太大了,我不知道你想到没有:),在别人的应用程序里,CreateFile()是有返回值的,我一旦hook 了它,那这个返回值就埴不回去了,要埴也要一个一个填(不现实)
    第三:我用portmon跟踪了大部分串口软件,我发现在CreateFile,一定是SetCommState()之类的函数,就是对DCB的操作。还有SetTimeOut().我想问问你,这里可不可以找到一个唯一和串口对应的函数,SetCommState()会有别的东西调用吗?(除了COMn).
    第4:我拿到了一个hFile句柄后,怎么判断它是对应那个串口?我用SetWindowsHookEx()注射了DLL后,如果,那个串口软件是一个MDI的,也就是一个窗口对一个串口。天,那我存下来的这个hFile不就变来变去?晕,我没想出来,怎么搞。
    第5:我认为HOOK API可以动态、不占用串口的监听串口,但不可能动态调试。:)谢谢,你对本问题的关心,我一个星期来,基本上没怎么睡觉,头越来越晕。
      

  4.   

    第二:HOOK CreateFile()的难度太大了,我不知道你想到没有:),在别人的应用程序里,CreateFile()是有返回值的,我一旦hook 了它,那这个返回值就埴不回去了,要埴也要一个一个填(不现实)-------有返回值的情况,我现在觉得是不可能的。
      

  5.   

    有QQ吗?把你QQ留下,我加你,在讨论,参数返回问题其实很好解决,看我以前的APIHOOK怎么处理的
     HWND WINAPI MYCreateWindowW(
      DWORD dwExStyle,      // extended window style
      LPCTSTR lpClassName,  // registered class name
      LPCTSTR lpWindowName, // window name
      DWORD dwStyle,        // window style
      int x,                // horizontal position of window
      int y,                // vertical position of window
      int nWidth,           // window width
      int nHeight,          // window height
      HWND hWndParent,      // handle to parent or owner window
      HMENU hMenu,          // menu handle or child identifier
      HINSTANCE hInstance,  // handle to application instance
      LPVOID lpParam        // window-creation data)
     {
     HWND wnd=CreateWindowExW(
       dwExStyle,      // extended window style
       lpClassName,  // registered class name
       lpWindowName, // window name
       dwStyle,        // window style
       x,                // horizontal position of window
       y,                // vertical position of window
       nWidth,           // window width
       nHeight,          // window height
       hWndParent,      // handle to parent or owner window
       hMenu,          // menu handle or child identifier
       hInstance,  // handle to application instance
       lpParam        // window-creation data
                              );
    HWND WndParent=wnd;
    /* if(hWndParent==NULL)
    {
    WndParent=wnd;
    }
    else
    {
    WndParent=hWndParent;
    }  */ SetWindowLong(WndParent,GWL_EXSTYLE,
    GetWindowLong(WndParent,GWL_EXSTYLE)^0x80000);
    HINSTANCE hInst = LoadLibrary("User32.DLL"); 
    if(hInst) 
    {            
    typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);          
    MYFUNC fun = NULL;
    //取得SetLayeredWindowAttributes函数指针     
    fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
    if(fun)fun(WndParent,0,200,2);
    //SetLayeredWindowAttributes(this->GetSafeHwnd(),0,eff,2);
    FreeLibrary(hInst); 
    //MessageBox(NULL,"CreateWindowW=NULL","CreateWindowW=NULL",MB_OK);
    }
    //MessageBox(NULL,"CreateWindowW=NULL","CreateWindowW=NULL",MB_OK);
     return wnd; }
      

  6.   

    还有,刚才看了一下portmon,我敢肯定它连APIHOOK都没有用,因为APIHOOK肯定需要以DLL的形式实现,它的却没有
      

  7.   

    :)可以hook CreateFile(),我原来以为这个句柄在DLL模块里会调用错误:)呵呵
      

  8.   

    你看这个:
    HANDLE WINAPI Hook_CreateFileW(
      LPCTSTR lpFileName,          // pointer to name of the file
      DWORD dwDesiredAccess,       // access (read-write) mode
      DWORD dwShareMode,           // share mode
      LPSECURITY_ATTRIBUTES lpSecurityAttributes,
                                   // pointer to security attributes
      DWORD dwCreationDisposition,  // how to create
      DWORD dwFlagsAndAttributes,  // file attributes
      HANDLE hTemplateFile         // handle to file with attributes to 
    )从  LPCTSTR lpFileName,可以判断出是不是含有COMn。
    我写了一个函数:
    int Str_Contain(char *input)
    {
    int len = 0;
    int i_i = 0;
    int j = 0;
    char* com = "COM";
    len = strlen(input);
    for(int i = 0;i<len;i++){
    i_i = i;
    for(j = 0;j<3;j++){
    if(input[i_i] == com[j]){
    i_i++;
    //j++;
    }
    else
    break;
    }
    if(j == 3)
    break;
    }
    return (j == 3);
    };然后这样用:
    HANDLE WINAPI Hook_CreateFileA(
      LPCTSTR lpFileName,          // pointer to name of the file
      DWORD dwDesiredAccess,       // access (read-write) mode
      DWORD dwShareMode,           // share mode
      LPSECURITY_ATTRIBUTES lpSecurityAttributes,
                                   // pointer to security attributes
      DWORD dwCreationDisposition,  // how to create
      DWORD dwFlagsAndAttributes,  // file attributes
      HANDLE hTemplateFile         // handle to file with attributes to 
    )
    {
    HANDLE hFile = ((PFCreateFileA)(PROC) g_CreateFileA)(
      lpFileName , dwDesiredAccess,
      dwShareMode, lpSecurityAttributes,
              dwCreationDisposition, dwFlagsAndAttributes, 
      hTemplateFile);
          if(Str_Contain((char*)hFile)){///////////////////////////////////出错
    MessageBox(NULL,lpFileName,"com",MB_OK);
    com = hFile;
    }
    return hFile;
    }我试了几次,发现(char*)hFile,也就是LPCTSTR无法用(char*)转换过来(这两个不是一回事吗???)
    不能用CString,我用的是Windows核心编程里那个现成的类,我一#include <afx.h>
    就会报错,不可以和windows.h在一起。你觉得我该怎么搞?这个COMn提不出来,就没有用了。
      

  9.   

    那个程序你看了吧?
    我做了个sdi,响映左击mouse
    void CDialogexeView::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call 
    LastMsgBoxInfo_HookAllApps(TRUE,0,handle);
    CView::OnLButtonDown(nFlags, point);
    }
    LastMsgBoxInfo_HookAllApps本来只有两个参数,我加了第三个,就是那个handle,准备传个句柄之为类的东西过去。在CreateFile成功的时候,在我的sdi里画个圆什么的测试一下。
    :(我没搞成