在程序中我使用這一個語句,為什么會出錯?
#include "winuser.h"
....
bool KeyLib::GetAllTopWindow()
{
FreeWindow(WindowRoot);
if(EnumWindows((WNDENUMPROC)EnumWindowsProc, 0 ))
{
FreeSnapshot();
GetSnapshot();
pWindow=WindowRoot;
while(pWindow)
{
pThread=ThreadRoot;
        while ( pThread )
{
              if ( pThread->ThreadId == pWindow->ThreadId )
  {
              pWindow->Thread = pThread;
              pProcess = ProcessRoot;
              while ( pProcess )
              {
                if ( pProcess->ProcessId == pThread->OwnerProcessId )
                {
                   pWindow->ProcessId = pProcess->ProcessId;
                   pWindow->Process   = pProcess;
                   strcpy ( pWindow->FileName, pProcess->FileName );
                   break;
                }
                pProcess = pProcess->Next;
             }
             break;
          }
          pThread = pThread->Next;
       }
       pWindow = pWindow->Next;
    }
    return true;
  
  }
  else return false;
  
}
出錯提示如下:
E:\myprogramm\KeyMessage\KeyLib.cpp(378) : error C2440: 'type cast' : cannot convert from '' to 'int (__stdcall *)(struct HWND__ *,long)'
        None of the functions with this name in scope match the target type
E:\myprogramm\KeyMessage\KeyLib.cpp(439) : error C2440: 'type cast' : cannot convert from '' to 'int (__stdcall *)(struct HWND__ *,long)'
        None of the functions with this name in scope match the target type
Error executing cl.exe.KeyMessage.exe - 2 error(s), 0 warning(s)

解决方案 »

  1.   

    应该函数EnumWindowsProc的问题把?
    把她定义成静态成员函数或者全局函数。
      

  2.   

    我试验了一下,是这个问题,你把EnumWindowsProc定义成一般的成员函数了,需要定义成静态成员函数
    (不是静态成员函数,就出现这样的问题,因为静态成员函数没有this指针!)
      

  3.   

    你这个EnumWindowsProc函数定义了吗
      

  4.   

    看一下下面的代码:
    #include <iostream>
    #include <windows.h>
    using namespace std;class CA {
    private:
    static BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
    const int BUFF_SIZE = 256;
    static TCHAR szWndTxt[BUFF_SIZE]; GetWindowText(hWnd, szWndTxt, BUFF_SIZE);
    cout << szWndTxt << endl; return TRUE;
    };

    public:
    void Func() const {
    if(EnumWindows((WNDENUMPROC)EnumWindowsProc, 0 )) {
    cout << "Y" << endl;
    }
    }
    };void main(void) {
    CA objA;
    objA.Func();
    }
      

  5.   

    The EnumWindowsProc function is an application-defined callback function 注意是一个“an application-defined callback function”
      

  6.   

    我是這樣定義的,怎么改啊?
    能否講詳細一點?基礎太差了!
    謝謝!
    BOOL CALLBACK KeyLib::EnumWindowsProc(HWND hwnd, LPARAM lparam)
    {
    int i=sizeof(TWINDOW);
    if(ChildRoot==NULL)
    {
    pWindow2=new TWINDOW;
    ChildCount=1;
    memset(pWindow2,0,i);
    ChildRoot=pWindow2;
    }
    else
    {
    ChildCount++;
    pWindow2->Next=new TWINDOW;
    pWindow2=pWindow2->Next;
    memset(pWindow2,0,i);
    }
      pWindow2->Visible=IsWindowVisible(hwnd);
      pWindow2->Handle   = hwnd;
      pWindow2->ThreadId = GetWindowThreadProcessId(hwnd,&(pWindow2->ProcessId));
      GetClassName(  hwnd, pWindow2->ClassName, MAX_PATH);
      int j = GetWindowTextLength(hwnd);
      pWindow2->Name = new char[j+4];
      GetWindowText( hwnd, pWindow2->Name, j+4);
      return true;}BOOL CALLBACK KeyLib::EnumChildProc(HWND hwnd, LPARAM lParam)
    {
    int i=sizeof(TWINDOW);
    if(ChildRoot==NULL)
    {
    pWindow2=new TWINDOW;
    ChildCount=1;
    memset(pWindow2,0,i);
    ChildRoot=pWindow2;
    }
    else
    {
    ChildCount++;
    pWindow2->Next=new TWINDOW;
    pWindow2=pWindow2->Next;
    memset(pWindow2,0,i);
    }
    pWindow2->Visible=IsWindowVisible(hwnd);
    pWindow2->Handle=hwnd;
    pWindow2->ThreadId=GetWindowThreadProcessId(hwnd,&(pWindow2->ProcessId));
    GetClassName(hwnd,pWindow2->ClassName,MAX_PATH);
    int j=GetWindowTextLength(hwnd);
    pWindow2->Name=new char[j+4];
    GetWindowText(hwnd,pWindow2->Name,j+4);
    return true;
    }
      

  7.   

    在EnumWindowsProc成员函数“声明”(注意是声明,不是定义)前加static
      

  8.   

    對不起,再問一下,在哪加呢?
    class KeyLib  
    {
    public:
    static BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam);
    static BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lparam);
    ...
    我加在那里,還是不行啊?