Solved

PChar and memory  access problem

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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