Jump to content
Tuts 4 You

How To Convert String To Pansichar Type?


ChupaChu

Recommended Posts

Posted (edited)

How do i convert Strings to PChar type?

And how can i concatenate parts of type string with parts of type PChar and PAnsiChar?

or

How do i concatenate (put together/stick) multiple variables (in one) of type PChar?

if i use 'bla bla' in function that requires PChar it is ok, but

if i want to put together my string and for example TEMP path of my system

(GetTempPath(255,P) will give us P in PChar type that has value of temp folder location)

How to do that?

Edited by ChupaChu
Posted

You should go and ask google before you create posts here. Nobody wants you to search for an hour but you should at least try finding it by yourself.

There should be a ****load of info about Delphi strings, nothing that seriously needs to be discussed.

It's fine if you don't find anything in a reasonable time or if there's some sort of bug you can't figure out.

Not trying to be rude, this is just an advice :)

Maybe this helps:

http://www.delphibasics.co.uk/RTL.asp?Name=ShortString

Especially take a look at the 'Related Commands', I guess Concat is what you're looking for ;)

Posted (edited)

Thanks for pointing that one out, offcourse i have searched before i ask.

It must be a bug of some kind, once it works - compiles without an error

and when you copy this code to other function it will throw an error (at

least it does on my 7.2 version).

Its CreateFile and GetSystemPath that are buggy in some way.

For example If you have this:

hFile :THandle;

Buffer: array [0 .. 4095] of Char;

temp:AnsiString;

i:Cardinal;

label finito;

begin

hFile := CreateFile('\\F:\testFile.dat', GENERIC_READ or GENERIC_WRITE,

FILE_SHARE_READ or FILE_SHARE_WRITE, NIL,

OPEN_EXISTING, FILE_ATTRIBUTE_HIDDEN, 0);

If (hFile <> INVALID_HANDLE_VALUE) Then Result := True;

Now Lets say i have a name in string type and want to concatenate it to some path:

var ss:string;

GetTempPath(i,buffer);

temp:=buffer;

ss:='\\?\' + temp + ss;

//a) now this will not work:

hFile := CreateFile(PAnsiChar(ss)), GENERIC_READ or GENERIC_WRITE, //...

// B) nor this:

hFile := CreateFile('\\?\'+temp+ss), GENERIC_READ or GENERIC_WRITE,

//c) this will work:

hFile := CreateFile(PAnsiChar('\\?\'+temp+ss)), GENERIC_READ or GENERIC_WRITE,

FILE_SHARE_READ or FILE_SHARE_WRITE, NIL,

OPEN_EXISTING, FILE_ATTRIBUTE_HIDDEN , 0);

If (hFile <> INVALID_HANDLE_VALUE) Then

begin Result := True;

CloseHandle(hFile);

// now if you copy all this to new function, declare same variables you will be suprised when you get en error message on line //c) in fact i needed to adjust it to

hFile := CreateFile(GetTempPath(i,buffer)+'myfile.dat'), GENERIC_READ or GENERIC_WRITE,... in order to get it to work, although it works now without UNC names

as GetTempPath(i,buffer) returns something like this X:\ddd\dd\s\, if i try to add '\\.\' in front of it - it just wont work - it drived me crazy.

Now i got it working somehow, but i still dont get whats wrong with it.

Regards, ChupaChu!

Edited by ChupaChu

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...