Solved

Why access violoation?

Posted on 2001-07-12
6
150 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Expert Comment

by:rondi
Comment Utility
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
Comment Utility
Actually, all answers work fine.  But Jaymol's answer arrived first.
0
 
LVL 6

Expert Comment

by:Jaymol
Comment Utility
Thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

9 Experts available now in Live!

Get 1:1 Help Now