Solved

Working with LPWSTR,LPTSTR,DWORD etc

Posted on 1998-03-06
4
3,091 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now