?
Solved

Working with LPWSTR,LPTSTR,DWORD etc

Posted on 1998-03-06
4
Medium Priority
?
3,523 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 100 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 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