UNICOD issue

This is a discussion on UNICOD issue within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I'm pretty much new to windows programming. I'm trying to write my own MessageBox function.So far I manage to ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    UNICODE issue

    Hi, I'm pretty much new to windows programming.
    I'm trying to write my own MessageBox function.So far I manage to do this:
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    
    int __cdecl MyMessageBox (HWND hwnd,TCHAR * szCaption,UINT uType,const TCHAR * szFormat, ...)
    {
         TCHAR szBuffer[1024];
    	 va_list pArgList;
    	 va_start(pArgList,szFormat);
    
    	 _vsntprintf(szBuffer,sizeof(szBuffer)/sizeof(TCHAR),szFormat,pArgList);
    
    	 return MessageBox(NULL,szBuffer,szCaption,0);
    }
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow) 
    {
         int x=5;
    	 MyMessageBox(NULL,_T("Caption"),MB_OK,_T("%d"),x);
         return 0 ;
    }
    When I compile it and run it everything works fine, but if I do this:

    Code:
    #define UNICODE
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    
    int __cdecl MyMessageBox (HWND hwnd,TCHAR * szCaption,UINT uType,const TCHAR * szFormat, ...)
    {
         TCHAR szBuffer[1024];
    	 va_list pArgList;
    	 va_start(pArgList,szFormat);
    
    	 _vsntprintf(szBuffer,sizeof(szBuffer)/sizeof(TCHAR),szFormat,pArgList);
    
    	 return MessageBox(NULL,szBuffer,szCaption,0);
    }
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow) 
    {
         int x=5;
    	MyMessageBox(NULL,_T("Caption"),MB_OK,_T("%d"),x);
         return 0 ;
    }
    I get compiler errors:
    error C2664: '_vsnprintf' : cannot convert parameter 1 from 'TCHAR [1024]' to 'char *'

    error C2664: 'MyMessageBox' : cannot convert parameter 2 from 'char [8]' to 'TCHAR *'

    I manage to eliminate the second error by changing line:

    Code:
    MyMessageBox(NULL,_T("Caption"),MB_OK,_T("%d"),x);
    to line:
    Code:
    MyMessageBox(NULL,TEXT("Caption"),MB_OK,TEXT("%d"),x);
    This is not what I expected because I found in header files something like this:
    Code:
    ...
    #define _T(x) L##x //tchar.h
    ...
    #define _TEXT(quote) L##quote //winnt.h
    ...
    #define TEXT(quote) __TEXT(quote)
    I thought that _T and TEXT are equivalent, but it seems they aren't.
    Anyway I was not be able to solve first error, so my program will not work if UNICODE is defined.
    I tried to use preprocessor and to define _vsntprintf as
    _vsnwprintf if UNICODE is defined, but failed.

    So I have two question:
    1. How to fix this errors so my program work with UNICODE defined
    2. What is the difference between _T and TEXT.
    I'm using .net and windows XP
    Thank you very much!
    Last edited by Micko; 08-29-2004 at 11:50 AM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    1. #define_UNICODE and UNICODE
    2. 2 extra characters of typing (ie. functionally, none).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Thanks dude!
    So the key reason is to define _UNICODE
    I made my program work before I read your post. I wrote this:
    Code:
    #define UNICODE
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    
    #ifdef UNICODE
    #undef _vsntprintf
    #define _vsntprintf _vsnwprintf
    #else
    #define _vsntprintf _vsnprintf
    #endif
    
    int __cdecl MyMessageBox (HWND hwnd,TCHAR * szCaption,UINT uType,const TCHAR * szFormat, ...)
    {
         TCHAR szBuffer[1024];
    	 va_list pArgList;
    	 va_start(pArgList,szFormat);
    	 _vsntprintf(szBuffer,sizeof(szBuffer)/sizeof(TCHAR),szFormat,pArgList);
    
    	 return MessageBox(NULL,szBuffer,szCaption,uType);
    }
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow) 
    {
         int x=5;
    	 MyMessageBox(NULL,TEXT("Caption"),MB_OK,TEXT("%d"),x);
         return 0 ;
    }
    I know how silly this code looks now.
    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 05:56 AM
  2. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  3. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  4. Idle Issue.
    By Charmy in forum C++ Programming
    Replies: 7
    Last Post: 06-24-2005, 07:10 AM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 05:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21