Solved

Why access violoation?

Posted on 2001-07-12
6
152 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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