Question

I posted a little earlier with a sendmessage issue and we came to the conclusion that it would be extremely hard to get the chat window from Xchat. I have now moved to ThrashIRC and using spy++ was able to find the chat window(highlighted):

enter image description here

As you can see it does have a caption which means that it does see the text. Here is the code that I am using to get the HWND's and the text:

#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;
}

The buffer is only that large because 1: I thought it might have been an error where the buffer was to small, and 2: the buffer is going to have to be large because of the amount of text. Any suggestions / code would be greatly appreciated! Thanks!

Was it helpful?

Solution

GetProcessID expects to be passed a valid process handle, as is clearly stated in the MSDN documentation. You're calling it with NULL as the argument:

if (!GetProcessId(NULL))

Why would you expect it to return anything but an invalid handle error when you're clearly providing it an invalid process handle?

Just to make sure you're aware, an HWND is a window handle, not a process handle. FindWindow and FindWindowEx return a window handle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top