Solved

Why access violoation?

Posted on 2001-07-12
6
151 Views
Last Modified: 2010-04-06
I always get access violation, can anyone tell me why and how I can fix it?

procedure TForm1.Button1Click(Sender: TObject);
var
   abc:pchar
   h    :hwnd;
begin
     // h is obtained from findwindow, or other ways, etc.
     GetWindowText(h,abc,255);
     beep;
end;

Thank you.
0
Comment
Question by:nyt
6 Comments
 
LVL 6

Accepted Solution

by:
Jaymol earned 20 total points
ID: 6277093
Hi,

This is what I'd do....

procedure TForm1.Button1Click(Sender: TObject);
var
  abc:pchar
  h    :hwnd;
begin
    GetMem(abc, 255);
    // h is obtained from findwindow, or other ways, etc.
    GetWindowText(h,abc,255);
    beep;
    // Do stuff with abc here...
    FreeMem(abc, 255);
end;

Hope it helps,

John.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6277111
Hi nyt,

var
  abc: array[0..255] of char;
...
begin
...
  GetWindowText(H, Abc, 255);
  ShowMessage(Abc);
end;
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6277241
abc is a Pointer to a memory location.
You do not initialize it to point to an allocated piece of memory so it has a random value as address it points to.
This address is not valid so you get an access violation.
You are lucky. It could have pointed to a valid address and you would overwrite parts of your programs data.

The other comments show you the correct code.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:rondi
ID: 6279687
procedure TForm1.Button1Click(Sender: TObject);
var
 abc: pchar
 h  : hwnd;
begin
   abc := StrAlloc(255);
   // h is obtained from findwindow, or other ways, etc.
   GetWindowText(h,abc,255);
   Beep;
   // Do stuff with abc here...
   StrDispose(abc);
end;
0
 

Author Comment

by:nyt
ID: 6279893
Actually, all answers work fine.  But Jaymol's answer arrived first.
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 6280081
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 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

21 Experts available now in Live!

Get 1:1 Help Now