Link to home
Start Free TrialLog in
Avatar of bobness
bobness

asked on

Working with LPWSTR,LPTSTR,DWORD etc

I often encounter the data types LPWSTR, LPTSTR, DWORD and have a semi-understanding of what they are, but how does one translate between, say, a LPWSTR or a LPTSTR and a STRING or a PCHAR in Delphi? Or between a DWORD and an INTEGER? I'm certain there must be a simple go between ... ?
Avatar of inter
inter
Flag of Türkiye image

Hi friend,

Here are what I know:

LPTSTR   :  It is a long pointer to string(in win32 everty pointer is long) It declares a pointer to the null terminated string(any number of characters terminated with #0 character, so you can not use #0 in a null terminated string. Each character occupies 1 byte -this may seem nonsense to you but read the next type-.
LPWSTR  : This is specific to Win95 and WinNT. Each character is 2 byte long so we have 65536 codes possible for a character not 256! This is for national language support of win32. By this way A string can be Chinese, Arabian or whatever! which is not possible with 256 characters(or partially possible with that anoying CodePage stuff)
DWORD  : This is just unsigned long int (32 bit unsigned integer for Win32 and all others)
INTEGER    : For Delphi 2.0+ 32 bit signed integer value, For Delphi 1.0 16 bit signed integer value. So when reading or writing it to a storage device it is a good practice to check size with SizeOf(Integer) directive
PCHAR  : It is pointer to the character, compatible with LPSTR

Conversions
-----------------

In most cases we used typecasting:
1 - Let x be Integer and we want to convert it to DWORD y
   y := DWORD(x);
   or in the other way
   x := Integer(y);
2 - As I said PChar is compatible with LPSTR and you can substitude PChar variables to the WinAPI functions expecting LPSTR.
3 - String to LPSTR or PChar (let x be string, y be LPSTR-you should allocate y by yourself)
    x := StrPas(y);
    or in the other way
    StrPCopy(y, x);
4 - The WideChar routines are a bit complicated and there is a type partially compatible with LPWSTR in Delphi called PWideChar.(So let x be string and y be PWideChar)
    x := WideCharToString(y);
    in other way round
    StringToWideChar(x, y, Length(x));

May this clears the things
Igor
ASKER CERTIFIED SOLUTION
Avatar of TheSwine
TheSwine

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi there,

You are right but since the data length  is the same for both, you can force(typecast) the conversion. Same holds for ShortInt and Byte. Some times it is necessary,  e.g. for winapi calls you should cast from integer to DWord.

Regards,
Igor
Avatar of TheSwine
TheSwine

Igor
Integer have 31 bits and one sign bit.
DWORD have 32 bits!
anyway it is recomended to work in delphi with DWORD when it necessary (like many programes do):
var
  X : DWORD;