我的代码如下:
        LPDWORD yb_handle = NULL;
HWND w_handle = (HWND)FindWindow(NULL,"YB_OnlineClient");
if(w_handle != NULL){
MessageBox("成功找到窗口");
GetWindowThreadProcessId(window_handle,yb_handle);
if(yb_handle != NULL)
MessageBox("成功得到创建的进程");
}
第一个MessageBox可以弹出来,第二个就不能,为什么呢?调用GetLastError()返回的是1400,也就是"无效的窗口句柄",
FindWindow的返回值:
Return Values
If the function succeeds, the return value is a handle to the window that has the specified class name and window name.If the function fails, the return value is NULL. To get extended error information, call GetLastError. 我的代码:
if(w_handle != NULL)
 MessageBox("成功找到窗口");
说明FindWindow没问题啊,并且在这个地方调用GetLastError()返回是0啊,没问题啊.是不是跟FindWindow中的LPCTSTR lpClassName这个参数有关系啊?这个参数如何获取啊?
If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter. 
帮忙看看,多谢.

解决方案 »

  1.   

    window_handle是什么?前面FindWindow返回的是w_handle,不是window_handle。
      

  2.   


    DWORD processID;HWND hWnd = (HWND)FindWindow(NULL,"YB_OnlineClient"); 
    if(hWnd != NULL){ 
      MessageBox("成功找到窗口"); 
      DWORD threadID = GetWindowThreadProcessId(hWnd,&processID); 
      if(threadID != NULL){ 
        MessageBox("成功得到创建的进程"); 
      }
    }
      

  3.   

    window_handle是什么?前面FindWindow返回的是w_handle,不是window_handl
    =====================================================================
    这个是写错了,按照gomoku 的代码也是不行,第2个MessageBox也是弹不出来,为什么呢?
      

  4.   

    HWND hwnd = ::FindWindow(NULL, "test");
    if(hwnd != NULL)
    {
      DWORD dwThreadId = 0;
      GetWindowThreadProcessId(hwnd, &dwThreadId);
      if(dwThreadId != 0)
      {
        ::MessageBox(NULL, L"HELLO", L"HELLO", MB_OK);
      }
    }
      

  5.   

    abuseyoudna1981的代码可以,为什么呢?能说明下原因吗?在函数前面加上::和不加有什么区别呢?
      

  6.   

    带::是调全局函数,不带::是优先调类的成员函数,如果你是在CWnd的派生类里面执行,则调的是CWnd::FindWindow,其返回值是CWnd*指针,不是HWND。