PDA

Click to See Complete Forum and Search --> : GetWindowText crashes the app


V(ery) Basic
Oct 1st, 2000, 09:30 AM
Thank you, and sorry about this but I thought I'd express my feelings in song:

I turn to you,
Like a flower turning towards the Sun,
I turn to you,
Because you're the only one,
You turn me around
when I'm upside-down

etc.

OK. Anyway, I thought that I was that little bit better than everybody else, and I'd make my own Foundation Classes, just nice and simple.

I've made nice ones using inheritance ( :D ) and the ones I've made all work, except the Edit one, here it is with the classes from which it inherits:


//Window Class

class Window
{
public:
void Show();
void Hide();
void SetFont(HFONT NewFont);
HWND GethWnd();
protected:
HWND itshWnd;
};


HWND Window::GethWnd()
{
return itshWnd;
}

void Window::Show()
{
ShowWindow(itshWnd, SW_NORMAL);
}

void Window::Hide()
{
ShowWindow(itshWnd, SW_HIDE);
}

void Window::SetFont(HFONT NewFont)
{
SendMessage(itshWnd, WM_SETFONT, (WPARAM) NewFont, (LPARAM) true);
}

//Control Class

class Control: public Window
{
protected:
HWND Owner;
HINSTANCE hIns;
};

//Edit Class

class Edit: public Control
{
public:
//constructor
Edit(RECT area, HWND OwnerhWnd, HINSTANCE hStance);
~Edit();
char *GetText();
void SetText(char *Text);
};

Edit::~Edit()
{
//Nothing
}

Edit::Edit(RECT area, HWND OwnerhWnd, HINSTANCE hStance)
{
Owner = OwnerhWnd;
hIns = hStance;
itshWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD , area.left, area.top, area.right - area.left, area.bottom - area.top, Owner, 0, hStance, NULL);
}

void Edit::SetText(char *Text)
{
SetWindowText(itshWnd, Text);
}

char *Edit::GetText()
{
int Len;
int IsError;
char *retVal = " ";
Len = GetWindowTextLength(itshWnd);
GetWindowText(itshWnd, (LPTSTR) retVal, Len+1);
return retVal;
}


SetText works fine, but as soon as I call GetText:


edVal1->GetText()


It raises a General Protection Fault.

And there was me thinking that the API couldn't actually cause a General Protection Fault because it was self-contained and if there was an error it just returned 0.

And I have checked that it is GetWindowText, because if it comment it out, it works fine, if I change the function to a void so that it just calls it for fun, it still crashes.

Is this a record for the first API that directly causes a General Protection Fault. Should I send this in to the Guinness Book of Records?

Please Help.

parksie
Oct 1st, 2000, 01:59 PM
Actually, the GPF is entirely your fault, because you passed it a bad buffer. Also, you can't pass the pointer back because as soon as you go out of scope, it kills it. You're best to use the string class for this:

#include <string>

using namespace std;

Then for your code:

string Edit::GetText() {
int Len;
int IsError;
char *pcBuf;
Len = GetWindowTextLength(itshWnd);
pcBuf = new char[Len+1];
GetWindowText(itshWnd, pcBuf, Len);
pcBuf[Len] = 0;
return pcBuf;
}

That should work. Also, bear in mind that if your code gets compiled using TSTR == wchar_t rather than char, then you'd need to use TCHARs everywhere else.

PS: I've already built a set of foundation classes ;)

V(ery) Basic
Oct 2nd, 2000, 11:56 AM
Thanks a lot for the answer. I'm sorry that I'm not exactly up to your standard, but I will be better than you when your die because dead people are crap programmers.

Originally posted by parksie
Actually, the GPF is entirely your fault


:(

Break it to me gently :rolleyes:

Originally posted by parksie
PS: I've already built a set of foundation classes



Oooh. Show-off. I'll have to come down to your house/cardboard box ;) and beat the $#!^ out of you. Actually, you're a bit older than me.,

I'd better not.

parksie
Oct 2nd, 2000, 12:33 PM
Now, I'm afraid there's one tiny little problem with that code. You know that GPF? Well, it's not exactly a situation with the API. There's no easy way to say this, but I think that it may have been indirectly or directly caused by you.

Standards? Me? I don't really mind how good anyone is as long as they think they're the best :D.

I'm older than you?!?!?! How old are you, V(ery)?

PS: I'm full of s**t so it wouldn't be too hard to beat it out of me :)

V(ery) Basic
Oct 2nd, 2000, 01:01 PM
It wasn't caused by me it was caused by inexperience

:D

Originally posted by a fat ar... parksie
I'm older than you?!?!?!


Didn't you guess from my infantile remarks and improper use of the language. My childish inuendos and the quirky juxtaposition of the prose which I employ?

Innit?
Originally posted by an ugly wan... parksie

TSTR == wchar_t rather than char, then you'd need to use TCHARs everywhere else.



Eh?

Language of chioce: English

If I wanted to speak in C++ I would say:


#include <iostream.h>

int main()
{
bool SpeakInEnglish;
if (SpeakInEnglish) then
cout >> "Is it cos I is black?\n";
else
cout >> "Is it cos I is 001D426B\n";
return 1;
}


See?

parksie
Oct 2nd, 2000, 01:08 PM
Okay...the evil god of inexperience (he was once annoying me, too - just remember that one :))

That TCHAR thingie is MS' way of saying "A character, either Unicode or ANSI, depending on how you compile your program".

I thought your infantile remarks were the result of extended conditioning by John so that this board would have some interest other than random dry gibberings about programming.

Had any more of those "warning" emails yet? :D

V(ery) Basic
Oct 5th, 2000, 12:48 PM
No. I haven't referred to hunts or group lucking. (Words have been changed slightly ;) )

Thanks again for the advice.

Programming is highly interesting...


//Not exactly the finished WndProc, if you get what I'm saying
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent ;
HWND hWndCtl;
char * Value1;
char * Value2;
long int RVal;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
hWndCtl = (HWND) lParam;
// Parse the menu selections:
if (hWndCtl == cmdCalc->GethWnd())
{

switch (cmbOper->GetItem())
{
case CB_ERR:
MessageBox(hWnd, "No Operation was entered", "Error", MB_OK)
break;
}
}

if (!HIWORD(wParam))
{
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
RECT rct;
HBRUSH hBrush;
POINT p;

GetWindowRect(hWnd, &rct);

p.x = rct.top;
p.y = rct.left;
ScreenToClient(hWnd, &p);
rct.top = p.x;
rct.left = p.y;

p.x = rct.bottom;
p.y = rct.right;
ScreenToClient(hWnd, &p);
rct.bottom = p.x;
rct.right = p.y;

hBrush = GetSysColorBrush(COLOR_BTNFACE);
hdc = BeginPaint(hWnd, &ps);
FillRect(hdc, &rct, hBrush);
DeleteObject((HGDIOBJ) hBrush);
ReleaseDC(hWnd, hdc);

break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}


Fascinating, just fascinating. You can luck the rasterbating and the tondage, C++ provides my cheap thrills.

Oooh.


Aah.





Okay, that's enough.