Solved

PChar and memory  access problem

Posted on 2002-03-13
5
301 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
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 33

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

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

Suggested Solutions

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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

12 Experts available now in Live!

Get 1:1 Help Now