Building a Unicode application.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am in the process of converting an application to Unicode that is built
with Visual C++ .NET 2003. On application startup in debug mode I get an
exception. The problem appears to be that code with #ifndef _UNICDODE is
executed in output.c, the library code for supporting printf functions. I
need to how to get the code that is defined with _UNICODE to be executed
instead. I have defined the _UNICODE constant in the project Properties and
specified wWinMainCRTStartup as the Entry Point in the linker properties.

Thanks in advance,
Craig
 
Craig Johnston said:
I am in the process of converting an application to Unicode that is built
with Visual C++ .NET 2003.

You can go to your project Properties (right click on the project name, and
select "Properties").
Then, in "General" group, go to "Character Set" and choose the option "Use
Unicode Character Set".
instead. I have defined the _UNICODE constant in the project Properties
and

You should define both _UNICODE and UNICODE (however, this is done by the
IDE if you change the configuration as shown above).

If you move to Unicode, you should pay attention in your code, e.g. in
places where you used 'char', you should use TCHAR (than is OK for both ANSI
and Unicode), or wchar_t (for Unicode-only).

Or if you have some string buffers and you have to specify the buffer
length, this length is usually in TCHARs, which is the same of length in
bytes only for ANSI, but *not* for Unicode (for Unicode, you must divide the
total length in bytes by the bytes in a TCHAR, i.e. 2).

So considering the following code:

// Assume DoSomething has a prototype like so:
//
// void DoSomething( LPTSTR bufferPtr, size_t bufferCch );
//
char buffer[200];
DoSomething( buffer, sizeof(buffer) );

If you port to Unicode, you should use:

TCHAR buffer[200];
DoSomething( buffer, sizeof(buffer) / sizeof(buffer[0]) );

(There must be something like _countof() to do the
sizeof(array)/sizeof(array[0]) calculation.)

Giovanni
 
I am in the process of converting an application to Unicode that is built
with Visual C++ .NET 2003. On application startup in debug mode I get an
exception. The problem appears to be that code with #ifndef _UNICDODE is
executed in output.c, the library code for supporting printf functions. I
need to how to get the code that is defined with _UNICODE to be executed
instead. I have defined the _UNICODE constant in the project Properties and
specified wWinMainCRTStartup as the Entry Point in the linker properties.

1. Change the project properties (see Giovanni's post)
2. Take a look at this
http://msdn2.microsoft.com/en-us/library/c426s321(VS.80).aspx
then here:
http://www.mihai-nita.net/article.php?artID=20050306b
and here:
http://www.microsoft.com/globaldev/getwr/steps/wrg_unicode.mspx#EGAAC
and in the end here:
http://blogs.msdn.com/michkap/archive/2007/01/05/1413001.aspxhttp://blogs.
msdn.com/michkap/archive/2007/01/05/1413001.aspx

(I would recomend that order, takes you from simple to more detailed)
 
Mihai and Giovanni
Thanks for responding. I changed the Project properties as you described but
my application is still executing code that has #ifndef _UNICODE wrapped
around it. Do I need to build my own copy of the C run-time library files
with _UNICODE defined?

Craig
 
Craig said:
Mihai and Giovanni
Thanks for responding. I changed the Project properties as you described but
my application is still executing code that has #ifndef _UNICODE wrapped
around it. Do I need to build my own copy of the C run-time library files
with _UNICODE defined?

Craig:

No. Everything should take care of itself.
 
I changed the Project properties as you described but
my application is still executing code that has #ifndef _UNICODE wrapped
around it.
My normal reaction would be "this is not possible"
Unless you have uncovered some really weird bug in the compiler
or the editor/debugger. Which I think it is kind of unlikely, we
are talking about the preprocessor, not some advanced template handling.

Can you "distill" the thing to a small example that you can share?

Do I need to build my own copy of the C run-time library files
with _UNICODE defined?
No, it should not need this.
 
Mihai
This code gets executed on application startup:

#ifndef _UNICODE
if (bufferiswide && (textlen > 0)) {
wchar_t *p;
int retval, count;
char buffer[MB_LEN_MAX+1];

p = text.wz;
count = textlen;
while (count--) {
retval = wctomb(buffer, *p++);
if (retval <= 0)
break;
WRITE_STRING(buffer, retval, &charsout);
}
} else {
WRITE_STRING(text.sz, textlen, &charsout);
}
#else /* _UNICODE */

The module that contains this code is in the file C:\Program Files\Microsoft
Visual Studio .NET 2003\Vc7\crt\src\output.c
This code is in the function dll!_output shown in the call stack below:

msvcr71d.dll!write_string(char * string=0x7c9131dc, int len=0x7c913212,
_iobuf * f=0x00000000, int * pnumwritten=0x7c913288) Line 1258 + 0x19
msvcr71d.dll!_output(_iobuf * stream=0xcccccccc, const char *
format=0xcccccccc, char * argptr=0xcccccccc) Line 1031 + 0x18 Ccccccccc()
Visual Engineer.exe!$E7() Line 2602 + 0x28 C++
msvcr71d.dll!_initterm(void (void)* * pfbegin=0x00b2f4b8, void (void)* *
pfend=0x00b354d4) Line 600
Visual Engineer.exe!wWinMainCRTStartup() Line 336 + 0xf
kernel32.dll!7c816fd7()

Thanks,
Craig
 
The module that contains this code is in the file
C:\Program Files\Microsoft
Visual Studio .NET 2003\Vc7\crt\src\output.c

Ah!

That is part of the CRT.
msvcrXX.dll contains both Unicode and non-Unicode functions.
The file is probably compiled both as Unicode and as non-Unicode and the two
resulting obj files are linked together.

My best guess with this info: you are probably calling a non-Unicode api
from your Unicode application.
Try going step by step and see exactly what call triggers the mess.
That is probably the ansi call.
 
Back
Top