?
Solved

PChar and memory  access problem

Posted on 2002-03-13
5
Medium Priority
?
312 Views
Last Modified: 2010-08-05
Hi
I am basically C++ and Java Programmer.
Now I am Learning Object Pascal(Delphi) for a new project of my company.

I would Like to know how can i allocate memory dynamically for required size using PChar.

When I try to use GetMem() method and pass that pointer to required method, I am getting run time exception saying that 'Access Violation for that pointer' . How can I over come this Problem.
Please Post me small example, So that i am thankful to you guys.


Thanks
Koundinya Sudhakar
0
Comment
Question by:sudhakar_koundinya
[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
5 Comments
 
LVL 7

Accepted Solution

by:
Motaz earned 200 total points
ID: 6861116
Use StrAlloc to allocate memory and StrDispose to free allocated memory:

var
  WinDir: PChar;
begin
  WinDir:= StrAlloc(100);
  GetWindowsDirectory(WinDir, 100);
  Label1.Caption:= WinDir;
  StrDispose(WinDir);
end;


0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 6861391
Thanks Motaz, for ur immediate response

I will test and give points

Koundinya



0
 
LVL 34

Expert Comment

by:Slick812
ID: 6863655
strAlloc is an good way, but since you asked about GetMem, here's some code for that

procedure GetText;
  var
  Length: Integer;
  Text: PChar;
  Str1: String;
begin
Length := GetWindowTextLength(hEdit1)+1;
GetMem(Text,Length);
GetWindowText(hEdit1,Text,Length);
Str1 := String(Text);
{or use
SetString(Srt1, Text, Length-1);}
FreeMem(Text);
end;

and another way

function GetWindowStr(WinHandle: THandle): String;
var
ResultStr: String;
begin
SetLength(ResultStr, GetWindowTextLength(WinHandle));
GetWindowText(WinHandle,@ResultStr[1],Length(ResultStr)+1);
Result := ResultStr;
end;

Pointers and Variables as Pointers are different in pascal than C code.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6864796
Hi Koundinya,

in Delphi you have better choices than using plain pchar. Well, sometimes pchar makes sense, but often Delphi strings are much more comfortable and sometimes even faster (due to reference counting).

If you would tell us a bit more about for which purpose you need this stuff, we can give advice as to what options you have besides using pchar.

Of course you can use pchar, if you want. See code examples above. If you can't get it to work, please post the code that produces the exception. We Delphians will surely see at once, what's wrong...

Regards, Madshi.
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 6879517
Hi Motaz,
Sorry for the late response and thanks for the good answer.


Hi Slick812 ,

Thanks for your answer also, I tried with that also, working fine. Thanks. But I think u understand me, as motaz gave the answer first, I am giving points to him.

Hi Madshi

My requirement is some thing like this,
I have to pass and parse the PChar streams through the functions

For e.g
ParseStream(stream:PChar,size:Integer,var str:PChar,siz:Integer)


So here my total requirement is with PChar Streams only.And Motaz and Slick812 Solved my problem.
Thanks

Koundinya
0

Featured Post

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!

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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

719 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