Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2007

Tip: Looking for answers? Try searching our database.

Building a Unicode application.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Craig Johnston - 16 Oct 2007 17:33 GMT
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
Giovanni Dicanio - 16 Oct 2007 23:10 GMT
>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
Mihai N. - 17 Oct 2007 10:15 GMT
> 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
[quoted text clipped - 3 lines]
> 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)

Signature

Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email

Craig Johnston - 17 Oct 2007 17:41 GMT
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

> > 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
[quoted text clipped - 16 lines]
>
> (I would recomend that order, takes you from simple to more detailed)
David Wilkinson - 17 Oct 2007 18:20 GMT
> 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.

Signature

David Wilkinson
Visual C++ MVP

Mihai N. - 18 Oct 2007 04:54 GMT
> 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.

Signature

Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email

Craig Johnston - 18 Oct 2007 15:59 GMT
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

> > I changed the Project properties as you described but
> > my application is still executing code that has #ifndef _UNICODE wrapped
[quoted text clipped - 9 lines]
> > with _UNICODE defined?
> No, it should not need this.
Mihai N. - 19 Oct 2007 08:43 GMT
> 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.

Signature

Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.