Click here to Skip to main content
15,891,136 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionMFC New Feature pack style problem! Pin
m_code23-May-10 22:21
m_code23-May-10 22:21 
QuestionChange Button Image on mouse over Pin
AbhiHcl23-May-10 21:50
AbhiHcl23-May-10 21:50 
AnswerRe: Change Button Image on mouse over Pin
namaskaaram23-May-10 23:35
namaskaaram23-May-10 23:35 
QuestionHow to Convert CString to char array? Pin
TechAvtar23-May-10 19:14
TechAvtar23-May-10 19:14 
AnswerRe: How to Convert CString to char array? Pin
Yusuf23-May-10 19:28
Yusuf23-May-10 19:28 
AnswerRe: How to Convert CString to char array? Pin
MS_TJ23-May-10 19:43
MS_TJ23-May-10 19:43 
AnswerRe: How to Convert CString to char array? Pin
Iain Clarke, Warrior Programmer23-May-10 20:56
Iain Clarke, Warrior Programmer23-May-10 20:56 
AnswerRe: How to Convert CString to char array? Pin
Aescleal24-May-10 4:17
Aescleal24-May-10 4:17 
Hi,

You've got two problems here:

- Converting from potentially wide to narrow charcaters
- Getting rid of any const

The first means that you can't use something like _tcscpy and TCHARs as you want real, honest, chars, not chars or wchar_ts depending on some preprocessor macro. The second means you've got to do a real copy and you can't just use an implicit conversion.

To solve the second problem you've got to do a copy. To solve the first you either copy the string using wcstombs or strcpy depending on whether the CString is encoded as unicode as not. You can either use the preprocessor to select which function you use, but that's really sucky like most uses of the preprocessor in C++ (avoid it like the plague) or you can use function overloading:

void to_narrow( const CStringA &to_convert, char *out )
{
   ...
}

void to_narrow( const CStringW &to_convert, char *out )
{
   ...
}


I haven't bothered including the code as there's another way you can do the conversion which relies more on the C++ standard library and has less pointers flopping about and confusing the issue. In this version I'm returning a std::vector<char> as it sorts out any memory management for the caller:

std::vector<char> to_narrow( const CStringA &to_convert )
{
    const char *convert_start = to_convert;
    const char *convert_end   = convert_start + to_convert.GetLength() + 1;

    return std::vector<char>( convert_start, convert_end );
}

std::vector<char> to_narrow( const CStringW &to_convert )
{
    const wchar_t *convert_start = to_convert;

    std::vector<char> output( to_convert.GetLength() * 4 + 1 );
    output.resize( wcstombs( &output[ 0 ], to_convert, output.size() ) );
    return output;
}


The supperfluous +1s everywhere are due to CString::GetLength not including the terminating '\0' character in the length. The *4 is just in case Microsoft ever support utf-8 as a character set - it gives the oodles of space for really weird characters

Any questions just shout.

Cheers,

Ash

PS: This code was written from memory and isn't tested, there might be some problems but the idea's sound and in use all over the place.
QuestionSplitter Window for MDI. Pin
janaswamy uday23-May-10 16:04
janaswamy uday23-May-10 16:04 
QuestionHow to include a dll and a lib files into a VS project Pin
Schehaider_Aymen23-May-10 11:18
Schehaider_Aymen23-May-10 11:18 
AnswerRe: How to include a dll and a lib files into a VS project Pin
Garth J Lancaster23-May-10 12:36
professionalGarth J Lancaster23-May-10 12:36 
GeneralRe: How to include a dll and a lib files into a VS project [modified] Pin
Schehaider_Aymen23-May-10 12:43
Schehaider_Aymen23-May-10 12:43 
GeneralRe: How to include a dll and a lib files into a VS project Pin
Garth J Lancaster23-May-10 12:56
professionalGarth J Lancaster23-May-10 12:56 
GeneralRe: How to include a dll and a lib files into a VS project Pin
Schehaider_Aymen23-May-10 12:57
Schehaider_Aymen23-May-10 12:57 
QuestionStarting a password protected NT Service [modified] Pin
pubis23-May-10 9:13
pubis23-May-10 9:13 
AnswerRe: Starting a password protected NT Service Pin
Aescleal23-May-10 9:59
Aescleal23-May-10 9:59 
GeneralRe: Starting a password protected NT Service Pin
pubis23-May-10 10:15
pubis23-May-10 10:15 
GeneralRe: Starting a password protected NT Service Pin
Aescleal23-May-10 10:24
Aescleal23-May-10 10:24 
GeneralRe: Starting a password protected NT Service Pin
pubis23-May-10 10:56
pubis23-May-10 10:56 
QuestionWM_QUERYENDSESSION problem on C++ Builder 2010 Pin
Member 88968923-May-10 7:06
Member 88968923-May-10 7:06 
AnswerRe: WM_QUERYENDSESSION problem on C++ Builder 2010 Pin
ARopo24-May-10 6:26
ARopo24-May-10 6:26 
GeneralRe: WM_QUERYENDSESSION problem on C++ Builder 2010 Pin
Member 88968924-May-10 13:36
Member 88968924-May-10 13:36 
QuestionC++ String Creation Pin
sikas_Cisco23-May-10 4:57
sikas_Cisco23-May-10 4:57 
AnswerRe: C++ String Creation Pin
Dr.Walt Fair, PE23-May-10 5:18
professionalDr.Walt Fair, PE23-May-10 5:18 
AnswerRe: C++ String Creation Pin
John R. Shaw23-May-10 7:21
John R. Shaw23-May-10 7:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.