-
December 4th, 2008, 04:01 PM
#1
[RESOLVED] Converting project to UNICODE causes crashes
I used the UNICODE preprocessor directive to support UNICODE, however, doing so causes bizarre crashes.
Is there something obvious that I'm doing wrong? I don't see why using the UNICODE directive causes this problem.
Looking at the stack trace I'm guessing that the problem has to do with this line of code:
Code:
AFX_MANAGE_STATE(AfxGetStaticModuleState())
The crash occurs when I'm exiting a function in my code.
-
December 4th, 2008, 05:14 PM
#2
Re: Converting project to UNICODE causes crashes
Odds are, you have a char array somewhere which you're trying to pass to a function which now wants a wchar_t array (or TCHAR). Normally the compiler would yell at you for this, but if you made the mistake of casting away the problem, it could have come back to bite you.
Unfortunately once you overwrite memory that doesn't belong to you, it's impossible to predict when the program will fail after that, so the problem isn't necessarily where the debugger thinks it is.
-
December 4th, 2008, 05:44 PM
#3
Re: Converting project to UNICODE causes crashes
Originally Posted by Lindley
Odds are, you have a char array somewhere which you're trying to pass to a function which now wants a wchar_t array (or TCHAR). Normally the compiler would yell at you for this, but if you made the mistake of casting away the problem, it could have come back to bite you.
Unfortunately once you overwrite memory that doesn't belong to you, it's impossible to predict when the program will fail after that, so the problem isn't necessarily where the debugger thinks it is.
I wish that were the case. Whoever wrote this program uses "_tstring" eveywhere which when looking at the string header file yields:
Code:
#ifdef _UNICODE
typedef wstring _tstring;
#else
typedef string _tstring;
#endif
-
December 4th, 2008, 07:01 PM
#4
Re: Converting project to UNICODE causes crashes
Normally you have to use both UNICODE and _UNICODE when making a unicode app - perhaps you only defined one of them?
Good luck.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
December 4th, 2008, 07:55 PM
#5
Re: Converting project to UNICODE causes crashes
Originally Posted by krmed
Normally you have to use both UNICODE and _UNICODE when making a unicode app - perhaps you only defined one of them?
Good luck.
Thanks, I tried that already but that doesn't help either. It's such a strange problem because when these preprocessor definitions are not defined everything works perfectly fine. This app has been around for quite a while.
I'm wondering though if there is anything special I need to do since this project is integrated with Outlook and is using MAPI.
Last edited by Rigel; December 4th, 2008 at 07:58 PM.
-
December 5th, 2008, 06:49 AM
#6
Re: Converting project to UNICODE causes crashes
Even though the code was written using _tstring, you need to check if it was used properly. For instance
Code:
_tstring mystring;
mystring = "This is my test";
will work fine in MBCS, but not in unicode, and
Code:
_tstring mystring;
mystring = L"This is my test";
will work in unicode but not MBCS.
This
Code:
_tstring mystring;
mystring = _T("This is my test");
would work either way.
Hope that helps.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
December 5th, 2008, 08:33 AM
#7
Re: Converting project to UNICODE causes crashes
Have you tried wrapping all of your MAPI calls with exception handlers and checking all return values for success? I don't know which MAPI calls you are using but exceptions, if implemented in the target (outlook), can potentially give you the exact reason for failure.
Take a look at this pseudo-code:
Code:
if(SomethingIsWrong)
throw exception(reason)
DoSomethingFatal
return status
This is one typical way that exceptions are generated. Notice that the programmer has assumed that the client of this function will implement exception handling. And if not, then when something is wrong the code just continues on to a fatal error. Also note that if you catch the exception then you can simply display (or output to debugger) the reason for failure.
It is also important to check the return value for each MAPI functions. You will have to check MSDN or help files for details, but many times these functions return ERROR_SUCCESS (0) for success and a non-zero code, which can be translated to a message string, for failure.
How extensive is your debugging? I would guess that in code like this you could locate the exact MAPI call that is causing the problem. The first thing that I would do is implement exception handling and return value checking on that function call and a few around it as a start.
One other note about UNICODE strings. While it is true that you must make sure your strings are UNICODE-correct, MAPI functions, like most other COM calls, use BSTR exclusively and applications always have to convert from LPCTSTR to BSTR before the call. Make sure that your conversion from LPCTSTR to BSTR is correct.
And another thing while I think about it. After everything I wrote above about COM, something is reminding me that when I used MAPI calls to Outlook many years ago, that the interface was not COM but a dll with exported functions and the functions were implemented in ANSI only - not UNICODE. I'll check my code if I can find it, but what functions are you using? The MAPI dll exports?
Last edited by 0xC0000005; December 5th, 2008 at 08:42 AM.
-
December 5th, 2008, 06:31 PM
#8
Re: Converting project to UNICODE causes crashes
FOUND THE PROBLEM!
sizeof operator is the problem and causes a stack corruption.
Incorrect:
Code:
TCHAR szClassName[64];
GetClassName(hwnd, szClassName, sizeof(szClassName)-1);
szClassName[sizeof(szClassName)-1] = 0;
TCHAR uses two bytes of storage instead of one and this is the reason why I'm receiving bizarre crashes.
Correct:
This will work in both Unicode/Non-Unicode versions.
Code:
TCHAR szClassName[64];
GetClassName(hwnd, szClassName, sizeof(szClassName)/sizeof(TCHAR)-1);
szClassName[sizeof(szClassName)/sizeof(TCHAR)-1] = 0;
Here is an article that explains it all.
http://en.wiki.mcneel.com/default.as...dkCountOf.html
-
December 5th, 2008, 07:17 PM
#9
Re: Converting project to UNICODE causes crashes
Lending yet more support to my "never use sizeof to get an array length, ever, for any reason, no matter how much you want to" philosophy.
That situation would be better dealt with by declaring a constant int equal to 64, and using it in both places.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|