473,804 Members | 3,273 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Style question - using LPCTSTR as a pointer

I'm using a system in which TCHAR is typedef-ed to represent a
character, in this case a wchar_t to hold Unicode characters, and
LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume
it's supposed to be a pointer to constant TCHAR, though they seem to
be defined in parallel rather than one typedef using the other.

I'm perfectly happy using LPCTSTR for a constant string, but for some
reason it seems odd to use LPCTSTR as an actual pointer. For instance,
in the following snippet:

for(ptr = str; *ptr; ptr++)

I am happy for str to be a LPCTSTR, but ptr feels like it should be a
const TCHAR*. Even though they are of course the same type.

Am I just weird?

Paul.
Aug 9 '08 #1
4 3388
TCHAR is a wchar_t if UNICODE is defined. Otherwise it is a plain char.

LPCTSTR is Microsoft nonsense stuff. You can use eg const TCHAR* etc.

Of course, using std::string is much safer.
<gw****@aol.com wrote in message
news:94******** *************** ***********@s50 g2000hsb.google groups.com...
I'm using a system in which TCHAR is typedef-ed to represent a
character, in this case a wchar_t to hold Unicode characters, and
LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume
it's supposed to be a pointer to constant TCHAR, though they seem to
be defined in parallel rather than one typedef using the other.

I'm perfectly happy using LPCTSTR for a constant string, but for some
reason it seems odd to use LPCTSTR as an actual pointer. For instance,
in the following snippet:

for(ptr = str; *ptr; ptr++)

I am happy for str to be a LPCTSTR, but ptr feels like it should be a
const TCHAR*. Even though they are of course the same type.

Am I just weird?

Paul.

Aug 9 '08 #2
gw****@aol.com wrote:
I'm using a system in which TCHAR is typedef-ed to represent a
character, in this case a wchar_t to hold Unicode characters, and
LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume
it's supposed to be a pointer to constant TCHAR, though they seem to
be defined in parallel rather than one typedef using the other.

I'm perfectly happy using LPCTSTR for a constant string, but for some
reason it seems odd to use LPCTSTR as an actual pointer. For instance,
in the following snippet:

for(ptr = str; *ptr; ptr++)

I am happy for str to be a LPCTSTR, but ptr feels like it should be a
const TCHAR*. Even though they are of course the same type.

Am I just weird?

Paul.
It's a microsoft-specific quite, but the answer could be that TCHAR
under UNICODE is defined to WCHAR, not wchar_t and WCHAR is *not
always* :-) same as wchar_t. See
http://msdn.microsoft.com/en-us/libr...97(VS.85).aspx for the
details.

In the future for the problems like this I advice you to pre-process
your code and look into the output of the pre-processor to get a
definitive answer on what the identifiers you use really mean.

Hope this will help,
-Pavel
Aug 9 '08 #3
gw****@aol.com wrote:
I'm using a system in which TCHAR is typedef-ed to represent a
character, in this case a wchar_t to hold Unicode characters, and
LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume
it's supposed to be a pointer to constant TCHAR, though they seem to
be defined in parallel rather than one typedef using the other.

I'm perfectly happy using LPCTSTR for a constant string, but for some
reason it seems odd to use LPCTSTR as an actual pointer. For instance,
in the following snippet:

for(ptr = str; *ptr; ptr++)

I am happy for str to be a LPCTSTR, but ptr feels like it should be a
const TCHAR*. Even though they are of course the same type.

Am I just weird?
I'm weighing in a little late but...

In general if I'm using assignment, I would not change the type unless I
had an overwhelming reason to do so. So for your example, if 'str' is a
LPCTSTR then I would make 'ptr' and LPCTSTR. To do anything else, makes
assumptions about the type that you simply should not make.

Of course, there is the issue that James Kanze raised, maybe 'str'
shouldn't be an LPCTSTR in the first place. I don't do Windows
programming so I can't answer that question.
Aug 10 '08 #4
<gw****@aol.com wrote in message news:94******** *************** ***********@s50 g2000hsb.google groups.com...
I'm using a system in which TCHAR is typedef-ed to represent a
character, in this case a wchar_t to hold Unicode characters, and
LPCTSTR is typedef-ed to be a pointer to constant wchar_t.
TCHAR is a Microsoft Platform SDK (the SDK you use to write Win32 / Win64 Apps) defined type that, depending on the definition of "UNICODE" holds either an ansi (CHAR), or unicode (WCHAR) character.

LPCTSTR is a long pointer (LP) to a const (C) ansi or unicode text (T) zero terminated string (STR).

Also, even when TCHAR or TSTR is a WCHAR or WSTR, they still don't really point to wchar_t - the size of wchar_t is left up to the compiler to define - GCC for example defines wchar_t to be 32 bits. There are gcc environments that target windows so thsi problem while largely theroetical, cannot be ignored if you need to write cross platform code that interoperates with windows types or APIs.
I presume it's supposed to be a pointer to constant TCHAR, though
they seem to be defined in parallel rather than one typedef using
the other.
They are defined in parallel because the *STR pointers point to strings - arrays of characters that are zero termianted, while *CHAR doesnt imply more than 1 character.
I'm perfectly happy using LPCTSTR for a constant string, but for some
reason it seems odd to use LPCTSTR as an actual pointer. For instance,
in the following snippet:

for(ptr = str; *ptr; ptr++)

I am happy for str to be a LPCTSTR, but ptr feels like it should be a
const TCHAR*. Even though they are of course the same type.
LPCTSTR is logically more correct because you are enumerating characters in a string.


Aug 12 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7553
by: Tom | last post by:
Is there such a type in C#. I have a C++ application that uses LPCTSTR to convert a Long Pointer to a Constant String? Thanks Tom
0
1240
by: Dirk Evertz | last post by:
Hi all, I've experienced a problem when passing a smart-pointer (_com_ptr_t) by value to another function using managed extensions. This only seems to happen if the object is created via new. Can anyone confirm this issue? I wrote a little program that demonstrates this behaviour: Main.cpp:
8
12167
by: ppcdev | last post by:
Here's what I try : LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str); c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot convert from 'System::IntPtr' to 'LPCWSTR' I really get mad with that !!!
10
25491
by: farseer | last post by:
How can i do this? i'd like to call the following code: .... string url = <my urld>; TCHAR* urlParams = GetParams( ); url.append( (char * ) urlParams ); GotoURL( ( LPCTSTR ) url ); <------THIS IS MY ISSUE ....
0
3736
by: Dean Hallman | last post by:
Hello, I am developing a BHO that should add a custom HTTP header on a specific domain only. Don't want the header globally, otherwise I could just add a registry key. So, on BEFORENAVIGATE2, I am canceling the current navigation and renavigating with the new custom header using Navigate2. This usually works fine, but in some cases (particularly when selecting a link that starts a new window), the Navigate2 call fails.
10
17452
by: Lucy Ludmiller | last post by:
Anyone knows how to convert a LPCTSTR to an STL striung?. Can't seem to finda nyting (that dosen't blab on for several pages) on the net about how to do this
14
10645
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have seen the following function to convert from a System::String^ to a const wchar_t*. I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to const wchar_t*. Then it should all work right? But I only get the first character. And when I try to do std::wstring l_s(convert(somestring)) I get really strange characters into l_s string representation, but when I check l_s individual characters they look ok. const wchar_t* convert(...
7
12231
by: justin001 | last post by:
In my _UNICODE & _MBCS build project, I have LPCTSTR string (i.e.it will be wchar_t*) .The following function is not giving proper result and instead it is producing junk data. I have confirmed it with writing the resultant data in to the file(not printing it on console).The actual requirement of this function was to convert LPCTSTR to char* since I want the resultant string in character buffer. Can anyone please correct me what I have...
1
3241
by: sambarker123 | last post by:
Hi all I am tring to copy a file.The code is as below int _tmain(int argc, _TCHAR* argv) { string s3,s4; s3="C:\\test\\to"; s4="C:\\test\\from\\sam.txt";
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10583
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.