Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between char * and LPSTR in windows

I apologise if it is a basic or silly question. What is the difference between char* and LPSTR. where the sizeof both gives 4 bytes in my compiler. Can someone explain me in detail. thanks..

like image 594
2vision2 Avatar asked Aug 14 '12 04:08

2vision2


People also ask

What is Lpstr C++?

The LPSTR type and its alias PSTR specify a pointer to an array of 8-bit characters, which MAY be terminated by a null character. In some protocols, it is acceptable to not terminate with a null character, and this option will be indicated in the specification.

What does Lpcstr mean?

LPCSTR is a pointer to a const string (LP means Long Pointer) LPCTSTR is a pointer to a const TCHAR string, ( TCHAR being either a wide char or char depending on whether UNICODE is defined in your project)

What is a Tchar?

TCHAR is simply a macro that expands to char in ANSI builds (i.e. _UNICODE is not defined) and wchar_t in Unicode builds ( _UNICODE is defined). There are various string types based on the TCHAR macro, such as LPCTSTR (long pointer to a constant TCHAR string).

What is Lpwstr C++?

The LPWSTR type is a 32-bit pointer to a string of 16-bit Unicode characters, which MAY be null-terminated. The LPWSTR type specifies a pointer to a sequence of Unicode characters, which MAY be terminated by a null character (usually referred to as "null-terminated Unicode").


3 Answers

LPSTR is a Windows type, meant to be be the same regardless of what platform you were compiling on. It's a long pointer to a string.

Back in the days of segmented architecture (the old 64K segments, not the newer selector-based segmented memory), where you had tiny, small, medium, large and huge memory models, it was important that the Windows type was always the same, regardless of what type of pointer char * was.

So, if you complied with different compilers where the underlying types were different, the windows.h header file would define LPSTR to compensate for that.

For example, Borland C may have had a sixteen-bit char * and LPSTR may have had to be defined as far char * for it. In a compiler where char * was already a long/far pointer, LPSTR would have just used that instead.

Nowadays, with 32+ bit flat models, there is probably no real need for such shenanigans, though it may still happen with things like thunking between 64-bit and 32-bit code. Still, the types defined back then are still with us and still very much in use.

like image 135
paxdiablo Avatar answered Oct 11 '22 16:10

paxdiablo


Basically, the LP* pointers were to indicate to use a 32 bit pointer on 16 bit versions of Windows:

From WikiBooks

The letters "LP" or the prefix "lp" stands for "Long Pointer", which is exactly the same as a regular pointer on 32 bit machines. LP data objects are simply legacy objects that were carried over from Windows 3.1 or later, when pointers and long pointers needed to be differentiated. On modern 32-bit systems, these prefixes can be used interchangeably.

like image 33
StuartLC Avatar answered Oct 11 '22 15:10

StuartLC


The difference is burried in the depths of time. LPSTR stands for "long pointer to string". Back before 32-bit processors, pointers to memory that might be in a different segment of memory (think, a long way away in memory), needed extra space to store.

On 32-bit (and later) processors, they're exactly the same thing. Microsoft uses LPSTR solely for historic reasons.

like image 34
Martin Avatar answered Oct 11 '22 15:10

Martin