|
One other thing...are you deriving classes from Length? If not, you're probably better off using static polymorphism (like I showed in my earlier e-mail) than dynamic polymorphism (virtual methods) if only because it could be a tad more efficient.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks for the advice... I never knew there was an efficiency difference between virtual vs. static casting methods.
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
I've got a code segment below that converts the block of chars read in from an RSS XML file and converts it to the unicode equivalent using a code page (the code page ID is determined beforehand by scanning for the the encoding ID in the XML).
Sometimes it works, sometimes it crashes the application. I'm using the buffer requirement returned by 'MultiByteToWideChar' to allocate the buffer required (+1 because the block of chars I'm passing does not include the 0 terminator). What's really odd is that on small strings it works fine, but on a 216 character string it crashes. If I extend the buffer allocation by 1 then it works all the time. I should not have to kludge buffer allocation to make things work.
int nSizeReq = MultiByteToWideChar(m_nCPID,0,(const char* m_pChars,nBlockLength,0,0);
TCHAR* pszConverted = new TCHAR[nSizeReq+1];
_tcsnset(pszConverted,0,nSizeReq+1);
MultiByteToWideChar(m_nCPID, 0, (const char*)m_pChars,nBlockLength, pszConverted, nSizeReq);
//up to here it always works, but the next step crashes because pszConverted has damage
//past its memory allocation
CString strConverted = pszConverted
Any ideas why this could be happening?
ps. it runs just great in debug, and if I run it in release mode whilst in visual studio it also works. Run the release by itself and *boom*
[edit]
Coded in C++ (MFC application) using Visual Studio 2008
Unicode is defined
Testing on Vista.
modified on Friday, March 13, 2009 10:51 AM
|
|
|
|
|
First thing I notice is the code you posted does not check the return value of MultiByteToWideChar. Therefore your next operations are running on blind faith. This is not usually considered a Software Development Best Practice.
|
|
|
|
|
Yep, you make a good point, I updated the code to check the value:
int nSizeReq = MultiByteToWideChar(m_nCPID,0,(const char* m_pChars,nBlockLength,0,0);
TCHAR* pszConverted = new TCHAR[nSizeReq+1];
_tcsnset(pszConverted,0,nSizeReq+1);
int nConverted = MultiByteToWideChar(m_nCPID, 0, (const char*)m_pChars,nBlockLength, pszConverted, nSizeReq);
int nTest = wcslen(pszConverted);
the results: nConverted = 214. nSizeReq = 214, but nTest = 206. Weird.
|
|
|
|
|
RichardBrock wrote: Weird.
What are you compiling to?
Try
int nTest = _tcslen(pszConverted);
|
|
|
|
|
The project outputs a 32 bit Windows executable (target platforms are XP and Vista), MFC linked is static.
I tried _tcslen as you suggested, same result. I'm testing from a live RSS feed, so the news item length has changed but here's the latest output from my outputdebugstring placed just after the 2nd MultiByteToWideChar call.
'return value = 140 (wcslen is 308) nSizeReq = 140 nBlockLength = 140'
so you can see the function call returns 140, the buffer allocated was 140 and the block length read from the file is 140. But the converted string is 308 in length, obviously overruning memory allocated to it.
Do you think compiler optimization could be causing a problem? I'm compiling with 'Enable link-time code generation (/GL)'.
Btw, a previous call for the preceding news item yields:
'return value = 121 (wcslen is 121) nSizeReq = 121 nBlockLength = 121'
|
|
|
|
|
You did not answer my question, I guess I didn't make it clear but I assumed you had some knowledge about what you were doing.
led mike wrote: What are you compiling to?
Since the subject of this discussion is character sets that's what that question is about. Are you compiling to _UNICODE or what?
You should check out the example of MultiByteToWideChar in this article[^]
Try to look for the differences in your code, there an obvious difference. Next I strongly urge you to study this subject thoroughly before you attempt your implementation. My experience is that working with conversion requires a sound understanding of this subject. I believe there are great articles here on Code Project that cover this topic well.
|
|
|
|
|
How are you calculating this nBlockLength?
prvn
|
|
|
|
|
I have a file open using CreateFile, I use ReadFile to locate the start and end tags in the XML file for the field, e.g. <description>.....</description> (Internet RSS news feed in Arabic). The nBlockLength indicates the number of characters extracted between > and <, the m_pChars buffer holds the actual character data.
|
|
|
|
|
Hi,
I want to know how can i get the font of system and modify it with an other and apply it again.
the problem that the control is related to system font (ActiveX)specialy a WebBrowser.
So, how can you help me please ?
|
|
|
|
|
Try something like:
LOGFONT logFont;
my_control.GetFont()->GetLogFont(&logFont);
_tcscpy(logFont.lfFaceName, _T("the_name_of_my_favourite_font"));
my_controls_font.CreateFontIndirect(&logFont);
my_control.SetFont(&my_controls_font);
Of course, add nice error checks and such as you like.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
Ok, but can you tell me again please what i should put in the variable my_control ? such as you know that the control is an webbrowser (activeX)?
|
|
|
|
|
So you mean, with the question "how to set the font of my control" you actually wanted to ask "how to use the ActiveX web browser control"? Actually i have no idea, did you place the ActivX thing on your GUI using VS's resource editor? If so, you probably can add a member for it with the same.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
Yes , i had assign an ID to this control and i understood how i put the name of my_control
but the problem exist when i do the line below :
CWindow wnd = GetDlgItem(IDC_EXPLORER);
wnd.GetFont()->GetLogFont(&logFont);
it say that there is a Error ---> 'GetLogFont' : is not a member of 'HFONT_'
so can you help me about this and should i make a casting and how?
Thank you
|
|
|
|
|
Ah...CWindow...that calls for a change of plans...try this instead:
LOGFONT logFont;
GetObject(wnd.GetFont(), sizeof(LOGFONT), &logFont);
...change the logfont as needed...
HFONT NewFont = CreateFontIndirect(&logFont);
wnd.SetFont(NewFont, TRUE);
Don't forget to DeleteObject NewFont once you are not using it anymore. Does this work?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
Sorry Code-o-mat but the problem exist always and that make me crazy
|
|
|
|
|
Ah, you allowed yourself to be fooled into thinking the question was about fonts when actually it was about working with a Web Browser control. Now what?
|
|
|
|
|
No, i had trying much time with this but the problem is still until this control is related to OS.So for that reason i would know how can i modify the font of system in my source code.
|
|
|
|
|
I let shame eat up my soul, get addicted to antidepressants and jump from the north bridge into the datastreams.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
So, there is no idea !!!!
thanks for all
|
|
|
|
|
So this[^] failed too?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
yes, it has always the font of system
|
|
|
|
|
I am trying to use a static library that, although it is meant to be for VC8 produces the following error when build the suppliers sample application:
Linking...<br />
fatal error C1900: Il mismatch between 'P1' version '20060201' and 'P2' version '20050411'<br />
LINK : fatal error LNK1257: code generation failed
I don't have the source for this library, any suggestions?
|
|
|
|
|
Looks like it can be related to Service Packs or even Visual Studio Installation problem that might require a re-installation.
see here[^]
|
|
|
|