Solved

PChar and memory  access problem

Posted on 2002-03-13
5
307 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 50 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

Industry Leaders: 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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

632 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