0

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!

Ken White
  • 123,280
  • 14
  • 225
  • 444
justauser
  • 37
  • 1
  • 9
  • But... what is the question? – rodrigo Jan 04 '14 at 22:51
  • The question is why is sendmessage returning no text? – justauser Jan 04 '14 at 23:09
  • 1
    I rolled back your edit. You cannot totally change the question to an entirely different one once someone has answered your question. It makes the answers you've received look foolish, wastes the time of the people who answered you, and isn't how SO works. If you now have a totally different question, post a new question instead. The question you originally asked was about `GetProcessID` failing, and that's the question that was answered here. – Ken White Jan 04 '14 at 23:25
  • Ok, sorry about that, I just felt like it is a waste and spamming the site starting a new one. – justauser Jan 04 '14 at 23:37

1 Answers1

2

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.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 1
    As I said, that gets you a window handle (HWND). You need a process handle (HPROCESS) to pass to `GetProcessID`. I'm sure the question of how to get a process handle from a window handle has been asked (and answered) here at SO before. – Ken White Jan 04 '14 at 23:23