Sign in to follow this  
crazy_andy

CString.format

Recommended Posts

previously with visual studio 2003 the following code worked great CString str,start; // assighn "C" to start here str.Format("%s:",start); this gave str tthe value "C:" Now with visual studio 2005 beta 2, upon trying to compile I get an error "cannot convert parameter 1 from 'const char [7]' to 'const wchar_t *'" so I tried CString str,start; // assighn "C" to start here str.Format((wchar_t *)"%s:",start); this compiles,, however str has the value of an unreadable thing. I see three squares. Its the same with MessageBox, except the const char[7] wants to be a LPCTSTR but all I see is squares. Anyone know what I'm doing wrong

Share this post


Link to post
Share on other sites
Try encapsulating your strings with the _T() macro, this converts the string to unicode or leaves it as ansi depending on how the project is set to compile.

If you put this it should work fine:
str.Format (_T("%s:"), start);

Share this post


Link to post
Share on other sites
Glad I could help :)

Search MSDN for WideCharToMultiByte and MultiByteToWideChar.

However, I would suggest that you use TCHAR's instead of chars. Have a look at the tchar.h file, it contains loads of string manipulation functions that work for both UNICODE and ANSI strings. And since you're using MFC, tchar.h is already included.

Hope this helps

Share this post


Link to post
Share on other sites
ok, I'm having problems with this sttement, and rather than start a new thread, I thought I would post it here.

this is the code I wrote in visual studio 2005 beta 2.

char pathInfo[MAX_DIRECTORY_LENGTH];
DWORD ResultCode = GetCurrentDirectory(MAX_DIRECTORY_LENGTH,(LPWSTR)pathInfo);


this is the code I am using to learn this stuff, I opened it in vs 2005 and converted it.

char szPath[MAX_DIR_LENGTH];
DWORD ddwRes = GetCurrentDirectory(MAX_DIR_LENGTH, szPath);


in the 2nd piece of code szPath contains "c:\docum...
in my code pathInfo contains "c", now looking at pahinfo, I see it is actuall "c:\docu.... but with a 0 (NULL) between every character.

I put the (LPWSTR) in to make it compile. without it, it can't convert from char[1024] to LPWSTR, however the 2nd code compiles without problem.

Anyone know why my code doesn't compile, but the other one does. also how can I make it compile and return the result I want?

Share this post


Link to post
Share on other sites
Like I said, TCHAR will solve all your problems! :) Well... nearly:

The whole point of the tchar.h file is to add some degree of compatibilty between ANSI and UNICODE. In the tchar.h header there will be something along the lines of:

#ifdef UNICODE
// This may be unsigned short, depending on how the compiler has been cofigured
#define TCHAR wchar_t
#else
#define TCHAR char
#endif

Therefore if you always use TCHAR instead of char then, 9 times out of 10, everything should work fine. Typecasting between UNICODE and ANSI strings will very rarely work correctly because ANSI has lots of character sets whereas UNICODE just has the one. So different types are used to handle the two strings, char is 8 bits in size and wchar_t and unsigned short are 16 bits if I remember correctly.

The reason that you're getting a null after each letter is because of this size difference. For example:

char szHello[] = _T("Hello"); // equals 0x48 0x65 0x6C 0x6C 0x6F
wchar_t wszHello[] = _T("Hello"); // equals 0x0048 0x0065 0x006C 0x006C 0x006F

As you can see, there is a 0x00 (i.e. null) either side of each character.

Try putting this:

TCHAR pathInfo[MAX_DIRECTORY_LENGTH];
DWORD ResultCode = GetCurrentDirectory(MAX_DIRECTORY_LENGTH, pathInfo);


[Edited by - DuFaceTheBass on June 4, 2005 12:10:40 PM]

Share this post


Link to post
Share on other sites
cheers, I'm lookking into TCHAR, but have hit another problem.

// It may not be LPTSTR but something similar (I don't have the code on me right now)
LPTSTR data = new TCHAR[1024];
data = globals::DLDiirectory.GetBuffer(1024);

globals::DLDiirectory contains "C:\documents And Settings"
after this data = "C:\Documents A"

I also tried _tcscpy(data,globals::DLDiirectory);
but got the same prob.

globals::DLDiirectory.GetLength(); returns 14 its really odd. Also the value of globals::DLDiirectory doesn't seem to affect it. It can be a string 100 characters long, and it may return the same as before or 50 characcters.

Share this post


Link to post
Share on other sites
I need to take the value stored in globals::dldirectory (which is a cstring)
and write it to the registr, using RegSetKeyEx because of the unicode + ansi size differance, the registry has a NULL betweeen each value in REG_SZ.

This data convertion is really confusing me.

Share this post


Link to post
Share on other sites
heres the problem part.

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\Apocalypse Systems\\General"),0,KEY_SET_VALUE,&globals::hk))
{
LPVOID msg;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPWSTR)&msg,0,NULL);
MessageBox(NULL,(LPCTSTR)msg,_T("Error"),MB_OK|MB_ICONERROR);
}


int j = globals::DLDirectory.GetLength();
LPTSTR data = new TCHAR[1024];
data = globals::DLDirectory.GetBuffer(1024); // DLDirectory = "c:\documents and settings\ ..." data = "c:\documents a"
globals::DLDirectory.ReleaseBuffer();

if (RegSetValueEx(globals::hk,_T("Download Folder"),0,REG_SZ,(LPBYTE)data,count))
{
LPVOID msg;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPWSTR)&msg,0,NULL);
MessageBox(NULL,(LPCTSTR)msg,_T("Error"),MB_OK|MB_ICONERROR);
}
}



global.h

#include <atlstr.h>
#if !defined GLOBAL_H
#define GLOBAL_H

class globals
{
public:
static char *URL[1];
static int current;
static bool DLRequested[1];

static bool FolderChanged;
static CString Folder;

static CString DLDirectory;
static CString TempDirectory;
static CString EDFIDirectory;

static CString NewFolder;
static bool IsNewFolder;

static DWORD AutoCreate;
static DWORD Alarm;
static DWORD balloon;
static DWORD ExitComplete;
static DWORD Minimize;

static HKEY hk;
};

#endif



wjen I hover over DLDorectory in debug mode, it tells me the value is "c:\documents and setting\..."
(... is me shorterning it)

Share this post


Link to post
Share on other sites
Ok, I think I may have a bodge that may work but I'm not sure. The only thing I can see that would cause your problem is that size difference between char and wchar_t. BYTE is actually char, and I doubt the registry functions will allow for this difference.

By the way I wouldn't allocate a new buffer for the CString, the class does it for you. Also there is no need for the ReleaseBuffer() call, thats only required if you modify the buffer.


int nCount = globals::DLDirectory.GetLength();
LPTSTR lpszBuffer = globals::DLDirectory.GetBuffer(nCount);

if (RegSetValueEx(globals::hk,_T("Download Folder"),0,REG_SZ,(LPBYTE)lpszBuffer,nCount * sizeof(TCHAR)))
{
LPTSTR lpszMsg;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),lpszMsg,0,NULL);
MessageBox(NULL,lpszMsg,_T("Error"),MB_OK|MB_ICONERROR);
}




I haven't tried this code out so I'm not sure if it will work but its worth a try eh? lol The reason I've added the sizeof(TCHAR) bit is to get the actual size of the string and not the length.

Hope this works :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this