|
See here.
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
Hi Friends,
what is the best way to convert a CString to char* , using GetBuffer or using
operator LPCTSTR ??
Appu..
"If you judge people, you have no time to love them."
|
|
|
|
|
Use GetBuffer (). Avoid Typecasting when you need to change or modify the value in the string.
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
-- modified at 2:55 Thursday 1st June, 2006
|
|
|
|
|
I dont think that there is problem in type casting.
It is the operator overloaded in CString.
SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"
|
|
|
|
|
You don't need an explicit cast to use CString 's operator LPCTSTR . Here's an example:
CString s("Hello world!");
const char *pString = s;
You don't need a static_cast<LPCTSTR>(s) or a (LPCTSTR)s . Also note that this isn't a cast in the normal sense; it's a user defined conversion operator.
Don't use GetBuffer unless you need to make modifications.
In short here are the rules:
- If you need a const pointer do nothing: the compiler will work out what to do.
- If you need non-const pointer use GetBuffer .
Steve
|
|
|
|
|
LPCTSTR
for get buffer, u may have to call release buffer.
if it is doing in a large source code base, and if miss a release buffer it will be hard to find and fix it.
u can modify a CString content by calling many other services provided by the same class.
SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"
|
|
|
|
|
u r right Sarath,
But the problem is GetBuffer takes the parameter for length ,
So i need to call GetLength and test whether the length is greater than 0,
So, this is a big process.
if i use LPCTSTR,
it is returning const char *(void) but i when i require const char*, this is not going to help me.
Is there any alternate to these two???
Appu..
"If you judge people, you have no time to love them."
|
|
|
|
|
Sorry for the late reply my connection got disconnected
it is possible to do in this way
its working for me.. but I donno whether it is the right way or not
CString strText("test");<br />
LPSTR lpStr = (LPSTR)(LPCSTR) strText;
SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"
|
|
|
|
|
Depends on what you want to do with the string.
If you want to modify it, use GetBuffer or GetBufferSetLength , which returns an LPTSTR (not const). Just remember to ReleaseBuffer .
If you're not modifying the string, you can use operator LPCTSTR , since it's const.
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
if you want to convert to char* , use (LPSTR)
if you want to convert to TCHAR* , use (LPTSTR)
if you want to convert to const char* , use (LPCSTR)
if you want to convert to const TCHAR* , use (LPCTSTR)
use GetBuffer() if you want to perform some modifications on the string...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
U mean type casting the CString ??
CString strTest;
To convert this to const char* ,can i use (LPCSTR)strTest ??
Appu..
"If you judge people, you have no time to love them."
|
|
|
|
|
|
NiceNaidu wrote: CString strTest;
To convert this to const char* ,can i use (LPCSTR)strTest ??
const TCHAR *pStr = strTest; .
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
NiceNaiduTo convert this to const char* ,can i use (LPCSTR)strTest ??
Yes, but the cast is unnecessary. Others have explained this.
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
neither of those two methos r correct, because both of them return LPTSTR OR LPTSTR, but they r different to char *, when UNICODE is defined beforehand, then LPTSTR equiles w_char *. so I think u should judge if the UNICODE is defined to decide how to convert to char *
life is like a box of chocolate,you never know what you r going to get.
|
|
|
|
|
Hai,
I am to use a .h file in .c file. In the .h file i have to give a definition for a c++ class. I have to use that .h file in the .c file not .cpp file. how can i use that. To say in simple words, i have to use c++ code in a c code( that too in linux) Can any one help me???
Any help appreciated
Saravana Kumar
|
|
|
|
|
C does not support Class concept,
So replace all the Classes with structures and try.
Appu..
"If you judge people, you have no time to love them."
|
|
|
|
|
|
whhich compiler u r using ?
if u r using a C compiler make the class definition in
#ifdef __cplusplus
// class Definition
#endif
SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"
|
|
|
|
|
Ya Thanks Sarath. It works i am to use cc compiler.
Saravana Kumar
|
|
|
|
|
An article I found at OldNewThings
Beware the C++ Implicit Conversion[^]
SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"
|
|
|
|
|
CMenu* menu = GetSubMenu(2);
for(int i = 0; i < menu->GetMenuItemCount(); i++)
{
EnableMenuItem(i, MF_BYPOSITION | MF_GRAYED);
}
DrawMenuBar();
I used this code to disable my second menu.But the error comes like..
'GetSubMenu' : function does not take 1 parameters
'EnableMenuItem' : function does not take 2 parameters
I think the parameters are correct,then why its coming like this.
-- modified at 2:13 Thursday 1st June, 2006
I changed the code as this..
CMenu* pMenu = AfxGetMainWnd()->GetMenu();
CMenu* Menu = pMenu->GetSubMenu(2);
for(int i = 0; i < Menu->GetMenuItemCount(); i++)
{
EnableMenuItem(i, MF_BYPOSITION | MF_GRAYED);
}
DrawMenuBar();
This time the error is come only EnableMenuItem()
|
|
|
|
|
CMenu m_Menu;
m_Menu.GetSubMenu(position);
------------------
GetSubMenu(hMenu,Postion);
------------------
CMenu m_Menu;
m_Menu.EnableMenuItem(i, MF_BYPOSITION | MF_GRAYED);
------------------
EnableMenuItem(hMenu,i, MF_BYPOSITION | MF_GRAYED);
whitesky
|
|
|
|
|
I could not understand what is hMenu..Is that handle for Menu.
|
|
|
|
|
HMENU is handle to point a menu.
it is used in Win32.
in MFC we are using CMenu instead.
SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"
|
|
|
|