Thread: GetWindowTextLength

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    GetWindowTextLength

    I cant see what im doing wrong.

    Neither GetWindowTextLength(hwnd) nor GetWindowText(hwnd, buf2, length)
    returning anything.


    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        HWND hwnd;
        char  *buf2;
    
        int length;
        length=GetWindowTextLength(hwnd);
        buf2 = new TCHAR[length+1];
    
        int nChars = GetWindowText(hwnd, buf2, length);
        cout << "Windowtext: " << buf2 << "\n";
        cout << length << " characters.\n";
        delete[] buf2;
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you have test code where your hwnd points to a window?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Well i thought it pointed by default to the window of the program.

    Isnt it the case?
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, no. It's just an uninitialized variable.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Ok, thank you Tabstop!

    So i have to find out how to point it somewhere.
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since you're using main and not WinMain, perhaps you want to look here: How To Obtain a Console Window Handle &#40;HWND&#41;

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Great! Thanks a lot!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GetWindowTextLength() = crash ?
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 07-04-2005, 11:00 AM