473,573 Members | 4,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LPTSTR initialization

how do i initialize an LPTSTR variable?

i'm trying to call a microsoft function

LONG QueryStringValu e(
LPCTSTR pszValueName,
LPTSTR pszValue,
ULONG* pnChars
) throw( );pszValue is supposed to get the value i want but i need to
initialize it before i pass it to QueryStringValu e, how do i initialize
it?the function (member of CRegKey) is described
here:http://msdn.microsoft.com/library/de...y/en-us/vclib/
html/_atl_cregkey.as p


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 22 '05 #1
9 25775

"alex" <al************ *@verizon.net> wrote in message
news:40******** **@127.0.0.1...
how do i initialize an LPTSTR variable?

i'm trying to call a microsoft function

LONG QueryStringValu e(
LPCTSTR pszValueName,
LPTSTR pszValue,
ULONG* pnChars
) throw( );pszValue is supposed to get the value i want but i need to
initialize it before i pass it to QueryStringValu e, how do i initialize
it?the function (member of CRegKey) is described
here:http://msdn.microsoft.com/library/de...y/en-us/vclib/ html/_atl_cregkey.as p


You need to allocate some memory, you should specify how much memory you
allocate in nChars.

E.g. suppose you decide to allocate 1000 characters

LPTSTR pszValue = new TCHAR[1000];
nChars = 1000;
xxxx.QueryStrin gFunction("yyyy y", pszValue, &nChars);
// do something with pszValue
delete[] pszValue; // free the allocated memory

john
Jul 22 '05 #2
"alex" <al************ *@verizon.net> wrote in message
news:40******** **@127.0.0.1...
how do i initialize an LPTSTR variable?

i'm trying to call a microsoft function

LONG QueryStringValu e(
LPCTSTR pszValueName,
LPTSTR pszValue,
ULONG* pnChars
) throw( );pszValue is supposed to get the value i want but i need to
initialize it before i pass it to QueryStringValu e, how do i initialize
it?the function (member of CRegKey) is described
here:http://msdn.microsoft.com/library/de...y/en-us/vclib/
html/_atl_cregkey.as p


This newsgroup is about standard C++ only, so Microsoft's APIs are
off-topic. However I think the question itself is enough C++ related to be
answered here.

What you need to realize is what an LPTSTR is. It's a typedef. An LPTSTR
actually is a TCHAR*, which depending on whether UNICODE is defined maps to
either char* or wchar_t*.

You need to initialize your LPTSTR to sufficient size for the string you
want to return. You do this in two ways, on the stack or on the heap (with
new):
On the stack:
TCHAR szValue[50];
ULONG nChars = 50;
key.QueryString Value("SomeValu e", szValue, &nChars);

On the heap:
LPTSTR szValue = new TCHAR[50];
ULONG nChars = 50;
key.QueryString Value("SomeValu e", szValue, &nChars);
// do something with szValue
delete[] szValue; // DO NOT FORGET THIS!

--
Unforgiven

Jul 22 '05 #3
alex posted:
how do i initialize an LPTSTR variable?

i'm trying to call a microsoft function

LONG QueryStringValu e(
LPCTSTR pszValueName,
LPTSTR pszValue,
ULONG* pnChars
) throw( );pszValue is supposed to get the value i want but i need to
initialize it before i pass it to QueryStringValu e, how do i initialize
it?the function (member of CRegKey) is described
here:http://msdn.microsoft.com/library/de...brary/en-us/vc
lib/ html/_atl_cregkey.as p

typedef const char* LPCTSTR;

typedef char* LPTSTR;

typedef unsigned long ULONG;
char value_name[] = "BlahBlah";

char value[] = "BlahBlah";

unsigned long poo = 0;

QueryStringValu e(value_name,va lue,&poo);

Or even:

QueryStringValu e("Hello","Good bye",&poo);
This function will alter the second and third arguments, as they're not
declared const.
Why does Microsoft give the intrinsic types new names?

Because the information that you've got and the header files you've got are
intended to be programming language NON-specific. If they used "char*", then
some-one writing in a language called Blerg wouldn't know what's going on.
So they've thought up of new names to be used with every computer
programming language.
-JKop
Jul 22 '05 #4
"JKop" <NU**@NULL.NULL > wrote in message
news:19******** *********@news. indigo.ie...
alex posted:
how do i initialize an LPTSTR variable?

i'm trying to call a microsoft function

LONG QueryStringValu e(
LPCTSTR pszValueName,
LPTSTR pszValue,
ULONG* pnChars
) throw( );pszValue is supposed to get the value i want but i need to
initialize it before i pass it to QueryStringValu e, how do i initialize
it?the function (member of CRegKey) is described
here:http://msdn.microsoft.com/library/de...brary/en-us/vc
lib/ html/_atl_cregkey.as p

typedef const char* LPCTSTR;


Wrong, LPCTSTR will expand to const wchar_t* if UNICODE is defined. Actually
the definition is:
typedef const TCHAR* LPCTSTR;

And TCHAR is:
#ifdef UNICODE
typedef wchar_t TCHAR
#else
typedef char TCHAR
#endif
typedef char* LPTSTR;

typedef unsigned long ULONG;
char value_name[] = "BlahBlah";

char value[] = "BlahBlah";

unsigned long poo = 0;

QueryStringValu e(value_name,va lue,&poo);
Wrong, the third parameter of QueryStringValu e must be the size in TCHARs of
the buffer pointed to by the second parameter. It will receive the amount of
TCHARs written. So it shouldn't be 0 initially.
Or even:

QueryStringValu e("Hello","Good bye",&poo);
This function will alter the second and third arguments, as they're not
declared const.
Why does Microsoft give the intrinsic types new names?

Because the information that you've got and the header files you've got
are
intended to be programming language NON-specific. If they used "char*",
then
some-one writing in a language called Blerg wouldn't know what's going on.
So they've thought up of new names to be used with every computer
programming language.


Not really. The header files are meant for C(++), they use C syntax so it'd
be a little hard to use them from a language that doesn't support C syntax.
Also, this particular function is not part of the Win32 API, but of ATL,
which is meant for C++ exclusively.

The main reason Microsoft renames intrinsic types is to standardise their
usage across the API, and to make them platform independant. With platform I
don't mean Windows vs. Unix, but Win16 vs. Win32 vs. Win64 and in the past
also Windows NT on the Alpha processors. For instance the LPARAM type, which
is used for Windows messaging, should be large enough to hold a pointer. So
on Win32 it'll expand to unsigned int, but on Win64 it's unsigned __int64.
Also, types such as WORD, DWORD are of a fixed length, regardless of what
word length your system has (which is a bit confusing, since it means DWORD
on Win32 is only a single machine word long (32 bits)). Then there are the
main pointer arithmic types such as INT_PTR which are guaranteed to have the
same length as a pointer.

In this situation we see another use of the typedefs. All string related
types with a T in them (TCHAR, LPTSTR, LPCTSTR) expand to either ansi or
wide characters depending on whether UNICODE is defined. In conjunction with
the _T macro you can use those types to write a program that can compile for
ansi and unicode without modification.

--
Unforgiven

Jul 22 '05 #5
JKop wrote:
<snip>

typedef const char* LPCTSTR;

typedef char* LPTSTR;

typedef unsigned long ULONG;
On some systems. Don't rely on it.


This function will alter the second and third arguments, as they're
not declared const.
Not neccessarily. Non-const does not require that the variable be altered;
const just prevents it.


Why does Microsoft give the intrinsic types new names?

Because the information that you've got and the header files you've
got are intended to be programming language NON-specific. If they
used "char*", then some-one writing in a language called Blerg
wouldn't know what's going on. So they've thought up of new names to
be used with every computer programming language.


Plus it's more portable. If, for example, you use TCHAR correctly you can
compile for both WinCE and `95 with the same code.

- Pete
Jul 22 '05 #6
Pete C. posted:
This function will alter the second and third arguments, as they're
not declared const.


Not neccessarily. Non-const does not require that the variable be
altered; const just prevents it.

You are correct, although if the function does not alter the variables, it's
stupid for not declaring them const.
-JKop
Jul 22 '05 #7

"JKop" <NU**@NULL.NULL > wrote in message
news:ow******** *********@news. indigo.ie...
Pete C. posted:
This function will alter the second and third arguments, as they're
not declared const.
Not neccessarily. Non-const does not require that the variable be
altered; const just prevents it.

You are correct, although if the function does not alter the variables,

it's stupid for not declaring them const.


But the function does alter the variable, RTFM.

john
Jul 22 '05 #8
"alex" <al************ *@verizon.net> wrote in message news:<40******* ***@127.0.0.1>. ..
how do i initialize an LPTSTR variable?

i'm trying to call a microsoft function

LONG QueryStringValu e(
LPCTSTR pszValueName,
LPTSTR pszValue,
ULONG* pnChars
) throw( );pszValue is supposed to get the value i want but i need to
initialize it before i pass it to QueryStringValu e, how do i initialize
it?the function (member of CRegKey) is described
here:http://msdn.microsoft.com/library/de...y/en-us/vclib/
html/_atl_cregkey.as p


Ok, you are kind of out of scope for topic here. This is a library
fcn in a vendor specific librry. And LPTSTR is not a standard type.
So, you should really be asking in one of the Microsoft or Windows
related groups.

But just guessing... It *looks* like a pointer. The psz in front of
it seems to say it's a pointer to a string terminated by a null.
So, presumably you need to point it at such before the call.
Presumably your docs will tell you what.
Socks
Jul 22 '05 #9

"Unforgiven " <ja*******@hotm ail.com> wrote in message
news:2j******** *****@uni-berlin.de...
"alex" <al************ *@verizon.net> wrote in message
news:40******** **@127.0.0.1...
how do i initialize an LPTSTR variable?

i'm trying to call a microsoft function

LONG QueryStringValu e(
LPCTSTR pszValueName,
LPTSTR pszValue,
ULONG* pnChars
) throw( );pszValue is supposed to get the value i want but i need to
initialize it before i pass it to QueryStringValu e, how do i initialize
it?the function (member of CRegKey) is described
here:http://msdn.microsoft.com/library/de...y/en-us/vclib/ html/_atl_cregkey.as p
This newsgroup is about standard C++ only, so Microsoft's APIs are
off-topic. However I think the question itself is enough C++ related to be
answered here.

What you need to realize is what an LPTSTR is. It's a typedef. An LPTSTR
actually is a TCHAR*, which depending on whether UNICODE is defined maps

to either char* or wchar_t*.

You need to initialize your LPTSTR to sufficient size for the string you
want to return. You do this in two ways, on the stack or on the heap (with
new):
On the stack:
TCHAR szValue[50];
ULONG nChars = 50;
key.QueryString Value("SomeValu e", szValue, &nChars);

On the heap:
LPTSTR szValue = new TCHAR[50];
ULONG nChars = 50;
key.QueryString Value("SomeValu e", szValue, &nChars);
// do something with szValue
delete[] szValue; // DO NOT FORGET THIS!

--
Unforgiven


this provides the proper initialization i needed, thanks, thanks to everyone
else who participated as well!

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 22 '05 #10

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

Similar topics

4
10176
by: sorty | last post by:
Hi, I have read in many places that TCHAR can be 'char' or 'wchar_t' depending on ANSI or UNICODE. I have also read that LPTSTR is a long pointer to a TCHAR. I am confused about the following. Please answer with yes/no and then elaborate as you please..
3
12574
by: free2cric | last post by:
Hi , is this correct to have final LPTSTR to char * CString fname(argv ); LPTSTR p1 = fname.GetBuffer(fname.GetLength()); char teststr; sprintf(teststr,"%s",p1); //or strcpy(teststr,p1); is this code to have my null terminated teststr.
1
5973
by: Steven Blair | last post by:
Hi there. Having a problem calling a win32 function from a dll. Here is the function header: DWORD thrdGetHcNum (DWORD nPort, DWORD nBaudrate, LPTSTR lpszHcNum) //Description of the 3rd parameter LpszHcNum: The head of the buffer area used to store the serial number of the terminal, 9 bytes at least.
3
4363
by: Mark | last post by:
Help please... i am VERY new to the managed C++ and i need to take a System::String* and cast it to an LPTSTR how might i do this? I have searched the web and groups but cannot find any examples. -thanks
1
2609
by: Lewap | last post by:
Hi! I've piece of code like follow: <code> LPTSTR lpszPipename = (LPTSTR) "\\\\.\\pipe\\testpipe"; hPipe = CreateNamedPipe(
8
5748
by: **Developer** | last post by:
Been reading some of the Marshal doc and am a little confused. Anyway, I've been using the following and it works OK. Public Declare Auto Function SHGetFolderPath Lib "shell32.dll" (ByVal hWnd As Integer, _ ByVal nFolder As Integer, ByVal nToken As Integer, _ ByVal dwFlags As Integer, _
4
1307
by: --== Alain ==-- | last post by:
Hi, How can i use LPTSTR in VC++.NET application ? the same question for HINSTANCE, and HANDLE types. Should i use a MarshalAs and to which type ? thanks, Al.
2
12152
by: Abhishek | last post by:
how to do convert a std::string to LPTSTR (chat *) datatype regards Abhishek
6
14311
by: pskumaran | last post by:
Hi All, I am trying to convert LPTSTR to char* by using sprintf, But sprintf copies only the first character from the string. i tried both array and pointer also , but the result is same. Here is my code... LPTSTR TestStr= _T("Test String"); char s1; char *s2; s2 = (char *) malloc(1000*sizeof(char)); sprintf(s1,"%s",TestStr);
0
7707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8037
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. ...
0
8215
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...
0
6432
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...
1
5605
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5296
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...
1
2227
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
1
1325
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1048
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.