Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why access violoation?

Posted on 2001-07-12
6
Medium Priority
?
159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Accepted Solution

by:
Jaymol earned 80 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

618 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