the difference between dev C++ and visual C++ 2008

why the function CopyFile(), GetModuleFileName(), etc works in dev and not in visual C++ 2008? the error result said that the function cannot convert char[] to LPCWSTR (what is that dude? :)) )
LPCWSTR: http://msdn.microsoft.com/en-us/library/cc230352(PROT.10).aspx

Bloodshed Dev-C++ is out of date and so it will not do the same checks against your code that M$ Visual Studio will. I would go with what M$VS says and say your code is wrong.

If you prefer the layout and feel of Dev-C++, I know that I do, then use the spin-off version found here: http://wxdsgn.sourceforge.net/
It's much more up to date with the C++ standard.
as i excpected... :)
The problem is that VC++ defines UNICODE by default (which is a good thing), while MinGW doesn't. When this macro is defined, the Windows headers use the UTF-16 (or "wide", because it uses wider characters) versions of the functions. It's possible to force usage of the ANSI version regardless of whether UNICODE is defined by appending A to the identifier. For example: CopyFileA(). The equivalent for the wide version is CopyFileW().
Visual Studio comes now configured to produce Unicode builds. You are used to using char all over the place. That is bad when programming Windows. Whoever is teaching you Windows programming should have taught you about TCHAR, TEXT() and related topics.

Either change the project to Multybyte (not recommended), or start using TCHAR and TEXT():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <iostream>
#include <Windows.h>

#ifdef _UNICODE
#define tcout std::wcout
#define tcin std::wcin
#else
#define tcout std::cout
#define tcin std::cin
#endif

typedef std::basic_string<TCHAR> tstring;

int main()
{
    tstring fileName(MAX_PATH, TEXT(' '));
    ::GetModuleFileName(NULL, &fileName[0], MAX_PATH);
    tcout << TEXT("Hello from ") << fileName << TEXT(" !!!") << std::endl;
    return 0;
}


As you can see, you can easily get TCHAR-friendly data types and variables. A proper Windows program in C/C++ should exhibit TCHAR only everywhere, except in parts that are COM-related because COM works only with Unicode strings. In here you'll be forced to use wchar_t and related data types, such as LPWSTR, LPCWSTR and BSTR.

EDIT: One more: OLESTR, LPOLESTR (I think I have seen these 2 before).
Last edited on
i thought TCHAR is unicode string, wasn't it? what is COM? isn't it that the compiler asked me to use LPCWSTR?
cannot convert char[] to LPCWSTR
Last edited on
TCHAR = char when the project is configured as Multibyte, and TCHAR = wchar_t when it is configured as Unicode.

To further clarify, the project properties are able to "inject" #define's into the preprocessor. A couple of those are

1
2
#define _UNICODE
#define UNICODE 


On the other hand, the Windows headers (and also the tchar.h header) define TCHAR as

1
2
3
4
5
#ifdef _UNICODE
typedef char TCHAR;
#else
typedef wchar_t TCHAR;
#endif 


This way the Windows headers are good for either Multibyte or Unicode builds. Once TCHAR is defined, it is a matter of defining other types based on it, like LPTSTR (which becomes LPWSTR when compiling for Unicode), LPCTSTR, etc.
btw what's the difference between char in TCHAR and "the ordinary char"?
?? No difference, or I don't get your question. If your project is built for Multibyte, TCHAR == char exactly; if your project is built for Unicode, TCHAR == wchar_t exactly.
oh because, i'm a little bit confused about your answer before. you said:

You are used to using char all over the place. That is bad when programming Windows. Whoever is teaching you Windows programming should have taught you about TCHAR, TEXT() and related topics.


so i thought that TCHAR is a new type, but you said that char and TCHAR is the same so that makes me confused. but now, as long as i see, your purpose for using TCHAR is for "optional" usage, for example, if your code isused for unicode then TCHAR take "role" as wchar_t and for multybyte (ordinary project?) as char. am i right?

i'm a little bit curious about this:

 
#ifdef _UNICODE 

is it means: "if unicode is defined" ? if so, why you're saying:


if your project is built for Unicode, TCHAR == wchar_t exactly.


edit: my question in bold, it looks confused before...
Last edited on
Throughout your program, if you use TCHAR, TEXT(), etc, then converting your program between multibyte and Unicode is merely a matter of #defining _UNICODE, as that definition will set TCHAR and all related types and functions to be the ANSI or wide versions.
hehe, my bad. :-( It should have been the other way around (#ifdef _UNICODE, then TCHAR is wchar_t and NOT char).

And OK, yes, there is a difference since I now understood your question: The Windows SDK is designed so it compiles for multibyte or Unicode. Basically, the way they do it is to define the TCHAR datatype conditionally as shown before. The other 2 main tricks are TEXT() (or _T()) for string literals and the function names. The function names are not function names: They are #define 's. The real function names end with A or W, depending on whether they are ANSI (multibyte) or Unicode (wide chars).

Example: MessageBox is really:

1
2
3
4
5
#ifdef UNICODE
#define MessageBox MessageBoxW
#else
#define MessageBox MessageBoxA
#endif 


So there you have it: Whenever you program for windows, use TCHAR, TEXT() and the function names (which are really just macros).
oh so i guess my answer before is right, right? :)

note: i've edited my question before so anyone who read it don't get confused... :))
Topic archived. No new replies allowed.