Solved

Why access violoation?

Posted on 2001-07-12
6
155 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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