Solved

Working with LPWSTR,LPTSTR,DWORD etc

Posted on 1998-03-06
4
3,204 Views
Last Modified: 2008-02-01
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 ... ?
0
Comment
Question by:bobness
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:inter
ID: 1360224
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
0
 
LVL 1

Accepted Solution

by:
TheSwine earned 50 total points
ID: 1360225
What igor wrote is True accept the second section about Integer and DWORD.
You can not convert DWORD to integer since Integer ( 32 bit )
is in the range of -2147483648..2147483647 and DWORD is in the range of 0..4294967294. It's like ShortInt vs. Byte.

in the next address you'll find translation table that help you a lot:
http://www.drbob42.com/delphi/default.htm
under Electronic articles section, HeadConv - C
Header Converter topic
0
 
LVL 5

Expert Comment

by:inter
ID: 1360226
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
0
 
LVL 1

Expert Comment

by:TheSwine
ID: 1360227
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;

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

828 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