|
Never use char and wchar with CString class. Always use TCHAR based types.
For eg: LPCTSTR, LPTSTR, TCHAR . and of course TCHAR based functions like _tcscpy, _tcscmp, _tcscmpi, _tcsstr .
You will always encounter such problems if you switch between UNICODE and non UNICODE builds.
TCHAR makes switching easy. So always use TCHAR based types with CString .
if _UNICODE is defined then TCHAR expands to wchar_t else it expands to char .
|
|
|
|
|
Thanks a lot for all your informative replies...
Well ill now change it to TCHAR. I guess this should solve the problem. If not then ill ask up again...
cheers
|
|
|
|
|
Hi
well as you all said to use TCHAR. i tried it just now...and got some prob as shown below
<br />
TCHAR strFilter[1000];<br />
_tcscpy_s(strFilter, "All Files (*.*)|*.*||");<br />
And error i got is none of the 2 overloads could convert all the arguments type.
So to overcome this problem i did as shown below...
<br />
TCHAR strFilter[1000];<br />
const TCHAR *strSource = L"All Files (*.*)|*.*||";<br />
_tcscpy_s(strFilter, strSource);<br />
This solved the error but i guess L is UniCODE...What could i use in place of that.
I guess L is used to helt converting from const char to TCHAR*...pls correct me if i am wrong.
Can i use it in this ...? If not then what could be the solution of the above error..
|
|
|
|
|
Software_Specialist wrote: I guess L is used to helt converting from const char to TCHAR*...pls correct me if i am wrong.
Yeah, I forgot to mention the main stuff...
When declaring strings for use with CString, wrap them in _T( "" )
Here it will be
const TCHAR* strSource = _T( "All files(*.*)|*.*||" );
|
|
|
|
|
ok kewl...
...got it working now...
So now i should always use TCHAR in place of char and for this i just have to include TCHAR.h and no need to define anything. It would work with UNICODE as well as non-UNICODE.
gr8....thanks a lot
|
|
|
|
|
Software_Specialist wrote: Now when i am writing code for UI, i have to use function strlen, strcpy , strcmp...
Why? The CString class provides all you need.
Software_Specialist wrote: So now do i have to change the whole application where i have used char.
Yes, since it is Unicode.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I don't know if anyone else mentioned this, but if you're going to go through all your code
and change it to work on Unicode, then I recommend in addition to changing all the char to TCHAR
also change all the CRT string calls to generics as well: strcpy = _tcscpy, strlen = _tcslen,
etc. instead of changing them to the wide character version.
That way all the code will be generic and you'll be able to reuse any code easier in any project
regardless of the character set settings.
It doesn't take that long to convert - I did a huge set of projects in a couple hours
MArk
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hi all
well i have a lil problem here
I have created few classes and stuff and i have used normal char type variable.
Now when i am writing code for UI, i have to use function strlen, strcpy , strcmp...
For instance if i do...
<br />
char str[10];<br />
CString Password;<br />
strcpy(str, Password);<br />
In above code i get error saying can not convert parameter 2 from 'CString to const char*'
So to overcome this problem i made these changes as shown below...
<br />
wchar_t str[10];<br />
CString Password;<br />
wcscpy(str, Password);<br />
This solved my error but now i have many other errors. where i used char . So now do i have to change the whole application where i have used char. All the functions and stuff..
Is it now compulsory to use unicode in MFC....??? I am new to MFC so new to Unicode thingy..bit confusing though..
Can any one shed some light over this..
Any or all help would be greatly appreciated..
Thanks a lot
|
|
|
|
|
if you define _UNICODE you have to follow unicode. For CString to const char* error you could have used some macro for typecasting. no need to go for WCHAR. There are many other ways to e.g. password.GetBuffer(Password.GetLength()); which will return yoi char *.
|
|
|
|
|
oh i wasn't aware of this.I already started changing stuff and error seems to be never ending. Hmm i got a backup of project though... So i guess i need to start it again.
Well i have not define _UNICODE...and what could be solution you could think of in the problem i mentioned.
Can we still use unicode without defining _UNICODE. I guess we can cos few things are working when i changed to wchar_t and i have not defined _UNICODE anywhere...
Thanks
|
|
|
|
|
It's not compulsory to use Unicode, you can remove the /D UNICODE from the MSVC project settings, but it is generally a good idea to go Unicode these days.
The short answer to whether you are going to have to change all the code or not is YES and the sooner you do it the less painful it will be
One option is to change your strcpy s for tcscpy s etc so that the code will compile with and without UNICODE defined. This way you can do the transition more gradually and in the meanwhile non Unicode builds which mix TCHARs with chars will work, keep putting in the changes to TCHAR functions until the code also builds and works in UNICODE. TCHAR and tcs functions turn into wchar and wcs with UNICODE defined(remeber to define _UINICODE as well beacuse someone at Microsoft screwed up) and they turn back into ordinary char stuff in non Unicode builds. You'll need <tchar.h> included of course.
Welcome to the wonderful world of international software
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Hi,
Always uses TCHAR instead of char / wchat_t and the generic-text functions defined in Tchar.h.
This will maps appropriately if used _UNICODE or not.
|
|
|
|
|
someone would tell me how to check a socketport is open(active) or not?
if it is active I want to close it,otherwise I would skip this work.
GoodLuck!
|
|
|
|
|
Hey,
try this link
http://msdn2.microsoft.com/en-us/library/wxzt95kb(VS.80).aspx
and this
http://www.codeproject.com/internet/SocketFileTransfer.asp
|
|
|
|
|
Did you mean if I want to get such target I would have to using CSocketFile?
But I did no need to using such class.I only want to indicate the socket is active or not,if so,I would close it.
|
|
|
|
|
kcynic wrote: I only want to indicate the socket is active or not,if so,I would close it.
Is there any harm in closing an already closed socket? If so, then maybe you could use getsockname() or getsockopt() , and check for WSAENOTSOCK .
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Oh,I see what did you mean. That is to say,I can close a socket no matter it is opened or not.
Thanks
|
|
|
|
|
kcynic wrote: ...I can close a socket no matter it is opened or not.
I *think* you can. I don't work with sockets so you'll need to test it to be sure.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
There must be some nice simple function i can use to get the full directory location of where i am running my program from. Lets say my program is "me.exe" from "C:\WINDOWS\stuff\me.exe", then i want to get "C:\WINDOWS\stuff" or "C:\WINDOWS\stuff\" .
My search failed so thats why im asking here, thanks
edit: im in visual studio with a MFC dialog based application
edit2: maybe i should add i want to find the full directory from the program itself, so it knows where its running from...
//Johannes
|
|
|
|
|
Do you need to GetModuleFileNameEx ?
|
|
|
|
|
Is there something less hightech and more simple?
Like none of the DWORD and all the other stuff
(i edited my question abit)
//Johannes
|
|
|
|
|
|
Im using the code:
TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL,szPath,MAX_PATH);
AfxMessageBox(szPath);
however the messagebox show the full path and the executable name, i just want the full dir,
thanks
//Johannes
|
|
|
|
|
if i use that in combination with
PathRemoveFileSpec(szPath);
it works
thanks WhiteSky!
//Johannes
|
|
|
|
|
I glad your problem solved.
|
|
|
|