473,394 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

LPTSTR initialization

how do i initialize an LPTSTR variable?

i'm trying to call a microsoft function

LONG QueryStringValue(
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 QueryStringValue, 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.asp


----== 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 25680

"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 QueryStringValue(
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 QueryStringValue, 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.asp


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.QueryStringFunction("yyyyy", 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 QueryStringValue(
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 QueryStringValue, 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.asp


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.QueryStringValue("SomeValue", szValue, &nChars);

On the heap:
LPTSTR szValue = new TCHAR[50];
ULONG nChars = 50;
key.QueryStringValue("SomeValue", 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 QueryStringValue(
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 QueryStringValue, 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.asp

typedef const char* LPCTSTR;

typedef char* LPTSTR;

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

char value[] = "BlahBlah";

unsigned long poo = 0;

QueryStringValue(value_name,value,&poo);

Or even:

QueryStringValue("Hello","Goodbye",&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 QueryStringValue(
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 QueryStringValue, 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.asp

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;

QueryStringValue(value_name,value,&poo);
Wrong, the third parameter of QueryStringValue 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:

QueryStringValue("Hello","Goodbye",&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 QueryStringValue(
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 QueryStringValue, 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.asp


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*******@hotmail.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 QueryStringValue(
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 QueryStringValue, 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.asp
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.QueryStringValue("SomeValue", szValue, &nChars);

On the heap:
LPTSTR szValue = new TCHAR[50];
ULONG nChars = 50;
key.QueryStringValue("SomeValue", 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
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...
3
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); ...
1
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...
3
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....
1
by: Lewap | last post by:
Hi! I've piece of code like follow: <code> LPTSTR lpszPipename = (LPTSTR) "\\\\.\\pipe\\testpipe"; hPipe = CreateNamedPipe(
8
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...
4
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
by: Abhishek | last post by:
how to do convert a std::string to LPTSTR (chat *) datatype regards Abhishek
6
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.