Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

PChar and memory access problem

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
sudhakar_koundinya
Asked:
sudhakar_koundinya
1 Solution
 
MotazCommented:
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
 
sudhakar_koundinyaAuthor Commented:
Thanks Motaz, for ur immediate response

I will test and give points

Koundinya



0
 
Slick812Commented:
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
 
MadshiCommented:
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
 
sudhakar_koundinyaAuthor Commented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now