Solved

Beginner    GetWindowText

Posted on 2000-02-18
5
236 Views
Last Modified: 2008-03-10
I created an edit control with this:

HWND hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"Edit","",(WS_CHILD | WS_VISIBLE ),90, 20,120,20,hWnd,(HMENU)(106),hInstance,NULL);

Is this the best way to create it and how do i get the text from it?
0
Comment
Question by:scooter1
5 Comments
 

Expert Comment

by:kishj
ID: 2537483
scooter1,

it depends on what your goals are and what development tools you are using to create a window. Usually your development ide (MS, Borland, etc.) supply a frameworks which let you create a window by doing a "new" on some type of object that manages and organizes the interface for you.

I would (using OWL from borland) do a new on a TWindow object. MS VC++ or CBuilder would be different in particulars. See your examples that come with your tools.

You can get text from the caption of a window, or from child controls of a window.

To get the caption from a window you can use bare bones windows call like:

int GetWindowText(

    HWND hWnd,      // handle of window or control with text
    LPTSTR lpString,      // address of buffer for text
    int nMaxCount       // maximum number of characters to copy
   );
Parameters

hWnd

Identifies the window or control containing the text.

lpString

Points to the buffer that will receive the text.

nMaxCount

Specifies the maximum number of characters to copy to the buffer. If the text exceeds this limit, it is truncated.

 

Return Values

If the function succeeds, the return value is the length, in characters, of the copied string, not including the terminating null character. If the window has no title bar or text, if the title bar is empty, or if the window or control handle is invalid, the return value is zero. To get extended error information, call GetLastError.
This function cannot retrieve the text of an edit control in another application.

So for your window above you would have:

if (hwnd)// if memory was allocated
{
  if (::IsWindow(hwnd))// if the window was created successfully
  {
  int nCharsReceived = 0;
  int nMaxCharsToReceive=256;
  char szWindowText[257];
    memset(szWindowText,0,257);// initialize your string
    nCharsReceived = ::GetWindowText(hwnd,szWindowText,nMaxCharsToReceive);
    if (nCharsReceived < 1) // error handling or no text was there
    {
      return;
    }
    // handle text here
  }
}
0
 

Author Comment

by:scooter1
ID: 2537515
i'm using VC++ 6.0 but not MFC
lets say i wan't to check like a password.

char buffer[40];
GetWindowText(hwnd,buffer,39);
if (buffer == "thepassword")
{
///dosomething here
}

using hwnd from the edit control above does not seem to work, what's the prob.?

0
 
LVL 5

Expert Comment

by:Wyn
ID: 2537580
Try GetLastError() after the GetWindowText() and find the error.Hwnd should work.
Regards
Wyn
0
 
LVL 1

Accepted Solution

by:
JMu earned 20 total points
ID: 2537807
scooter1, you are making the beginners mistake in comparison. You compare pointers.

Do it this way.

if ( lstrcmp( buffer, "thepassword" ) == 0 )
{
  // OK
}

When you start using C++ and CString class, you can compare strings the way you just did.

And, please call GetWindowText( hwnd, buffer, sizeof(buffer) );

Windows functions usually "wants" the whole buffer size. You are telling that there are 40 bytes available. So, you get maximum of 39 characters. In your example, you only get max. of 38 chars.

JMu
0
 

Author Comment

by:scooter1
ID: 2539108
ahh, muchas gracias
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now