GetProcessId失败,错误6:句柄无效

时间:2014-01-04 22:03:15

标签: c++ winapi sendmessage hwnd

我之前发布了一个sendmessage问题,我们得出的结论是,从Xchat获取聊天窗口非常困难。我现在已经转移到ThrashIRC并使用spy ++能够找到聊天窗口(突出显示):

enter image description here

正如你所看到它确实有一个标题,这意味着它确实看到了文字。这是我用来获取HWND和文本的代码:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <strsafe.h>

using namespace std;

void FindThrash()
{
    cout << "[ThrashIRC]" << endl;

cout << "| find ThrashIRC window" << endl;
HWND hwndThrashIRC = FindWindow(L"ThrashIRC", NULL);

if (NULL != hwndThrashIRC)
{
    cout << "   + found ThrashIRC window" << endl;
    cout << "| find MDIClient window" << endl;
    HWND hwndMDIClient = FindWindowEx(hwndThrashIRC, NULL, L"MDIClient", NULL);

    if (NULL != hwndMDIClient)
    {

        cout << "   + found MDIClient window" << endl;
        cout << "| find DefChannel window" << endl;
        HWND hwndDefChannel = FindWindowEx(hwndMDIClient, NULL, L"DefChannel", NULL);

        if (NULL != hwndDefChannel)
        {
            cout << "   + found MDIClient window" << endl;
            cout << "| find RichEdit20W window" << endl;
            HWND hwndRichEdit20W = FindWindowEx(hwndDefChannel, NULL, L"RichEdit20W", NULL);

            if (NULL != hwndRichEdit20W)
            {
                cout << "   + found RichEdit20W window" << endl << endl;
                cout << "- get text " << endl;

                const int bufferSize = 32768;
                char textBuffer[bufferSize] = "";
                SendMessage(hwndRichEdit20W, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);

                cout << "[begin text]" << endl;
                cout << textBuffer << endl;
                cout << "[end text]" << endl;

            }
            else
            {
                cerr << "RichEdit20W not found." << endl;
            }




        }
        else
        {
            cerr << "DefChannel not found." << endl;
        }




    }
    else
    {
        cerr << "MDIClient not found." << endl;
    }

}
else
{
    cerr << "ThrashIRC not open." << endl;
}
}

void ErrorExit(LPTSTR lpszFunction)

{



// Retrieve the system error message for the last-error code

LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();

FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    dw,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    (LPTSTR)&lpMsgBuf,
    0, NULL);

// Display the error message and exit the process

lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
    (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40)*sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
    LocalSize(lpDisplayBuf) / sizeof(TCHAR),
    TEXT("%s failed with error %d: %s"),
    lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}

int main()
{

    FindThrash();
    if (!GetProcessId(NULL))
        ErrorExit(TEXT("GetProcessId"));

    return 0;
}

缓冲区只有那么大,因为1:我认为它可能是一个错误,缓冲区很小,而2:由于文本的数量,缓冲区必须很大。任何建议/代码将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:2)

期望{p> GetProcessID传递有效的流程句柄,如MSDN documentation中明确指出的那样。你用NULL作为参数调用它:

if (!GetProcessId(NULL))

为什么当你清楚地向它提供无效的进程句柄时,你会期望它返回任何但是无效的句柄错误?

为了确保您知道,HWND是窗口句柄,而不是进程句柄。 FindWindowFindWindowEx会返回窗口句柄