64,266
社区成员
发帖
与我相关
我的任务
分享
#ifdef _UNICODE
#undef _UNICODE
// #undef clearly directed TCHAR to be defined as char
// even if I made my projected character set unicode
// as verified by the following screenshot.
#include <tchar.h>
#endif
#ifdef UNICODE
#undef UNICODE
// PathFileExists can be PathFileExistsW or PathFileExistsA and
// my Shlwapi.h shows that its declaration is controlled by the
// predefined macro UNICODE, not _UNICODE.
// not sure what's the difference between them.
#include <Shlwapi.h> // PathFileExists
#endif
// as you said earlier, there is only one MakeSureDirectoryPathExists
#include <DbgHelp.h> // MakeSureDirectoryPathExists
// if your compiler does not support this file or the later static_assert,
// then take them out.
#include <type_traits> // std::is_same
int main ()
{
TCHAR strDirectory [] = _T("c:\\1111\\222\\");
static_assert(std::is_same<TCHAR,char>::value,"bad");
if (!PathFileExists(strDirectory))
{
MakeSureDirectoryPathExists(strDirectory);
}
return 0;
}
#define _UNICODE
#ifdef _UNICODE
typedef WCHAR TCHAR;
#endif
#undef _UNICODE
完成预编译之后的代码是
typedef WCHAR TCHAR;
楼主期望的做法应该是
#define _UNICODE
#undef _UNICODE // 你的这句代码应该在这个位置 才有你想要的作用
#ifdef _UNICODE
typedef WCHAR TCHAR;
#endif
预编译完成之后是
没错就是什么都没有了
楼主好好体会一下
#ifdef _UNICODE
#undef _UNICODE //我已经取消掉了,那么TCHAR则是char!!!
我理解原因是 preprocessor 进来以后按顺序处理,到达 #include <windows.h> 的时候 _UNICODE 还是有定义的,因此生成的函数对应 unicode 的接口,进入 main 以后 _UNICODE 被强行取消,导致生成的字符串类型和函数类型不匹配。