Solved

Working with LPWSTR,LPTSTR,DWORD etc

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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